How to enable session state in web services

Some days back I was writing a few web services. I have coded web services many times, but never required to use Session State before. This time I required to check a few session variables before returning the correct data from the web service. I tried using the HttpSession like in a normal web page but got an error. After a little search found the right way to use HttpSession in webmethods. The simple trick is to enable the Session State before using it directly. This can be done by using the following attribute

[WebMethod(EnableSession = true)] // C#

<WebMethod(EnableSession:=true)> // VB .Net

Once you specify these lines over your web method you can use the session object naturally.

4 Responses to “How to enable session state in web services”

  1. Angus McDonald Says:

    Anu,

    Nice tip, there’s a lot of good uses for attributes and it’s something we often overlook.

    Missing you already, hope the new job is great!

    Cheers,
    Angus

  2. ruth amir Says:

    THANK!!!

  3. Fellipe Vieira Says:

    Hi,

    Recently I created a webservice, where I developed many webmethods that later I figured out the needing to work with session state. I searched on google, and then I found this attribute that helped me a lot.

    But then I suddenly thought if I could use/apply this attribute to an entire webservice class. The reason is that I wanted to set this configuration only in one place, and then I would ready to work with session variables in all my webmethods.

    Is there a way so I can accomplish such thing?

  4. mauricio araya Says:

    Thanks!!!


Leave a comment