WCF 在 IIS 中使用 netTcpBinding

WCF use netTcpBinding in IIS

我按照这个博客 CONFIGURING WCF SERVICE WITH NETTCPBINDING 一步一步来,当我从 VS 2013 添加服务引用时,出现以下错误。

The URI prefix is not recognized.
Metadata contains a reference that cannot be resolved: 'net.tcp://localhost/WCFNetTcp/WCFNetTcpService.svc/mex'.
You have tried to create a channel to a service that does not support .Net Framing. It is possible that you are encountering an HTTP endpoint.
Expected record type 'PreambleAck', found '72'.  

我的服务配置文件如下:

  <system.serviceModel>
    <services>
      <service behaviorConfiguration="netTcpb" name="WCFNetTcp.WCFNetTcpService">
        <endpoint address="" binding="netTcpBinding" bindingConfiguration="portSharingBinding" contract="WCFNetTcp.IWCFNetTcpService" />
        <endpoint address="mex" binding="mexTcpBinding" bindingConfiguration="" contract="IMetadataExchange" />
        <!--<endpoint address="mexHttp" binding="mexHttpBinding" contract="IMetadataExchange" />-->
        <host>
          <baseAddresses>
            <!--<add baseAddress="net.tcp://localhost:6666/WCFNetTcpService/"/>-->
            <add baseAddress="net.tcp://localhost:808/WCFNetTcp/WCFNetTcpService.svc" />
            <!--<add baseAddress="http://local/WCFNetTcp"/>-->
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="netTcpb">
          <serviceMetadata httpGetEnabled="false"/>
        <serviceDebug includeExceptionDetailInFaults="true"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <bindings>
      <netTcpBinding>
        <binding name="portSharingBinding" portSharingEnabled="true"/>
      </netTcpBinding>
    </bindings>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true">
      <baseAddressPrefixFilters>
        <add prefix="net.tcp://localhost:808"/>
      </baseAddressPrefixFilters>
    </serviceHostingEnvironment>
  </system.serviceModel>

我尝试了所有我能想到的net.tcp URL,但都不起作用。当服务托管在 IIS 中时,基本地址似乎不起作用,这取决于 IIS 中的 url。如果我使用以下配置文件创建托管在控制台应用程序中的服务,它就可以工作。

<system.serviceModel>
  <behaviors>
    <serviceBehaviors>
      <behavior>
        <serviceMetadata httpGetEnabled="false"/>
      </behavior>
    </serviceBehaviors>
  </behaviors>
    <services>
        <service name="WCFNETTCP.Service1">
            <endpoint address="" binding="netTcpBinding" contract="WCFNETTCP.IService1">
            </endpoint>
            <!--comment out after you add service reference-->
            <endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange" />
            <host>
                <baseAddresses>
                    <add baseAddress="net.tcp://xxx:6666/WCFNETTCP/" />
                    <add baseAddress="http://xxx:6667/WCFNETTCIP"/>
                </baseAddresses>
            </host>
        </service>
    </services>
</system.serviceModel>

如果您能与我们分享如何使其在 IIS 下工作,我们将不胜感激。

我的 IIS 好像有问题。我通过以下步骤解决了这个问题。

  1. 重新安装 IIS
  2. 在 Windows 功能中重新安装 WCF 非 HTTP 激活
  3. 通过此命令在 iis 中重新注册 asp.net

C:\Windows\Microsoft.NET\Framework64\v4.0.30319>dism /online /enable-feature /featurename:IIS-ASPNET45

  1. 检查IIS中的设置,我这边没有变化
  2. 重新构建我的 WCF 服务,并成功添加服务引用。
    希望它对你有用。