WCF Simplified Part 8: Sessions, Instance Management and Concurrency

Today we’ll look at WCF sessions, instance management, and concurrency.

1. Sessions: A session is a grouping of client requests. Unlike ASP.NET there is no data store associated with a WCF session. Not all bindings support sessions (BasicHttpBinding does not). Client applications initiate sessions and then receive and process the messages sent within the session. Messages delivered during a session are processed in the order in which they are received. When a WCF service accepts a client session, all calls between a WCF client object are handled by the same user-defined service object.

We can use the SessionMode attribute to require, allow, or prohibit bindings to use sessions between endpoints.

    a. Allowed: (default) If a service contract specifies that it allows sessions but does not require one, clients can connect and either establish a session or not establish a session.

    b. Required: When a service contract specifies that it requires a session, that contract is specifying that all calls must be part of the same conversation. The service contract requires a sessionful binding. An exception is thrown if the binding is not configured to support session (can’t use BasicHttpBinding). Creating a service contract that requires a session states that the group of operations that the service contract declares must all be executed within the same session and that messages must be delivered in order.

    c: NotAllowed: The service contract never supports bindings that initiate sessions.

2. Instance Management: The refers to how WCF manages your service class object instantiation (and disposal) depending on a client request, that is, managing the lifetime of the service instance. The possible values for InstanceContextMode are.

    a. PerCall: A new service object is created for each client request, that means that even if the same client is sending requests, a new service object is created for each request and disposed after the reply is sent. To see this, use a service like this,

public class Service1 : IService1 {
    Guid guid = Guid.NewGuid();

    public string GetGuid() {
        return guid.ToString();

and a client like this,

for (int i = 0; i < 5; i++) {

When a client calls the GetGuid method it will get a new Guid everytime, because the Service1 object is recreated after every reply.


    b. PerSession: (default) A new service object is created for each new client session and maintained for the lifetime of the session (applies to bindings that supports session like WSHttpBinsing). For BasicHttpBinding which doesn’t support sessions, its the same as PerCall. To see this, connect multiple clients to the host and each client will get a new Guid, Thus some state is maintained between requests from the same client.


    c. Single: A single service object handles all clients and client requests for the lifetime of the application. To see this. simply change the InstanceContextMode to InstanceContextMode.Single. Now every client and every client call will return the same Guid.


3. Concurrency: This refers to the number of threads executing at the same time in a service instance.

    a. Single: (default) One thread at a time can access the service object. The service object queues additional calls in the order it receives them. When the first call finishes, the next call in the queue locks the service. The benefit to this approach is that there is no risk of two calls reading the same memory and changing the same data. To see this try the service,

[ServiceBehavior(InstanceContextMode=InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Single)]
public class Service1 : IService1 {

public void TestConcurrency() {

This says that there is a single service object and only one thread can access it. Running two clients against this host will result in,


Note that after the first client took 10sec but the second took 20sec.

  b. Reentrant. One thread at a time can access the service object but the thread can exit and reenter. This is useful if the service needs to make callbacks to the client.

    c. Multiple. More than one thread at a time can access the service object. The WCF runtime assigns a thread to each client call and they execute simultaneously. Its up to you to make the service thread safe. To see this, simply change the ConcurrencyMode to Multiple in the example above, now running two clients will result in both taking 10sec,


To see how sessions interact with instance management in more detail see here.


About soumya chattopadhyay
I live and work in Seattle, WA. I work with Microsoft technologies, and I'm especially interested in C#.

4 Responses to WCF Simplified Part 8: Sessions, Instance Management and Concurrency

  1. Pingback: WCF Simplified Series « I.Net

  2. kesavaraj says:

    Very nice for beginners. Easy to understand

  3. Moss says:

    WCF Service respone getting mixed up

    When we try to send request WCF Service from two differnet Computer at the same time output is mixup


    computer A — Request1
    computer B — Request2


    computer A — Response1
    computer B — Response1(Response1 is override the Response2)

  4. Stig says:

    Hi Moss, 2011 is a long time ago but maybe you remember how you solved this case? Which binding did you struggle with?
    We experience something quite similar with basicHTTPbinding, however we ain’t sure about why this is happening.

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 )


Connecting to %s

%d bloggers like this: