在WCF服务上同时支持HTTP和HTTPS

时间:2019-02-26 10:19:17

标签: c# wcf

这里甚至在阅读了Microsoft文档https://docs.microsoft.com/en-us/dotnet/framework/wcf/configuring-services-using-configuration-filesHow can I combine the WCF services config for both http and https in one web.config?

之后,都对此主题进行了多次讨论。

我仍然不太了解WCF服务所有属性的工作方式,也无法使我的服务支持两种协议。

我可以使服务毫无问题地支持HTTP或HTTPS 这里的问题是使服务自行选择正确的协议

关键点是第一个端点上的绑定协议,其他任何更改(如下面进一步显示的更改)都没有影响。

App.config-

<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>

绑定-

<system.serviceModel>
  <bindings>
    <basicHttpBinding>
      <binding name="AccountServicePortSoap11"/>
        <security mode="None">
          <transport clientCredentialType="None" proxyCredentialType="None" realm=""/>
          <message algorithmSuite="Default" />
        </security>
      </binding>
      <binding name="AccountServicePortSoap22" />
        <security mode="Transport">
          <transport clientCredentialType="Window" proxyCredentialType="None" realm=""/>
          <message algorithmSuite="Default" />
        </security>
      </binding>
    </basicHttpBinding>
  </bindings> 

终点-

<client>
       <endpoint address="" bindingConfiguration="AccountServicePortSoap11" binding="basicHttpBinding" contract="AccountServiceRef.AccountServicePort" name="AccountServicePortSoap11" />
       <endpoint address="" bindingConfiguration="AccountServicePortSoap22" binding="basicHttpBinding" contract="AccountServiceRef.AccountServicePort" name="AccountServicePortSoap22" />
</client>

App.config的其他版本

  <appSettings>
    <add key="defaultServer" value="http://**.**.**.**:80 " />
  </appSettings>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="DocumentFormat.OpenXml" publicKeyToken="******" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-2.8.1.0" newVersion="2.8.1.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>  

我尝试过的更改-

<serviceBehaviors>
          <behavior name="MyServiceBehavior">
            <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
            <serviceDebug includeExceptionDetailInFaults="true" />
          </behavior>
        </serviceBehaviors>
      </behaviors>
      <serviceHostingEnvironment multipleSiteBindingsEnabled="true"/>
        <protocolMapping>
    <add scheme="http"  binding="basicHttpBinding" bindingConfiguration="AccountServicePortSoap22"/>
    <add scheme="https" binding="basicHttpBinding" bindingConfiguration="AccountServicePortSoap11"/>      
  </protocolMapping>
      <services>
        <service behaviorConfiguration="MyServiceBehavior" name="com.advantage.online.store.accountservice" >
          <endpoint address="" binding="basicHttpBinding" contract="AccountServiceRef.AccountServicePort" name="AccountServicePortSoap11" />
          <endpoint address="" binding="basicHttpBinding" contract="AccountServiceRef.AccountServicePort" name="AccountServicePortSoap22" />
        </service>
      </services>

1 个答案:

答案 0 :(得分:0)

通常来说,我可以通过以下几种方式通过http和https托管服务。

  <system.serviceModel>
<behaviors>
  <serviceBehaviors>
    <behavior>
      <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
      <serviceDebug includeExceptionDetailInFaults="false"/>
    </behavior>
  </serviceBehaviors>
  <endpointBehaviors>
    <behavior>
      <webHttp />
    </behavior>
  </endpointBehaviors>
</behaviors>
<bindings>
  <webHttpBinding>
    <binding name="mybinding">
      <security mode="Transport">
        <transport clientCredentialType="None"></transport>
      </security>
    </binding>
  </webHttpBinding>
</bindings>
<protocolMapping>
  <add binding="webHttpBinding" scheme="http"/>
  <add binding="webHttpBinding" scheme="https" bindingConfiguration="mybinding"/>
</protocolMapping>    
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />

结果。 enter image description here 我配置了两个端点,分别用于支持https / http。顺便说一句,我需要在IIS网站绑定模块中添加https基地址。
https://docs.microsoft.com/en-us/dotnet/framework/wcf/feature-details/how-to-configure-an-iis-hosted-wcf-service-with-ssl
随时让我知道是否有什么可以帮助您的。