C# Using sessions in a webmethod

With all the web 2.0 hype webmethod’s are a key in dynamic build websites.

 

But what if you want to get access to your session in a webmethod?
Well the guy’s at Microsoft have been smart enough to give you a easy option to do just that.
Note that webmethod’s are designed to be as fast and with as little overhead as possible.
Enabling session state in a webmethod takes a toll on performance of that method, so only use this option if there is a real need for it.

 

Sample code

        [WebMethod (EnableSession=true)]
        public static string SampleMethode()
        {
            return HttpContext.Current.Session["MyId"];
        }

For more information see http://msdn.microsoft.com/en-us/library/system.web.services.webmethodattribute.enablesession(v=vs.71).aspx