如何使/模拟持久的TCP连接?持久、TCP

2023-09-03 16:28:59 作者:/

它看起来像WCF TCP连接不会持久。首先ping回复需要一段时间,但随后的过程花费的时间更少。过了一会儿,它需要较长再次-another重新连接?

It looks like WCF TCP connections are not persistent. First ping reply takes a while but subsequent processes take less time. After a while it takes long again -another re-connection?

服务器>入门上的net.tcp://0.0.0.0:999

SERVER> Started on net.tcp://0.0.0.0:999

客户端>连接://本地主机:999 //不是一个真正的连接,可以连接

CLIENT> Connection created to net.tcp://localhost:999 //Not a real connection, ready to connect

客户端>在1s163ms平回复//第一个连接

CLIENT> Ping reply in 1s163ms //First connection

客户端>在22ms内平回复//已经连接

CLIENT> Ping reply in 22ms //Already connected

客户端>在26ms平回复

CLIENT> Ping reply in 26ms

客户端>在24ms平回复

CLIENT> Ping reply in 24ms

客户端>在325ms平回复//重新连接

CLIENT> Ping reply in 325ms //Re-connected

客户端>在19ms平回复

CLIENT> Ping reply in 19ms

客户端>在767ms平回复//重新连接

CLIENT> Ping reply in 767ms //Re-connected

如果这是真的,什么是TCP连接的空闲时间值之前,将被断开?我需要保持连接活着。

If it's true, what is the idle time value for a tcp connection before it will be disconnected? I need to keep the connection alive.

更新修改code:

NetTcpBinding tcpBind = new NetTcpBinding();
tcpBind.ReliableSession.Enabled = true;
tcpBind.ReliableSession.Ordered = true;
tcpBind.ReliableSession.InactivityTimeout = TimeSpan.FromMinutes(10);

ServiceHost svh = new ServiceHost(typeof(ServiceImplementation));
svh.AddServiceEndpoint(
    typeof(WCFSimple.Contract.IService),
    //new NetTcpBinding(),
    tcpBind,
    String.Format("net.tcp://{0}:{1}", ip, port));
svh.Open();

现在我得到了另一个错误:

Now I got another error:

行动 http://tempuri.org/IService/Pong 不受此端点支持。只有WS-ReliableMessaging的2005年2月的消息是由这个端点处理。

The action http://tempuri.org/IService/Pong is not supported by this endpoint. Only WS-ReliableMessaging February 2005 messages are processed by this endpoint.

更新我只修改服务器端和它造成的错误。然后我修改客户端的TCP可靠消息。

Update I modified only server side and it caused the error. Then I modified client side's TCP as reliable messaging.

推荐答案

通常情况下,WCF连接是短暂的,虽然有一些连接池的推移在后台。

Normally, WCF connections are transient, though there is some connection pooling that goes on in the background.

使用的net.tcp作为传输,持久性(长期)的连接是可能的。在code您已经发布是实现这一目标的方法之一。

Using net.tcp as the transport, persistent (long term) connections are possible. The code you've posted is one way to achieve that.

有关详细信息,以及一些替代方案,请你需要知道的关于单向通话,回调和事件的,从2006年10月的MSDN杂志的文章。

For more information, and some alternatives, check out What You Need To Know About One-Way Calls, Callbacks, And Events, an MSDN magazine article from October 2006.

是 IsInitiating 和 OperationContract的属性。

Also of use are the IsInitiating and IsTerminating properties available on the OperationContract attribute.