Skip to main content
How to enable session state in web services
Entry Date: Rate:
RSS Description:
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.

Web services are stateless and use SOAP and XML... Ehhh ummm ??? NOT ALWAYS

In AJAX Web applications, most of the times web services and page methods are just "private" for a given page or web application, taking this into consideration we have that:

  • --> SOAP is to heavy for HTTP traffic (we use AJAX to sky rocket performance and have minimum weight on our roundtrips to the server) and complex to handle using Javascript, it's better to serialize using JSON.
  • --> Webservices interacting with your web application will require most of the time to use the session web application object (security, values in session...) so no stateless for your web services. You could try to use WSE and... but things get quite complicated.

This article explains you how to enable session state for a given web method of your webservice, quite easy (just one line of code) and quite useful