我花了很多时间弄清楚如何配置我的WCF服务,以便他们在生产环境中工作。
基本上我需要这样做:
<behaviors> <serviceBehaviors> <behavior name="MyServiceBehavior"> <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" /> <serviceDebug includeExceptionDetailInFaults="true" /> </behavior> </serviceBehaviors> </behaviors> <serviceHostingEnvironment multipleSiteBindingsEnabled="true" aspNetCompatibilityEnabled="true" /> <services> <service name="MyNamespace.MyService" behaviorConfiguration="MyServiceBehavior"> <endpoint address="" bindingNamespace="https://secure.mydomain.com" binding="basicHttpBinding" bindingConfiguration="HttpsBinding" contract="MyNamespace.IMyService"/> </service> </services> <bindings> <basicHttpBinding> <binding name="HttpsBinding"> <security mode="Transport"> <transport clientCredentialType="None"></transport> </security> </binding> </basicHttpBinding> </bindings>
将bindingNamespace属性添加到端点是使其工作的最后一件事。
但是这个配置在我的本地开发环境中不工作,我正在常规的http下工作。所以我的配置有:
<behaviors> <serviceBehaviors> <behavior name="MyServiceBehavior"> <serviceMetadata httpGetEnabled="true" httpsGetEnabled="false" /> <serviceDebug includeExceptionDetailInFaults="true" /> </behavior> </serviceBehaviors> </behaviors> <serviceHostingEnvironment multipleSiteBindingsEnabled="true" aspNetCompatibilityEnabled="true" /> <services> <service name="MyNamespace.MyService" behaviorConfiguration="MyServiceBehavior"> <endpoint address="" binding="basicHttpBinding" contract="MyNamespace.IMyService"/> </service> </services>
这里的区别是我将httpsGetEnabled属性设置为false,我删除了bindingConfiguration和bindingNamespace。
问题是:如何创建一个处理BOTH的配置块?
我真的很讨厌每次发布时都要对配置进行大量的特殊修改。是的,我知道我可以有一个自动更改值的后期构建任务,但如果可能,我想合并配置。
我试过这样的东西:
<behaviors> <serviceBehaviors> <behavior name="MyServiceBehavior"> <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" /> <serviceDebug includeExceptionDetailInFaults="true" /> </behavior> </serviceBehaviors> </behaviors> <serviceHostingEnvironment multipleSiteBindingsEnabled="true" aspNetCompatibilityEnabled="true" /> <services> <service name="MyNamespace.MyService" behaviorConfiguration="MyServiceBehavior"> <endpoint address="" binding="basicHttpBinding" contract="MyNamespace.IMyService"/> <endpoint address="" bindingNamespace="https://secure.mydomain.com" binding="basicHttpBinding" bindingConfiguration="HttpsBinding" contract="MyNamespace.IMyService"/> </service> </services> <bindings> <basicHttpBinding> <binding name="HttpsBinding"> <security mode="Transport"> <transport clientCredentialType="None"></transport> </security> </binding> </basicHttpBinding> </bindings>
我认为,在启动服务时,将两个端点放在两个选项上。但是,这不行。我得到这个错误:
找不到与使用绑定BasicHttpBinding的端点匹配方案https的基址。注册基址方案是[http]。
从SO和其他互联网的角度来看,其他的人似乎都在杀死这条龙。
解决方法
那么你的组合配置有一个问题,就是你的两个端点在同一个地址上,这样就不行了。
如果您在IIS中托管,那么您的服务器,虚拟目录和* .svc文件将决定您的基本地址,它将是:
http://yourservername/VirtualDirectory/YourService.svc
如果您想要有两个端点,则至少有一个端点需要定义相对地址:
<services> <service name="MyNamespace.MyService" behaviorConfiguration="MyServiceBehavior"> <endpoint address="basic" binding="basicHttpBinding" contract="MyNamespace.IMyService"/> <endpoint address="secure" binding="basicHttpBinding" bindingConfiguration="HttpsBinding" contract="MyNamespace.IMyService"/> </service> </services>
在这种情况下,您的HTTP端点将在以下位置上:
http://yourservername/VirtualDirectory/YourService.svc/basic
和您的安全HTTPS端点:
https://yourservername/VirtualDirectory/YourService.svc/secure
此外:您的安全端点使用HttpsBinding配置 – 但是您缺少这样的绑定配置,您只需要:
<bindings> <basicHttpBinding> <binding name="HttpBinding"> <security mode="None"> <transport clientCredentialType="None"></transport> </security> </binding> </basicHttpBinding> </bindings>
您需要添加HttpsBinding配置!
<bindings> <basicHttpBinding> <binding name="HttpBinding"> <security mode="None"> <transport clientCredentialType="None"></transport> </security> </binding> <binding name="HttpsBinding"> <security mode="Transport"> <transport clientCredentialType="Windows" /> </security> </binding> </basicHttpBinding> </bindings>