Click here to Skip to main content
13,797,304 members
Click here to Skip to main content
Add your own
alternative version


36 bookmarked
Posted 11 Sep 2009
Licenced CPOL

Maintaining Session from the Client Application to the Web Service

, 11 Sep 2009
Rate this:
Please Sign up or sign in to vote.
This article is based on maintaining session from the client application to the Web Service


This is my first article on The Code Project that is based on maintaining session from the client application to the Web Service. In this article, I try to give a basic idea for maintaining the session from client to web service.


I hope you have already a basic idea about session management. By default, web service does not support session state. For achieving high scalability, web service is designed stateless. Suppose the requirement is to use session management in a web service to retain user specific information, you need to use the session in your web service. You can specify the session management in the particular web methods of the web service wherever the session state is required. Session state handling is not a part of SOAP specification. The cookie stores a session ID and ASP.NET uses the session ID to associate the client with the session state on the server. A new session will create each and every request if the client does not support cookies. This might be a problem for the cookieless session at the client.

Sample Code

[WebService(Namespace =, 
	Name = "Employees Service", Description = "Retrieve the Employees detail")]
    public class Service1 : System.Web.Services.WebService
        [WebMethod(EnableSession = true)]
        public void SetName(string strName)
            Session["EmpName"] = strName;

        public string GetName()
            if (Session["EmpName"] == null)
                return "";
                return (string)Session["EmpName"];

Client Code

namespace ConsumeWebService
    public partial class _Default : System.Web.UI.Page
      protected void Button2_Click(object sender, EventArgs e)
            //Create proxy of the Web Service
	   localhost.EmployeesService objWeb = 
		new ConsumeWebService.localhost.EmployeesService();
            objWeb.SetName("Vivek Kumar");
            MessageBox.Show("Welcome : " + objWeb.GetName());

The client code will produce the following output:


Here the name that you enter is not displaying on the popup window. For getting the data, we need to create the Cookie Container object in the client application and explicitly pass the object of this cookie container to the object of the web service. You need to keep the cookie container around as long as you need to keep the session cookie. To correct this code, you can create the cookie container as a form-level variable, as shown below:

private System.Net.CookieContainer CK = new System.Net.CookieContainer();

Now this object will pass to the object of the webservice before calling any method:

private void btnGetName_Click(object sender, EventArgs e)
                WebProxy.CookieContainer = CK;
                string strName = WebProxy.GetName();
                MessageBox.Show("Welcome:"+strName+ "!!");
            catch(Exception Ex)

Once you have the above lines of code, you will be able to get the session from your client code to the web service.


Points that Need to be Taken Care of

  • Session state will disappear when the session times out. The client will have no way of knowing when the session times out, which means the web service may behave unpredictably.
  • Session state is tied to a specific user, not to a specific class or object. This can cause problems if the same client wants to use the same web service in two different ways or creates two instances of the proxy class at once.
  • Session state is maintained only if the client preserves the session cookie. The state management you use in a web service won't work if the client fails to take these steps. For these reasons, web services and state management don't offer a natural fit.


  • 11-Sept-2009: Initial post


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Web Developer
India India
I have done Master Degree in Computer Application from JAYPEE INSTITUTE OF INFORMATION TECHNOLOGY(JIIT),NOIDA, U.P, India .I am having more than 3.5 years of solid experience in web-based and windows based solutions in Microsoft Technologies using .NET 2.0, .NET 3.0 , .NET 3.5, ASP.NET 2.0, ASP.NET 3.5 C# 2.0,Web Services, MS SQL Server 2005,Win Forms, Win Services,WCF, WWF. My area of interest is on object oriented programming ,Web Service,WCF and WWF,knowledge of 3-Tier Architecture and Designing. My hobbies, listing to music and and giving my maximum free time on

You may also be interested in...


Comments and Discussions

GeneralRe: Good work Pin
vivek_viv12-Sep-09 21:36
membervivek_viv12-Sep-09 21:36 
GeneralWin32 client Pin
Sunny Ahuwanya12-Sep-09 13:15
memberSunny Ahuwanya12-Sep-09 13:15 
GeneralRe: Win32 client Pin
Sunny Ahuwanya12-Sep-09 13:20
memberSunny Ahuwanya12-Sep-09 13:20 
GeneralRe: Win32 client Pin
vivek_viv12-Sep-09 21:34
membervivek_viv12-Sep-09 21:34 
GeneralExcellent Pin
sanjay_gope11-Sep-09 22:46
membersanjay_gope11-Sep-09 22:46 
GeneralRe: Excellent Pin
vivek_viv11-Sep-09 23:04
membervivek_viv11-Sep-09 23:04 
GeneralExcelent Start Pin
Abhijit Jana11-Sep-09 22:14
mentorAbhijit Jana11-Sep-09 22:14 
GeneralRe: Excelent Start Pin
vivek_viv12-Sep-09 21:38
membervivek_viv12-Sep-09 21:38 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web05 | 2.8.181207.3 | Last Updated 12 Sep 2009
Article Copyright 2009 by vivek_viv
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid