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/")]
[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();}
great example