开发者

How to make/simulate persistent TCP connection?

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?

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

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

CLIENT> Ping reply in 1s163ms //First connection

CLIENT> Ping reply in 22ms //Already开发者_运维知识库 connected

CLIENT> Ping reply in 26ms

CLIENT> Ping reply in 24ms

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

CLIENT> Ping reply in 19ms

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

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.

Update Modified 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:

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

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


I don't think Ping is a good tool to test a TCP connection. Because Ping uses ICMP as its underlying protocol instead of TCP.

I suggest you create a WCF client, connect to the service and then check the TCP connection via some network sniffering tools or simply use netstat for quick check.


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

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

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.

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜