在WCF服务上支持HTTP和HTTPS

问题描述 投票:0回答:1

这个主题在这里讨论过几次,但即使在阅读了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>
c# wcf
1个回答
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 如果有什么我可以帮忙,请随时告诉我。

© www.soinside.com 2019 - 2024. All rights reserved.