IIS 宿主和 SSL 的 WCF web.config 文件设置



经过数小时的搜索示例,其中大多数仅包含方法片段,但不包含我寻求指导的"全貌"。 从Visual Studio使用新的WCF服务创建的开箱即用的web.config开始,我编写了基本的Web服务。 在调试中运行时,WCF 测试客户端将显示可以测试的函数。 这太好了。 现在,想要将代码移动到 IIS(首先在我的本地计算机上,然后在使用 SSL 的 Web 服务器旁边),我添加了一些我在 Web 上找到的代码。 我确实有过一次我的配置工作,但设法更改了太多,以至于我丢失了原始配置。 所以,我有这个:

<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5"/>
</system.web>
<system.serviceModel>
<protocolMapping>
<add scheme="http" binding="webHttpBinding"/>
</protocolMapping>
<services>
<service name="TaskTrackerAppService.Service1" behaviorConfiguration="">
<endpoint address="" 
binding="webHttpBinding" 
contract="TaskTrackerAppService.IAppWebService"
behaviorConfiguration="WebBehavior"></endpoint>
<endpoint address="mex" binding="mexHttpBinding"
contract="IMetadataExchange" bindingConfiguration=""></endpoint>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>                
</serviceBehaviors>
<endpointBehaviors>
<behavior name="WebBehavior">
<webHttp/>
</behavior>        
</endpointBehaviors>
</behaviors>
<bindings>
<basicHttpBinding>
<binding name="TaskTrackerAppService.IAppWebService"></binding>
</basicHttpBinding>
</bindings>
<client>
<endpoint address="" binding="webHttpBinding" 
bindingConfiguration="TaskTrackerAppService.IAppWebService" 
contract="TaskTrackerAppService.IAppWebService"></endpoint>
</client>

<serviceHostingEnvironment aspNetCompatibilityEnabled="true" 
multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
<!--
To browse web app root directory during debugging, set the value below to true.
Set to false before deployment to avoid disclosing web app folder information.
-->
<directoryBrowse enabled="true"/>
</system.webServer>

我将客户端桌面应用程序服务引用配置为指向本地 IP http:192.168.0.100:90/AppWebService.svc。 然后,当我运行客户端应用程序时,出现错误:

Could not find default endpoint element that references contract 'ServiceReference.IAppWebService' in the ServiceModel client configuration section. This might be because no configuration file was found for your application, or because no endpoint element matching this contract could be found in the client element.

所以我想纠正 web.config 设置。 然后部署到 SSL 准备就绪的托管 IIS 服务。 作为奖励,是否有办法配置端点,以便我仍然可以运行调试器并获取 WCF 测试客户端。 在曾经工作的配置中,WCF 测试停止工作。它能否同时支持简单配置和托管配置?

谢谢。

客户端应用程序使用<system.serviceModel>中的<client>部分来指定服务终结点的"ABC"属性(地址、绑定和协定)。您应该在桌面应用程序中具有该部分,以便您可以简单地将其从服务器配置中删除。

但是,桌面应用程序的 app.config 中的<client>部分应具有与服务终结点相同的"ABC"属性。由于您的服务绑定是webHttpBinding客户端也应该具有webHttpBinding作为绑定,但我可以看到它所引用的绑定配置,TaskTrackerAppService.IAppWebService实际上是一个basicHttpBinding,所以这是一个错误的配置。

此外,由于您的生产环境正在使用 SSL,因此您的生产 web.config 应该具有类似于以下内容的 SSL 绑定配置:

<bindings>
<webHttpBinding>
<binding name="webBindingHTTPS">
<security mode="Transport">
<transport clientCredentialType="None" />
</security>
</binding>
</webHttpBinding>
</bindings>

使用以下端点配置:

<endpoint address="" 
binding="webHttpBinding" 
contract="TaskTrackerAppService.IAppWebService"
behaviorConfiguration="webBindingHTTPS"></endpoint>

实现此目的的最佳方法是使用 web.config 转换语法。在这种情况下,您的发布 web.config 可能具有以下元素:

<bindings>
<webHttpBinding>
<binding name="webBindingHTTPS" xdt:Transform="Insert">
<security mode="Transport">
<transport clientCredentialType="None" />
</security>
</binding>
</webHttpBinding>
</bindings>
<endpoint address="" xdt:Transform="Replace" xdt:Locator="Match(name)"
binding="webHttpBinding" 
contract="TaskTrackerAppService.IAppWebService"
behaviorConfiguration="webBindingHTTPS">
</endpoint>

这样,无论何时在调试模式下构建项目,它都将使用 SSL 进行配置,无论何时在发布模式下构建,它都将使用 SSL。

最新更新