.net - 使用 netTCP 协议(protocol)实现 WCF

标签 .net wcf tcp

在成功让 WCF 服务和客户端使用 Http 相互通信后,我现在正尝试将其切换到 netTcp,以便我可以加密通信。但是,我无法根据该协议(protocol)建立连接。

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

The HTML document does not contain Web service discovery information. Metadata contains a reference that cannot be resolved: 'http://localhost:31284/MyService.svc'. Content Type application/soap+xml; charset=utf-8 was not supported by service http://localhost:31284/MyService.svc. The client and service bindings may be mismatched. The remote server returned an error: (415) Unsupported Media Type. If the service is defined in the current solution, try building the solution and adding the service reference again.

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

Metadata contains a reference that cannot be resolved: 'net.tcp://localhost:8732/Design_Time_Addresses/NameSpace.ApiServices.Server/MyService'. Could not connect to net.tcp://localhost:8732/Design_Time_Addresses/NameSpace.ApiServices.Server/MyService. The connection attempt lasted for a time span of 00:00:02.0142014. TCP error code 10061: No connection could be made because the target machine actively refused it 127.0.0.1:8732. No connection could be made because the target machine actively refused it 127.0.0.1:8732 If the service is defined in the current solution, try building the solution and adding the service reference again.

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

<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>

最佳答案

好的,如果对其他人有帮助的话,终于深入了解了。 serviceMetdata 标记需要将 httpGetEnabled 属性设置为 false:

 <serviceMetadata httpGetEnabled="false" />

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

关于.net - 使用 netTCP 协议(protocol)实现 WCF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5076534/

相关文章:

.net - CE 5.0 Compact Framework .NET 上的 DataGrid 是否支持编辑?

c# - 打印多张图像

.net - 在 Windows 窗体中添加和删除控件和内存使用

windows - 远程 IP 地址是否可以在 Windows 下的传入 TCP session 中被欺骗?

sockets - TCP套接字的传输时间

c# - 如何连接到 .NET 框架内的 MySQL 数据库?

c# - WCF 中出现此异常的原因

c# - WCF 服务客户端配置生成错误

c# - 神秘 => 管道读取错误 : The pipe has been ended. (109, 0x6d)

networking - 有哪些不使用 TCP 或 UDP 等端口的基于 IP 的协议(protocol)?