7

Making a client/server style app using WCF, but I can't find any documentation that explains if the NetTcpBinding provides a persistent connection?

I would like my client to connect and remain connected to the server for weeks at a time. (Yes I know I need to handle disconnects etc..)

Does the NetTcpBinding allow for a long connection such as this? If so is there anything I need to specify or is this default behaviour?

Kelly
  • 6,992
  • 12
  • 59
  • 76
  • It may depend on several things, amongst others how you're hosting the service and managing the lifetime of your client proxy. Even then, I'm not sure if WCF will keep the client and server connection continuously open, but *why would you care*? *Why* do you want WCF to keep the underlying connection open for weeks? Are you sure you're not [asking about Y](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem)? – Jeroen Oct 20 '12 at 07:34
  • The server needs to keep each client connected as speed is a critical requirement of the system I am building. When a message is sent from the server it needs to arrive at each client as quickly as possible. The time taken to make a connection would introduce an unacceptable level of delay. – Kelly Oct 21 '12 at 17:32
  • @Kelly How about pinging the WCF service in a frequent interval from the client? By this way you can maintain a long duration connection with the service http://stackoverflow.com/questions/1085220/pinging-wcf-services. From my knowledge only the first call takes more time because of the creation of channel factories and may be other components but the subsequent calls (channel factory will be cached) will be usually faster and you are using net.tcp which will be of course faster. Do you really done some measurement? – VJAI Oct 22 '12 at 15:02
  • @Kelly Could you explain little more about your service? – VJAI Oct 22 '12 at 15:04
  • The best description of the service is a pub/sub style model. Programs publish to the service and clients connected to it can subscribe to get the information. It does more then this but this is the heart of it. So I would hate for a subscriber to disconnect only because they are are listening to a non-liquid topic. Currently using RabbitMQ for the pub/sub but planning on moving away from that and would like a custom solution as it's currently too slow. (Yes I know ZeroMQ is faster but still want custom so I can control it better.) – Kelly Nov 28 '12 at 23:10
  • @Mark, that is basically what I've got working. Both the client and server ping each on a predetermined interval, the interval is reset if they contact earlier as the ping is then not necessary. – Kelly Feb 16 '13 at 20:47

1 Answers1

7

As long as you keep the service host and client proxy alive and opened, the underlying connection in WCF should remain opened as well. Even below that, it appears that the binding uses connections from the TCP connection pool, as found on the NetTcpBinding MSDN page:

The NetTcpBinding uses TCP connection pooling based on the service’s host DNS name and the port number the service is listening on.

I'm not an expert on how TCP connection pooling works, you may want to try a sister site such as ServerFault for more details on this.

If you want control over all this, then the only property I can see that can be tweaked for the out-of-the-box NetTcpBinding is MaxConnections. However, if you're really willing to dive deep you can also build your own Custom Binding with a TcpTransportBindingElement, that gives you even more fine-grained control on the TcpConnectionPoolSettings.

In any case, if I were to venture a guess I'd think building the service host and client proxy will take far more time (relatively) than any time spent getting a connection from the pool, so keeping those two opened may be enough for your requirements.

The reason I asked in a comment why you want the connection to remain open is because that can hardly be a requirement in itself. Speed and response times are a real requirement though, so the best advice is probably to make those requirements explicit (quantify / qualify your need for speed) and run some tests to determine what's best/needed for your situation.

Community
  • 1
  • 1
Jeroen
  • 60,696
  • 40
  • 206
  • 339