Saturday, April 17, 2010

ASP.NET Enable Session in Web Service Methods

Ever tried accessing session variables in Web Service methods? It won't work unless you add a little property in the WebMethod header of the functions.

For example, the basic hello world:

[WebMethod(EnableSession=true)]
public void HelloWorld()
{
    ...
}

Just add the statement EnableSession=true and you'll be able to access session variables found normally in aspx pages like the following sample.

string userid = Session["userid"].ToString();

9 comments:

  1. Its nothing like that. I tried without using that, It worked.
    //Page Load
    Session["sid"] = "123";

    //Web Service Method
    var s = Session["sid"]; //s = "123"

    ReplyDelete
  2. Awesome work.Just wanted to drop a comment and say I am new to your blog and really like what I am reading.Thanks for the share

    ReplyDelete
  3. Hey keep posting such good and meaningful articles.

    ReplyDelete
  4. Very informative, keep posting such good articles, it really helps to know about things.

    ReplyDelete
  5. Very interesting blog. A lot of blogs I see these days don't really provide anything that attract others, but I'm most definitely interested in this one. Just thought that I would post and let you know.

    ReplyDelete
  6. Very great post. I simply stumbled upon your blog and wanted to say that I have really enjoyed browsing your weblog posts. After all I’ll be subscribing on your feed and I am hoping you write again very soon!

    ReplyDelete
  7. Hey keep posting such good and meaningful articles.

    ReplyDelete
  8. What you're saying is completely true. I know that everybody must say the same thing, but I just think that you put it in a way that everyone can understand. I'm sure you'll reach so many people with what you've got to say.

    ReplyDelete
  9. It was very useful for me. Keep sharing such ideas in the future as well. This was actually what I was looking for, and I am glad to came here! Thanks for sharing the such information with us.

    ReplyDelete