Click here to Skip to main content
Click here to Skip to main content

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

Introduction

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.

Background

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 = http://tempuri.org/, 
	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;
        }

        [WebMethod(EnableSession=true)]
        public string GetName()
        {
            if (Session["EmpName"] == null)
            {
                return "";
            }
            else
            {
                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:

Welcome.jpg

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)
        {
            try
            {
                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.

Final.jpg

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.

History

  • 11-Sept-2009: Initial post

License

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

About the Author

vivek_viv
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 Google.com

Comments and Discussions

 
GeneralWin32 client PinmemberSunny Ahuwanya12-Sep-09 12:15 
GeneralRe: Win32 client PinmemberSunny Ahuwanya12-Sep-09 12:20 
GeneralRe: Win32 client Pinmembervivek_viv12-Sep-09 20:34 

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

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

| Advertise | Privacy | Mobile
Web03 | 2.8.140709.1 | Last Updated 12 Sep 2009
Article Copyright 2009 by vivek_viv
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid