WCF instance management (Per call, Per session and Single)

1.Per Call instance mode

When we configure WCF service as per call, new service instances are created for every method call you make via WCF proxy client. Below image shows the same in a pictorial format:-

• WCF client makes first method call (method call 1).
• New WCF service instance is created on the server for this method call.
• WCF service serves the request and sends response and the WCF instance is destroyed and given to garbage collector for clean up.
• Now let’s say WCF client makes a second method call, again a new instance is created, request is served and the WCF instance is destroyed.

In other words for every WCF client method call one WCF service instance is created and destroyed once the request is served.

2.Per session Instance mode

Many times we would like to maintain state between method calls or for a particular session. For those kind of scenarios we will need to configure the service as per session. In per session only one instance of WCF service object is created for a session interaction. Below figure explains the same in a pictorial format.

• Client creates the proxy of WCF service and makes method calls.
• One WCF service instance is created which serves the method response.
• Client makes one more method call in the same session.
• The same WCF service instance serves the method call.
• When client finishes his activity the WCF instance is destroyed and served to garbage collector for clean up.

3.Single Instance mode

Many times we would like to create one global WCF instance for all WCF clients. To create one single instance of WCF service we need to configure the WCF service as ‘Single’ instance mode. Below is a simple pictorial notation of how single instance mode will operate:-

• WCF client 1 requests a method call on WCF service.
• WCF service instance is created and the request is served. WCF service instance is not destroyed the service instance is persisted to server other requests.
• Now let’s say some other WCF client i.e. client 2 requests a method call.
• The same WCF instance which was created by WCF client 1 is used to serve the request of WCF client 2. In other words only one global WCF server service instance is created to serve all client requests.

For example

In IService :

[ServiceContract]
public interface IService1
{
[OperationContract]
int counter();

}

In Service Class:

 [ServiceBehavior(InstanceContextMode= InstanceContextMode.PerCall)]
public class Service1 : IService1
{
int count;
public int counter()
{
count++;
return count;
}
}

At Client on button_Click event :

private void button1_Click(object sender, EventArgs e)
{
ServiceReference1.Service1Client obj = new WindowsFormsClient.ServiceReference1.Service1Client();
MessageBox.Show(obj.counter().ToString());
MessageBox.Show(obj.counter().ToString());
}

refer link : http://www.codeproject.com/KB/WCF/WCFInstance.aspx

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s