Wednesday, June 10, 2009

How to count the number of active session in WCF

There are three ways we can manage instance of WCF service. In other words, WCF supports three types of instance activation. These three types of activation are:
1> Per-call session – It allocates a new service instance per client request.
2> Sessionful session – It allocates a new service instance per client connection.
3> Singleton session – All clients share the same service instance.

Here, our goal is to count the number of active WCF service instance and set the limit of maximum concurrent client when the activation mode is Sessionful.
Let’s create a WCF Service. We will start by creating a WCF Contact.

ServiceContract(SessionMode=SessionMode.Required)]
public interface ISessionCount
{
[OperationContract]
string GetData(string s);
[OperationContract]
bool StartingService();
[OperationContract]
bool StopingService();
}
Here, the above code snippet is a WCF Contact where the SessionMode is Required. It means the client has to use the session mode.

[ServiceBehavior(InstanceContextMode=InstanceContextMode.PerSession)]
public class SessionCount : ISessionCount, IDisposable
{
//static int Counter;
static object Counter = new object();
static SessionCount()
{
Counter = 0;
}
public SessionCount()
{

}

public string GetData(string s)
{
return "This is from Service :" + s;
}

public bool StartingService()
{

lock (Counter)
{
int localCount = Convert.ToInt32(Counter);
Counter = localCount + 1;
if (localCount > 2)
{
this.Dispose();
return false;
}

}
return true;
}

public bool StopingService()
{
return true;
}

#region IDisposable Members

public void Dispose()
{
lock (Counter)
{
Counter = Convert.ToInt32(Counter) - 1;
}
}

#endregion
}

The above code snippet is the implementation of the service contact. Let’s examine the implementation point by point.

1> Implement the IDisposable Interface. When the service instance will be disposed, the Dispose method will be fired.
2> Declare a static Counter object. Instead of primitive type(Int, Long), I am using Object here because I have to lock the object before accessing this. We have to lock the object because of thread related issue.
3> Initialize the counter object in the type constructor. Type constructor is fired once in the application domain regardless of the number of new instance created.
4> Assume the maximum concurrent client of the WCF service can be 2.
5> When a client will create a proxy of the WCF service, it will call the start Service.
6> In the start service, we will increase the counter and check whether it exceeds the maximum concurrent user limet or not. If it exceeds then dispose the current instance return false.
7> When the client will be shutdown, it will call the stop service method which eventually will dispose the service instacne. However, if the client close the proxy, the service instace will be disposed. The responsibility of the dispose method is to decrease the Counter.

This is straight forward way we can protect the maximum concurrent client in WCF service.

No comments: