【问题标题】:Support both HTTP and HTTPS on WCF service在 WCF 服务上同时支持 HTTP 和 HTTPS
【发布时间】:2019-02-26 10:19:17
【问题描述】:

这个话题在这里讨论了好几次,但即使在阅读了微软文档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】:

    一般来说,我有以下几种方式通过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" />
    

    结果。 我配置了两个端点,分别用于支持 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
    如果有什么可以帮助的,请随时告诉我。

    【讨论】:

    • 我们对这个建议的问题是,尽管对代码非常熟悉,但我们似乎无法找到服务名称。没有服务名称,我们无法在“服务”下定义端点。找到正确的服务名称(如果有的话)可能是解决方案,尽管需要花费数小时的时间来寻找正确的服务名称。
    • 您在客户端工作吗?你是什​​么意思你找不到服务名称?我注意到您已经设置了服务的名称(Namespace.MyServiceClass),服务的合同(Namespace.IServiceInterface)。接口和实现类一般位于wcf服务项目中。
    • 我在各个方面工作,也在客户端工作。 “无法找到 serviceName”的含义是,我们尝试在 app.config 上列出的每个服务名称都是 Visual Studio 无法接受的,并带有此错误消息 - “根据其数据类型“服务名称类型”,该名称无效。我们没有编写代码,但我已经在这个项目上工作了几个月了。
    • 尝试清理并重建项目,然后输入服务名称/服务接口时智能感知服务名称。此外,我建议您参考上面的示例,通过一个简单的示例来理解如何通过 https/http 托管服务。
    • 您可以使用简化配置。查看修改后的回复。
    猜你喜欢
    • 2015-09-15
    • 1970-01-01
    • 2023-02-08
    • 1970-01-01
    • 2014-09-23
    • 2014-10-17
    • 2010-12-16
    • 2016-02-13
    • 1970-01-01
    相关资源
    最近更新 更多