【发布时间】:2012-08-22 11:02:45
【问题描述】:
我已经阅读了很多文章来解决这个问题,但我无法解决它。
场景 我有一个网站,它有一个 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>
【问题讨论】:
-
顺便问一下,你真的需要在你的服务器上配置主机头来以相同的名字访问它吗?唯一的区别是无域地址只能在同一域内的计算机上使用,并且域外的每个人都应该使用完整地址。如果可以,请尝试在 IIS 配置中不指定主机头。