Session Id in WCF

Objective 

This article is part # 4 of Instance Management in WCF. This article will explain how to work with Session ID in WCF. This article will give theoretical introduction and explanation of coding sample also. It will explain 2 full coding sample that how to work with Session ID in WCF.

Part # 1 of this series could be found Part 1

Part # 2 of this series could be found Part 2

Part # 3 of this series could be found Part 3 

Note: There is no any attached code with this article because; I have amended same sample to give different demonstration. I am sorry for that, but once you will go through article, you will able to understand the concept behind. 

The Session ID in WCF

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    

                                                                                                                                                                                                                                                                                                                          

  1. Every session has a unique Session ID.
  2. Both client and service can obtain and use the Session ID.
  3. The Session ID is in form of GUID.
  4. The main use of Session ID is in logging.


    Session ID at Server side 

  1. Every Service operation has an operation call context accessible via the operational class.
  2. A Service can obtain a reference to the operation context of current method via the Current static method of the OperationConext class.

 

Session ID at client side

 

Fetching Session ID at client for a Per-Session configured service (Sample #1)

In below code sample

  1. Service is configured as Session full service.
  2. SessionMode of contract of service is set to Required.
  3. Client is referencing the service and creating different proxies.
  4. Expected output is , 

     

Service Interface (Contract)

using System;

using System.Collections.Generic;

using System.Linq;

using System.Runtime.Serialization;

using System.ServiceModel;

using System.Text;

namespace SessionTestingService

{

[ServiceContract(SessionMode=SessionMode.Required)]


public
interface
IService1

{

[OperationContract]


int GetData();

}

}

Service

using System;

using System.Collections.Generic;

using System.Linq;

using System.Runtime.Serialization;

using System.ServiceModel;

using System.Text;

namespace SessionTestingService

{

[ServiceBehavior(InstanceContextMode= InstanceContextMode.PerSession)]


public
class
Service1 : IService1

{


public
int GetData()

{


return 9;

}

}

}

Client

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using SessionIdSampleatClient.ServiceReference1;

namespace SessionIdSampleatClient

{


class
Program

{


static
void Main(string[] args)

{


Service1Client proxy = new
Service1Client();

proxy.Open();


string sessionid = proxy.InnerChannel.SessionId;


Console.WriteLine(“Session id of 1st Proxy = “+sessionid);

proxy.Close();


Service1Client proxy1 = new
Service1Client();

proxy1.Open();

sessionid = proxy1.InnerChannel.SessionId;


Console.WriteLine(“Session id of 2nd Proxy = “ + sessionid);

proxy1.Close();


Service1Client proxy3 = new
Service1Client();

proxy3.Open();

sessionid = proxy1.InnerChannel.SessionId;


Console.WriteLine(“Session id of 2nd Proxy = “ + sessionid);

proxy3.Close();


Console.ReadKey(true);

}

}

}

Output



Comparing Session ID at client and Service for a Per-Session configured service (Sample #2)

In below code sample

  1. Service is configured as Session full service.
  2. SessionMode of contract of service is set to Required.
  3. Service is exposing one Operation Contract, which is returning Session ID for a particular operation context.
  4. Client is creating a Proxy and fetching the session ID.
  5. There is a comparison between Session Id returned by service and Session Id fetched by client for a particular session. Whether both session ID is same or not.

Service Interface (Contract)

using System;

using System.Collections.Generic;

using System.Linq;

using System.Runtime.Serialization;

using System.ServiceModel;

using System.Text;

namespace SessionTestingService

{

[ServiceContract(SessionMode=SessionMode.Required)]


public
interface
IService1

{

[OperationContract]


string GetSessionID();

}

}

Service

using System;

using System.Collections.Generic;

using System.Linq;

using System.Runtime.Serialization;

using System.ServiceModel;

using System.Text;

namespace SessionTestingService

{

[ServiceBehavior(InstanceContextMode= InstanceContextMode.PerSession)]


public
class
Service1 : IService1

{


public
string GetSessionID()

{


string sessionId = OperationContext.Current.SessionId;


return sessionId;

}

}

}

Client

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using SessionIdSampleatClient.ServiceReference1;

namespace SessionIdSampleatClient

{


class
Program

{


static
void Main(string[] args)

{


Service1Client proxy = new
Service1Client();

proxy.Open();


Console.WriteLine(“Session Id Fetched from the Service “ + proxy.GetSessionID());


string sessionid = proxy.InnerChannel.SessionId;


Console.WriteLine(“Session id Fetched at the Client = “+sessionid);


int res = sessionid.CompareTo(proxy.GetSessionID());


if(res==0)

{


Console.WriteLine(“Service Session Id and Clinet Session Id is Same “);

}

proxy.Close();


Console.ReadKey(true);

}

}

}

Output



 Conclusion 

This article explained about, Session ID in WCF. This article gave theoretical introduction and explanation of coding sample also.

Happy Coding

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 )

Facebook photo

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

Connecting to %s

Create a website or blog at WordPress.com