【问题标题】:Can't add service reference for WCF service with netTcpBinding hosted in IIS无法使用 IIS 中托管的 netTcpBinding 添加 WCF 服务的服务引用
【发布时间】:2012-01-06 18:56:14
【问题描述】:

我对已部署到 IIS 的服务具有以下配置。我可以使用指向 wsHttpBinding 端点的 URL 为服务添加服务引用,但我无法使用指向 mexHttpBinding 端点的 URL 添加它(并且我想切换客户端以使用 netTcpBinding)

我得到的错误是:

下载“http://ServerName:98/MyService.svc/mex”时出错。

请求失败,HTTP 状态 400:错误请求。

元数据包含无法解析的引用:'http://ServerName:98/MyService.svc/mex'。

元数据包含无法解析的引用:'http://ServerName:98/MyService.svc/mex'。

<system.web>
  <compilation debug="true"
               targetFramework="4.0" />
  <customErrors mode="Off"/>
</system.web>

<system.serviceModel>
  <bindings>
    <netTcpBinding>
      <binding name="netTcpBinding_MyService" />
    </netTcpBinding>
    <wsHttpBinding>
      <binding name="wsHttpBinding_MyService"
               maxBufferPoolSize="2147483647"
               maxReceivedMessageSize="2147483647" />
    </wsHttpBinding>
  </bindings>
  <services>
    <service behaviorConfiguration="ServiceBehaviour"
             name="MyService.MyService">
      <endpoint address=""
                binding="wsHttpBinding"
                bindingConfiguration="wsHttpBinding_MyService"
                name="MyServiceEndpoint"
                contract="MyService.IMyService" />
      <endpoint address="net.tcp://ServerName:198/MyService.svc"
                binding="netTcpBinding"
                bindingConfiguration="netTcpBinding_MyService"
                name="MyServiceNetTcpEndpoint"
                contract="MyService.IMyService" />
      <endpoint address="http://ServerName:98/MyService.svc/mex"
                binding="mexHttpBinding"
                contract="IMetadataExchange" />
      <host>
        <baseAddresses>
          <add baseAddress="http://ServerName:98/" />
          <add baseAddress="net.tcp://ServerName:198/" />
        </baseAddresses>
      </host>
    </service>
  </services>
  <behaviors>
    <serviceBehaviors>
      <behavior name="ServiceBehaviour">
        <serviceMetadata httpGetEnabled="true"
                         policyVersion="Policy15" />
        <serviceDebug includeExceptionDetailInFaults="true" />
        <dataContractSerializer maxItemsInObjectGraph="2147483647" />
        <serviceCredentials>
          <windowsAuthentication includeWindowsGroups="true"
                                 allowAnonymousLogons="false" />
        </serviceCredentials>
      </behavior>
    </serviceBehaviors>
  </behaviors>
  <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
  <modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>

最奇怪的是,这个配置几乎是从我配置和部署的另一个服务中逐字复制的,并且我能够使用 mexHttpBinding 端点成功地将服务引用添加到客户端。原来服务的配置是这样的:

<system.web>
    <compilation debug="true"
                 targetFramework="4.0" />
    <customErrors mode="Off"/>
  </system.web>

  <system.serviceModel>
    <bindings>
      <netTcpBinding>
        <binding name="netTcpBinding_MyOtherService" />
      </netTcpBinding>
      <wsHttpBinding>
        <binding name="wsHttpBinding_MyOtherService"
                 maxBufferPoolSize="2147483647"
                 maxReceivedMessageSize="2147483647" />
      </wsHttpBinding>
    </bindings>
    <services>
      <service behaviorConfiguration="ServiceBehaviour"
               name="MyOtherService.MyOtherService">
        <endpoint address=""
                  binding="wsHttpBinding"
                  bindingConfiguration="wsHttpBinding_MyOtherService"
                  name="MyOtherServiceEndpoint"
                  contract="MyOtherService.IMyOtherService" />
        <endpoint address=""
                  binding="netTcpBinding"
                  bindingConfiguration="netTcpBinding_MyOtherService"
                  name="MyOtherServiceNetTcpEndpoint"
                  contract="MyOtherService.IMyOtherService" />
        <endpoint address="mex"
                  binding="mexHttpBinding"
                  bindingConfiguration=""
                  name="MyOtherServiceMexHttpBindingEndpoint"
                  contract="IMetadataExchange" />
        <host>
          <baseAddresses>
            <add baseAddress="http://ServerName:97/" />
            <add baseAddress="net.tcp://ServerName:970/" />
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="ServiceBehaviour">
          <serviceMetadata httpGetEnabled="true"
                           policyVersion="Policy15" />
          <serviceDebug includeExceptionDetailInFaults="true" />
          <serviceCredentials>
            <windowsAuthentication includeWindowsGroups="true"
                                   allowAnonymousLogons="false" />
          </serviceCredentials>
          <dataContractSerializer maxItemsInObjectGraph="2147483647" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>

您会注意到,配置中唯一相关的区别是 netTcpBinding 端点和 mexHttpBinding 端点的地址。最初,我开始时它们都是空的,但是当我一直在尝试让它工作时,我添加了明确的地址。

此外,两个服务在 IIS 中的配置相同,即在相关端口上启用和配置了 net.tcp 绑定。它们还各自在同一台服务器上运行单独的站点和单独的应用程序池(并且两个应用程序池都以相同的身份运行)。

任何帮助将不胜感激,谢谢。

【问题讨论】:

    标签: wcf iis nettcpbinding


    【解决方案1】:

    解决了。对于我们部署到的每个环境,我们都有一个 Web.{Environment}.config,并且由于 Web.config 上的文件锁定(可能没有从 TFS 中签出),在构建时没有通过 Web.config 复制正确的配置...

    其中一件事我真的应该先检查一下,但是当它以前一直在工作,然后突然突然坏掉而没有任何警告时,我从来没想过要检查。

    【讨论】:

      【解决方案2】:

      在 IIS 中使用服务时,您无法在端点配置中设置绝对 URL。根 URL 由 IIS 控制,它遵循网站(指定端口)/Web 应用程序/虚拟目录/SvcFile 的通用结构。您只能指定在 .svc 文件之后指定的地址的相对部分。

      设置完整的绝对地址和基地址仅适用于自托管方案。

      即使您使用 wsHttpBinding 端点添加服务引用,您仍然应该能够将其切换到 netTcpBinding 端点,因为应该在您的 WSDL 中描述该端点(如果您删除了那些绝对地址)。两个端点都应该在客户端的配置文件中进行配置。然后,您只需将端点配置的名称传递给服务存根(WCF 代理)的构造函数。

      【讨论】:

      • 正如我所说,我最初没有 netTcpBinding 的地址,而只是将“mex”作为 mexHttpBinding 的地址,但这不起作用。这是我在其他服务中使用的配置。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多