使用netTCP协议实现WCF

时间:2011-02-22 09:48:39

标签: .net wcf tcp

成功获得WCF服务和客户端使用Http相互通信后,我现在正试图将其切换到netTcp,以便我可以加密通信。但是,我无法在此协议下建立连接。

更改了服务上的App.config文件并删除了客户端项目中的服务引用后,我尝试重新添加服务引用。点击"发现"找到服务但是在地址为http://localhost:31284/MyService.svc的http上,这不是我在任何地方指定的地址:我认为它必须是默认地址。如果我尝试添加此服务,我会收到错误:

  

HTML文档不包含Web   服务发现信息。   元数据包含一个引用   无法解决:   ' HTTP://本地主机:31284 / MyService.svc&#39 ;.   内容类型application / soap + xml;   charset = utf-8不受支持   服务   http://localhost:31284/MyService.svc。   客户端和服务绑定可能是   不匹配。远程服务器返回   错误:(415)不支持的媒体   类型。如果服务是在。中定义的   目前的解决方案,尝试建立   解决方案并添加服务   再次参考。

在我的app config中,我已将端点地址指定为net.tcp:// localhost:8732 / Design_Time_Addresses / NameSpace.ApiServices.Server / MyService。如果我将此地址放入服务引用框并尝试连接到它或尝试使用WCF测试客户端进行连接,它也会失败。两者都引发了非常相似的错误:

  

元数据包含一个引用   无法解决:   '的net.tcp://本地主机:8732 / Design_Time_Addresses / NameSpace.ApiServices.Server /为MyService&#39 ;.   无法连接到   的net.tcp://本地主机:8732 / Design_Time_Addresses / NameSpace.ApiServices.Server /为MyService。   连接尝试持续了一段时间   时间跨度00:00:02.0142014。 TCP   错误代码10061:没有连接可以   是因为目标机器   积极拒绝它127.0.0.1:8732。   无法建立联系,因为   目标机器积极拒绝它   127.0.0.1:8732如果在当前解决方案中定义了服务,请尝试   构建解决方案并添加   服务参考。

不确定发生了什么。两者都在同一台机器上运行,我的防火墙已关闭。我检查了netstat -a并且无法在端口8732上看到TCP侦听,但我已经在我的配置中打开并启用了端口共享服务。看起来像这样:

<bindings>
  <netTcpBinding>
    <binding portSharingEnabled="True" name="tcpBinding" closeTimeout="00:10:00" openTimeout="00:10:00" sendTimeout="00:10:00" maxBufferSize="2147483647" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647">
      <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
      <security>
        <transport>
          <extendedProtectionPolicy policyEnforcement="Never" />
        </transport>
      </security>
    </binding>
  </netTcpBinding>
</bindings>

<behaviors>
  <serviceBehaviors>
    <behavior name="standard">
      <serviceDebug includeExceptionDetailInFaults="false" />
      <serviceMetadata />
    </behavior>
  </serviceBehaviors>
</behaviors>

<services>
  <service name="NameSpace.ApiServices.Server.MyService" behaviorConfiguration="standard">
    <endpoint address="" binding="netTcpBinding" bindingConfiguration="tcpBinding" contract="NameSpace.ApiServices.Server.IMyService">
      <identity>
        <dns value="localhost" />
      </identity>
    </endpoint>
    <endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange" />
    <host>
      <baseAddresses>
        <add baseAddress="net.tcp://localhost:8732/Design_Time_Addresses/NameSpace.ApiServices.Server/MyService/" />
      </baseAddresses>
    </host>
  </service>
</services>

任何人都可以看到我做错了吗?

编辑:我被问到我的客户端配置:

    <bindings>
      <netTcpBinding>
        <binding name="tcpBinding" closeTimeout="00:10:00" openTimeout="00:10:00" sendTimeout="00:10:00" maxBufferSize="2147483647" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647">
          <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
          <security>
            <transport>
              <extendedProtectionPolicy policyEnforcement="Never" />
            </transport>
          </security>
        </binding>
      </netTcpBinding>
    </bindings>
    <client>
        <endpoint address=""
            binding="netTcpBinding" bindingConfiguration="tcpBinding"
            contract="NameSpace.Server.IUpdateCustomers" name="tcpBinding" />
    </client>
</system.serviceModel>

2 个答案:

答案 0 :(得分:1)

好吧,如果能帮助其他人,最后得到它的底部。 serviceMetdata标记需要将httpGetEnabled属性设置为false:

 <serviceMetadata httpGetEnabled="false" />

否则,我认为,它试图在Http和Tcp上启动端点并失败。

答案 1 :(得分:0)

在服务器中创建tcp端点后,您还必须确保在客户端更新引用。我要检查的第一件事是端点是否与服务器和客户端兼容。检查客户端的app.config,并检查tcp端点是否也在这里正确设置,而不仅仅是旧的http端点。

另外,我认为TCP的mex端点无法在与服务相同的端口上运行,因此请尝试在主服务端点上的另一个tcp端口上公开mex端点。