Connections to WCF service

Last week I encountered a problem while connecting to a WCF service (more accurate a WWF in a WCF). The error message was : “Client is unable to finish the security negotiation within the configured timeout (00:01:00).  The current negotiation leg is 1 (00:01:00).” A timeout at the security negotiation, why? I have a lot of programs with connections to WCF and I never had this error message before. The reason is the number of connections opened and closed in a certain period of time.

 

Explanation : my program create 20 threads, each thread create a connection to the WCF service, open, do the call and close the connection properly. Why this error message? It’s a windows reason, a socket connection in windows remain not accessible when closed for a period of time, the default is 4 minutes and can be changed by a registry value. The connection is in a pool, and we must ask a connection from the same pool to be able to retrieve the same connection and that’s the problem, each time I create a new connection from a different pool and I reach the maximum allowed sockect connection.

 

How to resolve the problem?

It’s not so difficult, Difficult in WWF.
The pool of connection is created by the ChannelFactory class, so if you create a connection always from the same ChannelFactory, you will use the pool. So don’t use the statis method CreateChannel because you’ll always create a new pool.

 

WCF reference added via “Add service reference” in visual studio (svcutil.exe)

If you add the reference, the generated proxy’ll use the pool.

 

If you create your own proxy

Create a ChannelFactory object, and create the channel always from this object, it’ll use the same socket pool. Don’t call the static method from the ChannelFactory class.

 

From a WWF (worflow)

It’s more difficult because you don’t create the code to call a WCF (you can), you normally use a send activity, by default it doesn’t use the pool. If you want to use the pool, you need to use ChannelManagerService to say on which end point you want to use pooling. If you want to read more about, read the following post about Serge Lucas, it is the detailed solution for this problem.







4 comments:
gravatar
Unknown said...
September 7, 2009 at 1:49 PM  

Hi,

I have a host in a windows service like this :

ServiceHost sTransmissionServiceHost;
protected override void OnStart(string[] args)
{
var serviceType = typeof(Transmission_Wcf.TransmissionService);
sTransmissionServiceHost = new ServiceHost(serviceType);
sTransmissionServiceHost.Open();
}

And in another project, I add a service reference that I use like this :

public sealed class Instance
{
public static readonly wcfTransmission.TransmissionServiceClient Client = new wcfTransmission.TransmissionServiceClient();
}


In my code : Instance.Client.Action();

each time that I call this Method, the TCP connection pool grow.

Do you have a solution ?

Thanls for your article

gravatar
Coolweb said...
September 11, 2009 at 8:52 PM  

ok, first sorry for the time that I took to reply but I was in holiday :-). You say that the connection pool grows, it's normal a connection pool will allow to grow to a certain number of connection, I don't know how much but it's a pool, so more than one and it's windows that decides how many connections can be in the pool and when it will reach this maximum, it will reuse a connection from that pool. This is a problem if you have a timeout error message, in this case it says that you use always a new pool without reusing an existing connection. But be aware, if you do a sort of "attack" by opening eg 1000 connections to the same pool, you will also have an error message, it's for security reason. You must write code to do normal connection, I will try to search how many connections a pool in windows can contains. I hope that the response help you...

gravatar
Ravi Kumar said...
January 15, 2014 at 7:29 PM  

nice 1. I found this solution have a look at http://www.etechpulse.com/2013/09/client-is-unable-to-finish-security.html

Thanks

gravatar
Happy Feet said...
June 8, 2018 at 4:42 AM  

I'm having the same issue, Could you share which registry key need to be updated?

Post a Comment