运行ServiceHost时无法同时支持http和https url

oha*_*nho 6 c# wcf

我正在尝试ServiceHost在两个HTTP&上启用曝光HTTPS.

以下是运行该服务的代码:

oServiceHost = new ServiceHost(typeof(API), new Uri(WebHookURL))
oServiceHost.Open();
Run Code Online (Sandbox Code Playgroud)

正如您在此处所见 - 我WebHookURL在运行时获取服务URL().如上所述,URL协议可以是HTTPHTTPS.

经过大量的阅读和测试,它归结为这个web.config文件:

 <system.serviceModel>
    <client>
      <endpoint binding="customBinding" bindingConfiguration="encryptingBinding" contract="ModuleComm.Commons.ServiceContracts.ModuleService" name="clientConf" />
    </client>
    <services>
      <service name="myServiceWebServer.AsynchronousSocketListener">
        <endpoint binding="customBinding" bindingConfiguration="encryptingBinding" contract="ModuleComm.Commons.ServiceContracts.ModuleService" />
      </service>
      <service behaviorConfiguration="API.Service1Behavior" name="myServiceWebServer.API">
        <endpoint address="" behaviorConfiguration="web" binding="webHttpBinding" contract="myServiceWebServer.IAPI" />
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="API.Service1Behavior">
          <serviceMetadata httpGetEnabled="True" httpsGetEnabled="True" />
          <serviceDebug includeExceptionDetailInFaults="False" />
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="web">
          <webHttp />        
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <bindings>
      <customBinding>
        <binding name="encryptingBinding">
          <!--  <messageEncryping /> -->
          <textMessageEncoding>
            <readerQuotas maxStringContentLength="2147483647" />
          </textMessageEncoding>
          <tcpTransport maxReceivedMessageSize="2147483647" maxBufferSize="2147483647">
            <connectionPoolSettings leaseTimeout="23:59:00" maxOutboundConnectionsPerEndpoint="10000" />
          </tcpTransport>
        </binding>
      </customBinding>
      <webHttpBinding>
        <binding name="webBinding">
          <security mode="Transport">
            <transport clientCredentialType="None"/>
          </security>
        </binding>
      </webHttpBinding>
    </bindings>
    <protocolMapping>
      <add binding="webHttpBinding" bindingConfiguration="webBinding" scheme="https" />
    </protocolMapping>
  </system.serviceModel>
Run Code Online (Sandbox Code Playgroud)

当试图设置WebHookURL(例如:http:// localhost:8111)到一个http地址 - 代码工作正常.

不幸的是,当设置WebHookURLhttps地址(例如:https:// localhost:8111)时 - 代码将无法工作,并且在尝试创建ServiceHost实例时会抛出此异常:

Could not find a base address that matches scheme http for the endpoint with binding WebHttpBinding. Registered base address schemes are [https].
Run Code Online (Sandbox Code Playgroud)

我错过了什么?

更新1:

试过这个配置,但我得到配置错误:试过这个,但我得到配置错误:

<system.serviceModel>
    <services>      
      <service behaviorConfiguration="API.Service1Behavior" name="WebServer.API">
        <endpoint address="" behaviorConfiguration="web" binding="webHttpBinding" bindingConfiguration="webBinding" contract="WebServer.IAPI" />
        <endpoint address="" behaviorConfiguration="web" binding="webHttpBinding" bindingConfiguration="wsBindingHTTPS" contract="WebServer.IAPI" />
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="API.Service1Behavior">
          <serviceMetadata httpGetEnabled="True" httpsGetEnabled="True" />
          <serviceDebug includeExceptionDetailInFaults="False" />
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="web">
          <webHttp />        
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <bindings>      
      <wsHttpBinding>        
        <binding name="webBinding">
          <security mode="None">
            <transport clientCredentialType="None"/>
          </security>
        </binding>
        <binding name="wsBindingHTTPS">
          <security mode="Transport">
            <transport clientCredentialType="None"/>
          </security>
        </binding>
      </wsHttpBinding>
    </bindings>
  </system.serviceModel>
Run Code Online (Sandbox Code Playgroud)

Bar*_*r J 0

问题就在这里:

   <webHttpBinding>
        <binding name="webBinding">
          <security mode="Transport">
            <transport clientCredentialType="None"/>
          </security>
        </binding>
      </webHttpBinding>
Run Code Online (Sandbox Code Playgroud)

webHttpBinding仅支持HTTP请求,不支持HTTPS请求。webHttpBinding 也不支持互操作性。

WsHttpBinding还支持互操作性。通过此绑定,SOAP 消息默认是加密的。它支持 HTTP 和 HTTPS。在编码方面,它提供了对Text以及MTOM编码方式的支持。它支持 WS-* 标准,例如 WS-Addressing、WS-Security 和 WS-ReliableMessaging。默认情况下,可靠会话被禁用,因为它可能会导致一些性能开销。 http://www.codeproject.com/Articles/431291/WCF-Services-Choosing-the-property-WCF-binding

因此,为了使用 https,请替换webHttpBindingWsHttpBinding.

这是与您的解决方案相匹配的代码片段:

1)为服务编写两个端点,一个用于http,另一个用于https。

<services>
    <service behaviorConfiguration="MyServiceBehavior" name="JK.MyService">

      <endpoint address="" behaviorConfiguration="WebBehavior" binding="webHttpBinding" bindingConfiguration="webBinding" contract="JK.IMyService">
        <identity>
          <dns value="localhost" />
        </identity>
      </endpoint>

      <endpoint address="" behaviorConfiguration="WebBehavior" binding="webHttpBinding" bindingConfiguration="webBindingHTTPS" contract="JK.IMyService">
        <identity>
          <dns value="localhost" />
        </identity>
      </endpoint>     

    </service>
  </services>
Run Code Online (Sandbox Code Playgroud)

2) 在 serviceBehaviors 中启用 httpGetEnabled="True" httpsGetEnabled="true"。

<behaviors>

<serviceBehaviors>      
  <behavior name="MyServiceBehavior">
    <serviceMetadata httpGetEnabled="True" httpsGetEnabled="true"/>
    <serviceDebug includeExceptionDetailInFaults="true" />
  </behavior>      
</serviceBehaviors>

<endpointBehaviors>
  <behavior name="WebBehavior">
    <webHttp/>
  </behavior>
</endpointBehaviors>

</behaviors>
Run Code Online (Sandbox Code Playgroud)

3)编写http和https的两个绑定配置。对于 http,指定安全模式=“无”;对于 https,指定安全模式=“传输”。

<bindings>
    <wsHttpBinding>

      <binding name="webBinding">
        <security mode="None">
          <transport clientCredentialType="None" />
        </security>
      </binding>

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

    </wsHttpBinding>
  </bindings>
Run Code Online (Sandbox Code Playgroud)

检查此链接