如何将基于套接字的客户端与 WCF (net.tcp) 服务一起使用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/546004/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use socket based client with WCF (net.tcp) service?
提问by Burhan
I have developed a WCF service that uses the net.tcp adapter and listens to a specific port. I want to connect to that service using a normal .net client that uses sockets to send data to the port and listens to responses.
When I try to send data to this service, I get the error: "The existing connection was forcibly closed by remote host".
However, i am able to connect with the service by another client which uses the Address/Binding/Contracts of the WCF service.
Is there a way that enables me to communicate with a WCF service by using an ordinary socket based client?
我开发了一个 WCF 服务,它使用 net.tcp 适配器并侦听特定端口。我想使用普通的 .net 客户端连接到该服务,该客户端使用套接字将数据发送到端口并侦听响应。
当我尝试向该服务发送数据时,出现错误:“现有连接被远程主机强行关闭”。
但是,我能够通过另一个使用 WCF 服务的地址/绑定/合同的客户端连接该服务。
有没有办法让我使用基于普通套接字的客户端与 WCF 服务进行通信?
回答by EnocNRoll - AnandaGopal Pardue
The key decision is whether or not to make the WCF service conform to the socket client or whether to make the socket client conform to the WCF service.
关键是要让WCF服务符合socket客户端还是让socket客户端符合WCF服务。
It will be simplest to attempt to conform to the WCF service, rather than trying to implement something custom within WCF, which is never easy. At the bottom of the Other Resources section below you will see a link that describes the message inspection that is necessary in order to attempt to conform to a WCF service.
尝试符合 WCF 服务将是最简单的,而不是尝试在 WCF 中实现一些自定义的东西,这绝非易事。在下面其他资源部分的底部,您将看到一个链接,该链接描述了尝试符合 WCF 服务所必需的消息检查。
Having said that, .NET sockets do not natively communicate with WCF.
话虽如此,.NET 套接字本身并不与 WCF 通信。
Any attempt to do so will require custom programming on the WCF side of things.
任何这样做的尝试都需要在 WCF 方面进行自定义编程。
Whether you are using TcpClient or raw sockets in .NET to connect to and communicate with WCF does not matter. Any such interoperability must be handled with custom logic within WCF. Note that Net.Tcp is a custom transport protocol. It is not technically using TCP in the same way that the TcpClient is.
无论您是使用 TcpClient 还是 .NET 中的原始套接字连接到 WCF 并与之通信都无关紧要。任何此类互操作性都必须使用 WCF 中的自定义逻辑来处理。请注意,Net.Tcp 是自定义传输协议。它在技术上不像 TcpClient 那样使用 TCP。
For example, UDP is very commonly used by socket servers in the Linux world. WCF does not provide a built-in UDP transport. However, there is a UDP sample for WCF that implements UDP for WCF. Unfortunately, that sample does not illustrate communicating to and from a non-WCF UPD socket server.
例如,UDP 在 Linux 世界中被套接字服务器非常常用。WCF 不提供内置的 UDP 传输。但是,有一个 WCF 的 UDP 示例为 WCF 实现了 UDP。不幸的是,该示例没有说明与非 WCF UPD 套接字服务器之间的通信。
I have an outstanding question that is rather detailed where I explain my effort to get sample code to generically be testable for using UDP...
我有一个相当详细的悬而未决的问题,我在其中解释了我为使示例代码一般可测试以使用 UDP 所做的努力......
Is it possible to make the WcfTestClient work for custom transport channels?
是否可以使 WcfTestClient 用于自定义传输通道?
Nobody has answered my question yet. So, if you succeed in making this work, I am very interested. My case was driven by a desire for the WCF Service to be able to call out to a UDP socket server running on Linux without having to clutter my service with non-WCF coding. I don't want to mix approaches.
还没有人回答我的问题。所以,如果你成功地完成了这项工作,我很感兴趣。我的案例是因为希望 WCF 服务能够调用在 Linux 上运行的 UDP 套接字服务器,而不必用非 WCF 编码来干扰我的服务。我不想混合方法。
Other Resources...
其他资源...
Choosing a Transport
This article states that "The WCF TCP transport is optimized for the scenario where both ends of the communication are using WCF".Trying to connect a non-WCF client to a WCF service that uses the BasicHttpBinding.
The developer ends up writing custom code via the WebClient (as opposed to TcpClient).
http://social.msdn.microsoft.com/Forums/en-US/wcf/thread/c2d72c2d-c095-4ae1-b8ae-d15f32a4e0be/WCF vs. Raw .NET Sockets
The answer points out that TCP + binary serialization or UDP + binary serialization might be needed. There is a UDP binding sample, as I mentioned above.
http://social.msdn.microsoft.com/Forums/en-US/wcf/thread/c0520111-c1ca-4ffd-a4e0-ac68e86130ee/Writing Custom Requests to Simple WCF Services
The author explains how to figure out the format of the message that needs to be sent to the service, so that the TcpClient or other non-WCF sockets client can send the write information to the WCF service. The implication here is that you are not attempting to conform the WCF service, but rather you are forcing the socket client to do the heavy lifting. Even so, you not get the built-in advantages of the WCF bindings if you are expecting that.
http://blogs.msdn.com/carlosfigueira/archive/2008/01/13/writing-custom-requests-to-simple-wcf-services.aspx
选择传输
本文指出“WCF TCP 传输针对通信两端都使用 WCF 的场景进行了优化”。尝试将非 WCF 客户端连接到使用 BasicHttpBinding 的 WCF 服务。
开发人员最终通过 WebClient(而不是 TcpClient)编写自定义代码。
http://social.msdn.microsoft.com/Forums/en-US/wcf/thread/c2d72c2d-c095-4ae1-b8ae-d15f32a4e0be/WCF 与原始 .NET 套接字
答案指出可能需要 TCP + 二进制序列化或 UDP + 二进制序列化。正如我上面提到的,有一个 UDP 绑定示例。
http://social.msdn.microsoft.com/Forums/en-US/wcf/thread/c0520111-c1ca-4ffd-a4e0-ac68e86130ee/将自定义请求写入简单的 WCF 服务
作者解释了如何弄清楚需要发送到服务的消息的格式,以便 TcpClient 或其他非 WCF 套接字客户端可以将写入信息发送到 WCF 服务。这里的含义是您不是在尝试符合 WCF 服务,而是在强迫套接字客户端执行繁重的工作。即便如此,您也无法获得 WCF 绑定的内置优势(如果您期望的话)。
http://blogs.msdn.com/carlosfigueira/archive/2008/01/13/writing-custom-requests-to-simple-wcf-services.aspx
回答by tomasr
The Net.TCP binding uses a custom wire-level framing format that is not really documented, though Nicholas Allen started a series of blog posts on the topic recently. The series start here: http://blogs.msdn.com/drnick/archive/2009/01/19/message-framing-part-1.aspx
Net.TCP 绑定使用自定义的线级帧格式,该格式并未真正记录在案,尽管 Nicholas Allen 最近开始了有关该主题的一系列博客文章。该系列从这里开始:http: //blogs.msdn.com/drnick/archive/2009/01/19/message-framing-part-1.aspx
To be honest, Net.TCP is really, currently, more useful for WCF to WCF communication. if you want to interop with a custom TCP format that you need to handle, you're much better off either using raw sockets or creating your own custom WCF transport channel (which might not be trivial, btw)
老实说,Net.TCP 目前对于 WCF 到 WCF 的通信确实更有用。如果您想与需要处理的自定义 TCP 格式进行互操作,最好使用原始套接字或创建自己的自定义 WCF 传输通道(顺便说一句,这可能不是微不足道的)
回答by TomTom
Hy,
嗨,
did you enable the WCF Tracing? Because if you do and you get the following message: "The service does not allow you to log on anonymously."then it is (usually) a security setting problem.
您是否启用了 WCF 跟踪?因为如果你这样做了,你会收到以下消息:“该服务不允许你匿名登录。” 那么它(通常)是一个安全设置问题。
In this case disable the security mode for your binding:
在这种情况下,禁用绑定的安全模式:
<netTcpBinding>
<binding name="MyCustomBinding">
<security mode="None" />
</binding>
</netTcpBinding>
But better would be to work with certificates.
但更好的是使用证书。

