【发布时间】:2018-11-30 16:24:02
【问题描述】:
我正在尝试在HTTP 和HTTPS 上启用ServiceHost。
这是运行服务的代码:
oServiceHost = new ServiceHost(typeof(API), new Uri(WebHookURL))
oServiceHost.Open();
正如您在此处看到的 - 我在运行时获取服务 URL (WebHookURL)。
如前所述,URL 协议可以是HTTP 或HTTPS。
经过大量阅读和测试,最终得到了这个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>
当尝试将 WebHookURL(例如:http://localhost:8111)设置为 http 地址时 - 代码可以正常工作。
不幸的是,当将WebHookURL 设置为https 地址时(例如: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].
我错过了什么?
更新 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>
【问题讨论】:
-
更好的方法是在前面放置一个网络服务器并终止服务器上的 SSL,然后转发请求