EnableSession: It is used to enable
session state for web method by simply set this property to true. We can enable
session state for every individual method.
Now create a web service using EnableSession property of web method attribute.
Take a Web Service Application and replace the code with following code.
using
System;
using
System.Collections.Generic;
using
System.Linq;
using System.Web;
using
System.Web.Services;
namespace
webmethodproperties
{
///
<summary>
/// Summary
description for Service1
///
</summary>
[WebService(Namespace =
"http://tempuri.org/")]
[WebServiceBinding(ConformsTo =
WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from
script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
public class
Service1 : System.Web.Services.WebService
{
[WebMethod(EnableSession=true
)]
public int
get()
{
if (Session["ss"]
== null)
{
Session["ss"] = 1;
}
else
{
int i = (int)Session["ss"];
i++;
Session["ss"] = i;
}
return (int)Session["ss"];
}
}
}
This service will return a number that how many times the method has called.
Then, Run the service application -> go to test page of service->invoke the
method.
Output:

You will note that whenever you refresh the page, the number is increased by 1.
Join the conversation! Your thoughts help the community grow.
Sign in to leave a comment
It is the same account you read, post and publish with — and you will come straight back to this page.
Mohammad Shahid SolankiPosted Nov 4, 2017, 6:18 AM
Very needful... i have used this thing in one of my project !!!
megala kanimozhiPosted Oct 3, 2016, 9:35 AM
Is it possible to access mvc controller created session in web api controller
Fellipe VieiraPosted Aug 17, 2012, 8:52 AM
Hi Alok. I checked your post and I used this attribute on my webmethods. But then I thought in one thing if I could apply this attribute to my entire webservice class. The reason is that I wanted all my webmethods to share session state. Is there a way so I can accomplish such thing ?