支持 HTTP 和 HTTPS Web.Config WCF 服务
Posted
技术标签:
【中文标题】支持 HTTP 和 HTTPS Web.Config WCF 服务【英文标题】:Support both HTTP and HTTPS Web.Config WCF Service 【发布时间】:2015-09-15 07:49:26 【问题描述】:在尝试同时支持 https 和 https 时,我的 WCF 服务遇到了配置问题。理想情况下,我想要在我的开发机器上运行 http,然后发布到运行 https 的天蓝色。
我按照这些帖子尝试运行配置: http://jayakrishnagudla.blogspot.com/2009/12/configuring-wcf-services-to-work-with.html
How to configure a single WCF Service to have multiple HTTP and HTTPS endpoints?
我的Web.Config如下:
<system.serviceModel>
<services>
<service name="Backend.Services.UserService.UserService" behaviorConfiguration="">
<endpoint address=""
binding="webHttpBinding"
contract="Backend.Services.UserService.IUserService"
bindingConfiguration="HttpsBinding"
behaviorConfiguration="Web">
</endpoint>
<endpoint address=""
binding="webHttpBinding"
contract="Backend.Services.UserService.IUserService"
bindingConfiguration="HttpBinding"
behaviorConfiguration="Web"
>
</endpoint>
</service>
</services>
<bindings>
<webHttpBinding>
<binding name ="HttpBinding">
<security mode="None">
<transport clientCredentialType="None"/>
</security>
</binding>
<binding name="HttpsBinding">
<security mode="Transport"/>
</binding>
</webHttpBinding>
</bindings>
<behaviors>
<serviceBehaviors>
<behavior name="ServiceBehavior">
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="Web">
<webHttp />
</behavior>
</endpointBehaviors>
</behaviors>
<protocolMapping>
</protocolMapping>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
</system.serviceModel>
据我所知,根据上面的链接,这个配置应该是正确的。但是,当我通过 http 在本地构建和运行此服务时,出现以下错误:
Could not find a base address that matches scheme https for the endpoint with binding WebHttpBinding. Registered base address schemes are [http].
我不太确定问题出在哪里,并认为是配置错误。任何帮助,将不胜感激。
【问题讨论】:
这可能是因为您将服务配置为使用 https 运行,但是,当 .NET 开发服务器加载您的应用程序时,它没有使用 https。设置一个测试,部署在你的开发机器上的 IIS 下并访问 http://DEVBOX/myapp。如果您的应用配置正确,则不会出现错误。 是的,如果我使用 https//DEVBOX/myapp,我会收到“连接被拒绝”,因为我没有在我的开发机器上配置 https 证书,但是如果我使用 http//DEVBOX/myapp我可以成功地看到可用的服务。该错误仅在我尝试访问 .svc 服务时发生。在此之前,一切都可以通过开发箱上的 http 正常工作。 【参考方案1】:我已经尝试了几天(实际上是几年,但几天前才重新开始),上面的帖子用protocolMapping
为我指明了正确的方向,但你需要指定bindingConfiguration
在protocolMapping
部分中使其选择
<system.serviceModel>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true"/>
<protocolMapping>
<add scheme="http" binding="basicHttpBinding" bindingConfiguration="ServiceBinding"/>
<add scheme="https" binding="basicHttpBinding" bindingConfiguration="ServiceBindingSSL"/>
</protocolMapping>
将bindingConfiguration
属性保留在端点之外——它将根据协议自动设置。然后在绑定部分设置第二个绑定配置:
<basicHttpBinding>
<binding name="ServiceBinding" ...>
<security mode="None">
</security>
</binding>
<binding name="ServiceBindingSSL" ... >
<security mode="Transport">
</security>
</binding>
</basicHttpBinding>
这项技术对我有用。希望对你有帮助!
【讨论】:
【参考方案2】:你看过协议映射 wcf 配置部分吗?
<protocolMapping>
<add scheme="http" binding="wsHttpBinding"/>
<add scheme="https" binding="wsHttpBinding"/>
</protocolMapping>
编辑:我不确定您为什么在 webHttpBinding 类型下配置了“https”绑定。您不应该同时定义 http webHttpBinding 和 wsHttpBinding 并将其分配给您的端点吗?
<webHttpBinding>
<binding name ="HttpBinding">
<security mode="None">
<transport clientCredentialType="None"/>
</security>
</binding>
</webHttpBinding>
<wsHttpBinding>
<binding name="wsHttpEndpointBinding">
<security mode="TransportWithMessageCredential">
<transport clientCredentialType="None" />
<message clientCredentialType="UserName" />
</security>
</binding>
</wsHttpBinding>
【讨论】:
我在上述方案中添加了,但是当我在本地运行并通过 http 本地部署时,我仍然收到与我的帖子中相同的错误。 我在上面进行了编辑,希望对您有所帮助。除非您安装了有效的证书,否则您将无法看到 https,但是,您应该能够告诉它允许 http 连接。【参考方案3】:根据 Rick Moss 的回答,我终于让它适用于 HTTP 和 HTTPS。除了我没有离开端点的 bindingConfiguration,因为它导致了无端点监听错误:
<configuration>
...
<system.serviceModel>
<services>
<service name="WcfService1.Service1">
<endpoint address="" name="ep1" binding="webHttpBinding" bindingConfiguration="ServiceBinding" contract="WcfService1.IService1" behaviorConfiguration="WebBehavior" />
<endpoint address="" name="ep1" binding="webHttpBinding" bindingConfiguration="ServiceBindingSSL" contract="WcfService1.IService1" behaviorConfiguration="WebBehavior" />
</service>
</services>
<bindings>
<webHttpBinding>
<binding name="ServiceBinding" ...>
<security mode="None">
</security>
</binding>
<binding name="ServiceBindingSSL" ...>
<security mode="Transport">
<transport clientCredentialType="None" />
</security>
</binding>
</webHttpBinding>
</bindings>
<behaviors>
<endpointBehaviors>
<behavior name="WebBehavior">
<webHttp />
</behavior>
</endpointBehaviors>
...
</behaviors>
<protocolMapping>
<add scheme="http" binding="basicHttpBinding" bindingConfiguration="ServiceBinding"/>
<add scheme="https" binding="basicHttpBinding" bindingConfiguration="ServiceBindingSSL"/>
</protocolMapping>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
</system.serviceModel>
...
</configuration>
编辑:这似乎只适用于 IIS 中的匿名身份验证。在 IIS 中同一网站下的不同 Web 服务中启用 Windows 身份验证导致:
Could not find a base address that matches scheme https for the endpoint with binding WebHttpBinding. Registered base address schemes are [http].
我讨厌 Web.config 文件。它们与 IIS 的耦合太紧密了
【讨论】:
以上是关于支持 HTTP 和 HTTPS Web.Config WCF 服务的主要内容,如果未能解决你的问题,请参考以下文章
支持 HTTP 和 HTTPS Web.Config WCF 服务
链接SpringBoot配置SSL同时支持http和https访问
哪些浏览器支持“//”而不是“http://”和“https://”? [复制]