简体   繁体   English

如何使用基于套接字的客户端与WCF(net.tcp)服务?

[英]How to use socket based client with WCF (net.tcp) service?

I have developed a WCF service that uses the net.tcp adapter and listens to a specific port. 我开发了一个使用net.tcp适配器并侦听特定端口的WCF服务。 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. 我想使用普通的.net客户端连接到该服务,该客户端使用套接字将数据发送到端口并侦听响应。
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. 但是,我能够通过另一个使用WCF服务的地址/绑定/合同的客户端连接该服务。
Is there a way that enables me to communicate with a WCF service by using an ordinary socket based client? 有没有一种方法可以让我通过使用普通的基于套接字的客户端与WCF服务进行通信?

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服务符合套接字客户端,或者是否使套接字客户端符合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. 尝试符合WCF服务最简单,而不是尝试在WCF中实现自定义内容,这绝非易事。 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服务所必需的消息检查。

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. 无论您是使用.NET中的TcpClient还是原始套接字连接到WCF并与之通信都无关紧要。 Any such interoperability must be handled with custom logic within WCF. 必须使用WCF中的自定义逻辑处理任何此类互操作性。 Note that Net.Tcp is a custom transport protocol. 请注意,Net.Tcp是一种自定义传输协议。 It is not technically using TCP in the same way that the TcpClient is. 从技术上讲,TCP的使用方式与TcpClient相同。

For example, UDP is very commonly used by socket servers in the Linux world. 例如,UDP在Linux世界中的套接字服务器中非常常用。 WCF does not provide a built-in UDP transport. WCF不提供内置UDP传输。 However, there is a UDP sample for WCF that implements UDP for WCF. 但是,WCF有一个UDP示例,它为WCF实现UDP。 Unfortunately, that sample does not illustrate communicating to and from a non-WCF UPD socket server. 遗憾的是,该示例未说明与非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. 我的情况是由希望WCF服务能够调用在Linux上运行的UDP套接字服务器而不必使用非WCF编码混乱我的服务。 I don't want to mix approaches. 我不想混合方法。

Other Resources... 其他资源......

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. Net.TCP绑定使用的是一种自定义的线级框架格式,虽然Nicholas Allen最近在该主题上发布了一系列关于该主题的博客文章。 The series start here: http://blogs.msdn.com/drnick/archive/2009/01/19/message-framing-part-1.aspx 该系列从这里开始: 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. 说实话,Net.TCP目前对WCF到WCF通信更有用。 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) 如果你想与你需要处理的自定义TCP格式互操作,你最好使用原始套接字或创建自己的自定义WCF传输通道(这可能不是微不足道,顺便说一下)

Hy, HY,

did you enable the WCF Tracing? 你启用了WCF跟踪吗? 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. 那么它(通常)是一个安全设置问题。

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. 但更好的方法是使用证书。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM