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:


    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!


  2. ruth amir Says:


  3. Fellipe Vieira Says:


    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:


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: