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

时间:2018-04-05 06:49:16

标签: wcf web-config

经过几个小时的搜索示例,其中大部分只包含方法片段,但不包含“整个图片”。我在寻求指导。从开箱即用的web.config开始,Visual Studio使用新的WCF服务创建,我编写了我的基本Web服务。在调试中运行时,WCF测试客户端会显示您可以测试的功能。这很棒。现在,想要将代码移动到IIS(首先在我的本地机器上,然后在使用SSL的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测试停止工作。它可以支持简单配置和托管配置吗?

感谢。

1 个答案:

答案 0 :(得分:1)

客户端应用程序使用<client>中的<system.serviceModel>部分来指定服务端点的"ABC" properties(地址,绑定和合同)。您应该在桌面应用程序中包含该部分,以便可以从服务器配置中删除它。

然而,桌面应用程序的app.config中的<client>部分应具有与服务端点相同的“ABC”属性。由于您的服务绑定是webHttpBinding,客户端也应该webHttpBinding作为绑定,但我可以看到它所指的bindingConfiguration,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 transformation syntax。在这种情况下,您的Release 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>

通过这种方式,每当您在调试模式下构建项目时,它将配置为withoud SSL,并且无论何时在发布模式下构建,它都将使用SSL。