我已经阅读了很多文章来解决这个问题,但我无法解决它。
场景 我有一个网站,它有一个 WCF 服务和 2 个主机头
- 自旋本地主机
- spin-localhost.worldwide.co.uk(更改为 spin-localhost2)
但是使用相同的端口44001我可以访问 spin-localhost 服务,但无法访问 spin-localhost.worldwide.co.uk(现在是 spin-localhost2)。
编码
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="Bbc.Ww.SalesProjections.UI.Services.CilsDataReceiveBehavior">
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<bindings>
<basicHttpBinding>
<binding name="WindowsAuth" maxReceivedMessageSize="2097152">
<security mode="TransportCredentialOnly">
<transport clientCredentialType="Windows" />
</security>
</binding>
</basicHttpBinding>
</bindings>
<services>
<service behaviorConfiguration="Bbc.Ww.SalesProjections.UI.Services.CilsDataReceiveBehavior" name="Bbc.Ww.SalesProjections.UI.Services.CilsDataReceive">
<endpoint address="http://spin-localhost:44001/Services/CilsDataReceive.svc" binding="basicHttpBinding" name="BasicHttpEndpoint" bindingConfiguration="WindowsAuth" contract="Bbc.Ww.SalesProjections.UI.Services.ICilsDataReceive" />
<endpoint address="http://spin-localhost.worldwide.co.uk:44001/Services/CilsDataReceive.svc" binding="basicHttpBinding" name="BasicHttpEndpoint" bindingConfiguration="WindowsAuth" contract="Bbc.Ww.SalesProjections.UI.Services.ICilsDataReceive" />
</service>
</services>
<serviceHostingEnvironment>
<baseAddressPrefixFilters>
<add prefix="http://spin-localhost:44001" />
</baseAddressPrefixFilters>
</serviceHostingEnvironment>
</system.serviceModel>
上述解决方案失败了我在工厂尝试过
public class CustomHostFactory : ServiceHostFactory
{
protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
{
return new ServiceHost(serviceType, baseAddresses[0]);
}
}
编辑:我使用的是 /Net 3.5 而不是 4。
参考资料 http://www.sieena.com/blog/archive/2011/02/01/wcf-service-in-iis-with-multiple-host-headers.aspx
http://blogs.msdn.com/b/rampo/archive/2007/06/15/supporting-multiple-iis-bindings-per-site.aspx
http://social.msdn.microsoft.com/forums/en-US/wcf/thread/9e248455-1c4d-4c5c-851c-79d9c1631e21/
还有很多....
更新 1:经过进一步测试,worldwide.bbc.co.uk 似乎存在错误,所以我更改了名称并绑定到 spin-localhost2。
<endpoint address="http://spin-localhost2:44001/Services/CilsDataReceive.svc" binding="basicHttpBinding" name="BasicHttpEndpoint" bindingConfiguration="WindowsAuth" contract="Bbc.Ww.SalesProjections.UI.Services.ICilsDataReceive" />
我可以使用 spin-localhost 访问第一个服务,但 spin-localhost2 给出 401、401 和 400(未找到)。用 Fiddler 查了一下。
解决方案(尚未测试):
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="Bbc.Ww.SalesProjections.UI.Services.CilsDataReceiveBehavior">
<!-- <serviceMetadata httpGetEnabled="true" />-->
<serviceDebug includeExceptionDetailInFaults="true"/>
<serviceCredentials>
<userNameAuthentication userNamePasswordValidationMode="Windows"/>
</serviceCredentials>
</behavior>
</serviceBehaviors>
</behaviors>
<bindings>
<basicHttpBinding>
<binding name="WindowsAuth" maxReceivedMessageSize="2097152">
<security mode="TransportCredentialOnly">
<transport clientCredentialType="Windows"/>
</security>
</binding>
</basicHttpBinding>
</bindings>
<services>
<service behaviorConfiguration="Bbc.Ww.SalesProjections.UI.Services.CilsDataReceiveBehavior" name="Bbc.Ww.SalesProjections.UI.Services.CilsDataReceive">
<endpoint address="" binding="basicHttpBinding" name="BasicHttpEndpoint" bindingConfiguration="WindowsAuth" contract="Bbc.Ww.SalesProjections.UI.Services.ICilsDataReceive"/>
</service>
</services>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>