WCF service crashes after several calls (approx 30 calls).

I was using the WCF client to call a WCF service. After approximately 30 calls the host would stop responding, and then give a timeout error. The website associated with the host service would also become non-responsive for several minutes. It would come back online after several minutes and then my client calls would work for approximately 30 more calls, then the application would die again with timeout errors from the host.

 

The problem was that I needed to close the ServiceModel.ClientBase channel explicitly. I also made sure to use Try{}Catch{} with an Abort() call in the catch in case the code failed.

The code that caused errors looked like this:

WCFSomeAPI.SomeAPIClient client = new WCFSomeAPI.SomeAPIClient(“WSHttpBinding_SomeAPI”);
string cd = client.GetSomeInformation(sID);

 

 

The corrected code looks like this:

 

WCFSomeAPI.SomeAPIClient client = new WCFSomeAPI.SomeAPIClient(“WSHttpBinding_SomeAPI”);

try

{
string cd = client.GetSomeInformation(sID);

client.Close();

… (do something)

}

catch

{

client.Abort();
}

 

This seems simple an obvious, but I was under the impression that GC was taking care of cleaning up my resources on this, which was not the case, and I was either causing too many open connections or memory leaks (watching process didn’t really make me think this was a memory issue on my client side, but the article I read about this said this was a memory leak issue, but it appeared more like I was killing the host with memory leaks or open connections (similar to DOS attack)).

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s