WCF TCP clients - basic guidelines on how to use t

2020-05-27 04:00发布

问题:

I've got a WCF service and want to connect to it using a TCP binding. This is all well and good, but how are you supposed to handle the clients? I've noticed that if you create a new client for each call it doesn't re-use the channel and leaves around a bunch of TCP connections until they time out.

Is it normal use to create a client, call a method on it, then close it?

What if you want to re-use the connection? What are the limitations on that? Can you make simultaneous calls from different threads? If you can't, do you have to do your own connection pooling? And when re-using the connection, do you have to check the connection state before making calls and clean it up if it's faulted?

回答1:

Well this is a lot of questions together and the situation is really little bit complicated. When you create a client you can do it either via service reference and get class derived from ClientBase<ServiceContract> or you can use ChannelFactory<ServiceContract> and create channels manually (the former case uses the ChannelFactory internally).

How this relates to your question? Lets first look at real TCP connection. When you define NetTcpBinding you can set its MaxConnections property (default is 10). This property defines the number of pooled connection. It means that if you create client channel to the server and close the channel the connection is not terminated immediately. It remains opened in the pool until it is used by another opened client channel to the same server or until its idle timeout expires. You can open as many connections as the server allows you but only number defined by MaxConnections will be pooled once you close related client channels. Other connections will be terminated immediately. If you create CustomBinding you can use TCP transport directly where you can also control idle timeout (I think default is 2 minutes). Connections are pooled as long as the related ChannelFactory is not destroyed = use one ChannelFactory per application (ClientBase do it internally).

Now lets talk about channel itself because it is related to your other questions. WCF differs sessionfull and sessionless channels. TcpTransportChannel is sessionfull. It means that once you open the channel you create a session. Session means that all requests from the single client proxy are by default always served by the same service instance (per session instancing). But the instance is by default single threaded. It means that you can have multiple threads using the same proxy but the service will handle requests in a sequential order. If you want your service to process multiple request simultaneously you must mark it with [ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple]. Once you do it you are responsible for thread safe processing in the service (multiple threads accessing same service instance).

Sessionful channels have one big disadvantage. Any failure or exception on the service will break the channel and you will usually know about it after you try to use the channel again (exception saying that channel is in faulted state and cannot be used). You must always correctly handle these situations and you must correctly close channels/proxies once you don't want to use them or abort them once they are faulted. Faulted channel cannot be repaired - it must be aborted and you must create a new proxy/channel. I'm not sure if connections are returned to pool if you don't do that.

Reusing proxy/channel depends on the type of application you are building. I would definitely not reuse proxy/channel among multiple requests in web application but reusing is absolutely ok in WinForm or WPF application.

Edit:

Yes ClientBase uses ChannelFactory internally. The way how the ChannelFactory is used has changed over time. In .NET 3.0 the factory was created for each ClientBase instance. Since .NET 3.5 WCF uses internally MRU cache (Most recently used) which caches up to 32 lastly used factories. To take advantage of this caching you must use proxy constructor without parameters or with endpointConfigurationName and remoteAddress / EndpointAddress. You mustn't create endpoint in the code - these proxies doesn't use the cache. More about the topic is here.



回答2:

Are you closing your service proxies on the client side?

IService service = channelFactory.CreateChannel();

service.DoStuff();

((IContextChannel) service).Close();

This applies to all WCF clients, regardless of whether the binding is TCP or not.

For more information, see: http://msdn.microsoft.com/en-us/library/aa355056.aspx