【问题标题】:WCF multiple host header with 'same port' causing error具有“相同端口”的 WCF 多主机标头导致错误
【发布时间】:2012-08-22 11:02:45
【问题描述】:

我已经阅读了很多文章来解决这个问题,但我无法解决它。

场景 我有一个网站,它有一个 WCF 服务和 2 个主机头

  1. 自旋本地主机
  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/2008/02/11/how-can-wcf-support-multiple-iis-binding-specified-per-site.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 配置中不指定主机头。

标签: c# asp.net wcf


【解决方案1】:

在 vesion 4 下带有 .NET 的 WCF 不支持多主机头 IIS 托管,对此有一些解决方法,具体取决于您的 .NET 版本,这些方法或多或少有些复杂。

.NET 4.0 中的 WCF 支持这一点,作为通过在配置中使用 &lt;serviceHostingEnvironment multipleSiteBindingsEnabled="true" /&gt; 启用的可选功能。

那里的一切都得到了彻底的解释:http://blogs.msdn.com/b/rampo/archive/2008/02/11/how-can-wcf-support-multiple-iis-binding-specified-per-site.aspx

【讨论】:

  • 就像我在对问题的评论中所说的:你真的需要为此配置特定的主机头吗?
  • 如果我要使用 .NET 4 和您声明的属性,是否需要端点属性?
  • 很好,我已经将我的项目升级到 .Net 4.0,所以问题已解决:) 谢谢
【解决方案2】:

带有 IIS 的 WCF 将支持具有 不同 相对地址作为 shown in this blog post 的多个端点,但我不认为 IIS 将支持同一端口和同一网站/应用程序中的不同主机标头。尝试创建单独的 IIS 网站,每个主机标头一个,并为每个服务使用单独的 serviceModel 配置。这样您就可以为您创建的服务使用相同的端口。

【讨论】:

  • IIS 确实支持同一端口上的不同主机头。 microsoft.com/technet/prodtechnol/WindowsServer2003/Library/IIS/… .NET 4.0 有解决方案,但不幸的是我在 .Net 3.5
  • 虽然 IIS 能够为网站内的每个唯一 IP 地址分配主机,但 WCF 不支持此配置。看看this MSDN article。每个 IIS IP 地址分配等于一个单独的基地址,但帖子指出:“您也可以为一个服务设置多个基地址,但每个服务的每个传输只允许一个基地址。”
猜你喜欢
  • 2011-08-29
  • 1970-01-01
  • 2010-09-30
  • 2019-08-03
  • 1970-01-01
  • 1970-01-01
  • 2019-09-16
  • 1970-01-01
  • 2017-11-08
相关资源
最近更新 更多