Click here to Skip to main content
15,867,141 members
Articles / Web Development / ASP.NET

Maintaining Session from the Client Application to the Web Service

Rate me:
Please Sign up or sign in to vote.
4.89/5 (14 votes)
11 Sep 2009CPOL2 min read 389.9K   36   18
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

C#
[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

C#
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:

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

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

C#
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)


Written By
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

 
QuestionWhere we will create these two methods..In which page? Webservice or aspx page? Pin
mohanjune19873-Oct-12 0:04
mohanjune19873-Oct-12 0:04 
AnswerRe: Where we will create these two methods..In which page? Webservice or aspx page? Pin
vivek_viv25-Nov-12 22:42
vivek_viv25-Nov-12 22:42 
QuestionCookie Container ? Pin
Ashu 135765729-May-12 23:07
Ashu 135765729-May-12 23:07 
AnswerRe: Cookie Container ? Pin
vivek_viv6-Jun-12 5:29
vivek_viv6-Jun-12 5:29 
GeneralMy vote of 3 Pin
Yash from Pune13-Mar-12 20:17
Yash from Pune13-Mar-12 20:17 
GeneralRe: My vote of 3 Pin
vivek_viv19-Mar-12 22:40
vivek_viv19-Mar-12 22:40 
GeneralNo .CookieContainer Pin
hAzUpL88-Oct-09 7:40
hAzUpL88-Oct-09 7:40 
GeneralA little more detail here. Pin
Steve Wellens12-Sep-09 15:22
Steve Wellens12-Sep-09 15:22 
GeneralRe: A little more detail here. Pin
vivek_viv12-Sep-09 20:52
vivek_viv12-Sep-09 20:52 
GeneralGood work Pin
Abhishek Sur12-Sep-09 13:04
professionalAbhishek Sur12-Sep-09 13:04 
Nice work.

Hope to find more package from you like this. 5 from me
Cheers.
Rose | [Rose]

Abhishek Sur

My Latest Articles
Create CLR objects in SQL Server 2005
C# Uncommon Keywords
Read/Write Excel using OleDB

Don't forget to click "Good Answer" if you like to.

GeneralRe: Good work Pin
vivek_viv12-Sep-09 20:36
vivek_viv12-Sep-09 20:36 
GeneralWin32 client Pin
Sunny Ahuwanya12-Sep-09 12:15
Sunny Ahuwanya12-Sep-09 12:15 
GeneralRe: Win32 client Pin
Sunny Ahuwanya12-Sep-09 12:20
Sunny Ahuwanya12-Sep-09 12:20 
GeneralRe: Win32 client Pin
vivek_viv12-Sep-09 20:34
vivek_viv12-Sep-09 20:34 
GeneralExcellent Pin
sanjay_gope11-Sep-09 21:46
sanjay_gope11-Sep-09 21:46 
GeneralRe: Excellent Pin
vivek_viv11-Sep-09 22:04
vivek_viv11-Sep-09 22:04 
GeneralExcelent Start Pin
Abhijit Jana11-Sep-09 21:14
professionalAbhijit Jana11-Sep-09 21:14 
GeneralRe: Excelent Start Pin
vivek_viv12-Sep-09 20:38
vivek_viv12-Sep-09 20: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.