EnableSession Property of WebMethod in Web Service

Here, you will learn about EnableSession property of WebMethod in web service.

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:

enablesession property of web method

You will note that whenever you refresh the page, the number is increased by 1.