WCF in an ASP.Net Application

I posted a question on Stackoverflow.com today because I am experiancing some issues in production with a web application that is using WCF. To make a long story short, see this post.

My solution was actually quite simple and easy for me to implement. I can’t post actual code here since it’s a production project and I didn’t have time to write up a proof of concept. This is the short of it, however:

My ServiceClient object was already wrapped up in a another class where the client was created and disposed off properly. Each function call also had a prefunction and postfunction call. For example:

public string GetData()
{
   Open();
   string ret = GetData();
   Close();
   return ret;
}

Open and Close do not actually open and close the service client. Some of my functions require back to back calls so I didn’t want to be closing and opening connections repeatedly. I put these functions in, however, so that I could insert code before and after data calls, just in case I needed it. They were both empty until today. I have about 50 function calls in this WCF service, so it was a good thing I put those function calls in ahead of time!

I added 2 member variables to my class. A busy bool variable and a last used datetime. In the open function I now set busy to true and in the close I reset it back to false and also update the last used time. I then created some member properties to get how long it’s been idle (since last used time), if it’s busy, and what the communication state is (in cause of fault, but I also have fault checking inserted into Open, but let’s not go there right now).

I then created a connection pool class that simply holds a generic list of these client classes. Initially on application start, this list is blank. I created a function to get the next free client. This simply goes through this generic list and checks the communication state (has to be opened) and if it’s busy. If it’s open and not busy, I just return that client, otherwise I create a new one, add it to the pool and then return it. I also added some functions to get the state of each client and idle time, etc. For cleanup purposes, I just added a timer that executes every minute and checks to see if the idle time is over 5 minutes. If it is, it disposes that client and removes it from the pool.

My application always calls one single function to get the data client, so inserting the ability to get it from the pool was trivial. I just changed that function to get the pool object out of the Application state and then called the get next free client function and returned that.

All in all, this took less than 20 minutes to build and test. I installed it on the client’s production server since it was just crawling and a crash would be better than it’s current state anyway. I’m happy to report that things are moving quite well now. I would even venture to say that it’s running faster now than it did when I had web services set up instead of WCF.

If anyone sees a problem with this set up, or has a better way to do it, or just wants to tell me I’m dumb, go for it. I’m completely new to WCF and I will always take helpful advice.