How to enable session in Web Service?


If we are working on web service so many times, we will get requirement to access the asp.net 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 = "http://tempuri.org/&quot;)]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

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

[WebMethod(EnableSession=true)]
public string HelloWorld()
{
string a = Session["Test"].ToString();
return a;
}

}

Step3:Compile the service and consume in asp.net 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();

}

Advertisement

One thought on “How to enable session in Web Service?

  1. srinivas June 21, 2012 / 3:37 am

    great example

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 )

Twitter picture

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

Facebook photo

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

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.