What is PerSession Behavior of WCF Service

This article explains what is PerSession Behavior in WCF Services, how to configure and implement PerSession behavior and understand the difference between PerCall and PerSession WCF services

What happens when the WCF service configured as Per Session?

When client creates a new proxy of WCF service configured as InstanceContextMode=InstanceContextMode.PerSession, the WCF service creates a dedicated instance for a particular client proxy and handles all subsequent calls through this instance until the client closes the proxy.

Each private session uniquely binds a proxy to the particular instance. Note that the client session has one service instance per proxy. If the client creates another proxy to the same or a different service endpoint, then the new proxy will have a different dedicated instance.

As each instance maintains a session in service memory, this behavior creates scalability and transaction issues like the classic client-server model. With normal infrastructure, it is difficult to handle more than a dozen ongoing calls due to the cost associated with each dedicated instance.

WCF supports three kinds of behavior - PerCall, PerSession, and Single. When you use bindings like wsHttpBinding that supports reliable sessions, PerSession is the default behavior.

Below picture shows how PerSession instantiation happens

WCF InstanceContextMode.PerSession

Implementing PerSession service Behavior

Create WCF Service for PerSession Behavior

Create a new WCF service library and client applications as suggested in creating WCF service with PerCall Service Behavior. and understand how it works for PerCall behavior.

We will make some modification into the WCF Service library to work it as PerSession.

You may give different names to projects.

Configure WCF Service for PerSession Behavior

Assuming that you have created a WCF Service Library and client application as suggested in the previous step or download code files here, And hosted this WCF Service in IIS.

Open ProductService file from PerSessionWCFService WCF Service Library application. Modify ServiceBehavior attribute to configure PerSession.

Your code will look like

    
[ServiceBehavior(Name = "ProductService",
        Namespace = "https://geeksarray.com/productservice",
    InstanceContextMode = InstanceContextMode.PerSession)]
public class ProductService : IProductService
{
    public void SetProductQty(int qty)
    {
        this.ProductQty = qty;
    }

    public int GetProductQty()
    {
        return this.ProductQty;
    }

    private int ProductQty
    {
        get;
        set;
    }
}
            

Note that we have changed the InstanceContextMode property and set it as PerSession. Even if you removed the setting for the InstanceContextMode property the default value PerSession will be applied as you are using binding with the reliable session.

Update Service Reference

You have configured theservice as PerSession, now build the service library, publish and host it in IIS.

Open client application and update existing Service Reference by right click on ProductServiceRef under Service References of client application. Select Update Service Reference

Client application for PerSession WCF Service

Open the client application -> Program.cs file. Add code suggested in below descriptions to the main method of the Program.cs file.

    
ProductServiceRef.ProductServiceClient client
    = new ProductServiceRef.ProductServiceClient();
    
                

It creates a new client proxy for Product Service. WCF service will create a new dedicated instance to handle this proxy. The data which is manipulated by this instance will be saved in memory for future reference. All requests will be handle by this instance until you call the client.close();

    
client.SetProductQty(10);
Console.WriteLine(string.Format
            ("Current Product Qty: {0}", client.GetProductQty()));

client.SetProductQty(20);
Console.WriteLine(string.Format
            ("Current Product Qty: {0}", client.GetProductQty()));
Console.WriteLine(string.Format
            ("Current Product Qty: {0}", client.GetProductQty()));

client.Close();
                    
            

The first line is calling OperationContract SetProductQty which takes input value as 10. On the service side when this operation gets called it save the value in private property ProductQty which is available for the next calls.

The second line of code block calls OperationContract GetProductQty, service returns the value which is saved in property ProductQty. In this case, it returns value as 10. As this is PerSession behavior of WCF service saved the value 10 to ProductQty. If this was PerCall service behavior you would have got 0 (integer default) for each read call.

The third line of code block again calls operation to set value as 20. The next two lines read values from ProductQty and get it as 20.

The reason behind getting value as 20 for the second read call is, the value of ProductQty as 20 will be retain as long as you do not call client.close() to close proxy.

The next line closes the current client proxy.

                    
client = 
    new ProductServiceRef.ProductServiceClient();
Console.WriteLine(string.Format
    ("Current Product Qty: {0}", client.GetProductQty()));
    
    

Now you assign a new service instance to the client object and call the operation contract to read the value of ProductQty however you haven't set the value and the previous proxy is closed you will be getting value as 0(integer default).

Your entire code block will look like this.

    
namespace ClientApp
{
class Program
{
static void Main(string[] args)
{
    ProductServiceRef.ProductServiceClient client
        = new ProductServiceRef.ProductServiceClient();

    client.SetProductQty(10);
    Console.WriteLine(string.Format
        ("Current Product Qty: {0}", client.GetProductQty()));

    client.SetProductQty(20);
    Console.WriteLine(string.Format
        ("Current Product Qty: {0}", client.GetProductQty()));
    Console.WriteLine(string.Format
        ("Current Product Qty: {0}", client.GetProductQty()));

    client.Close();

    client = new ProductServiceRef.ProductServiceClient();
    Console.WriteLine(string.Format
        ("Current Product Qty: {0}", client.GetProductQty()));
    client.Close(); 

    Console.Read();
}
}
}

Speak your mind
Please login to post your comment!


Blog Search





If you like my content please feel free to buy me coffee. Buy Me A Coffee