How to enable session in Web Service?

If we are working on web service so many times, we will get requirement to access the session. We can access the Session in web service by including (EnableSession=true) in WebMethod.

We can test this concept like this
Step1: Store some value in session, For example

Session[“Test”] = “This is Session Test using WebService”;

Step2:Write the webMethod like this

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

/// <summary>
/// Summary description for WebService
/// </summary>
[WebService(Namespace = ";)]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

public class WebService : System.Web.Services.WebService {

public string HelloWorld()
string a = Session["Test"].ToString();
return a;


Step3:Compile the service and consume in application
Step4: Call the service in default page like this

protected void BtnCheck_Click(object sender, EventArgs e)
localhost.WebService proxy = new localhost.WebService();
lblmsg.Text = proxy.HelloWorld();



One Response to “How to enable session in Web Service?”

  1. srinivas Says:

    great example

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your 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 )

Google+ photo

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

Connecting to %s

%d bloggers like this: