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();

12 comments:

  1. From operating systems to multimedia, PC & mobile games to anti-virus, from drivers to registry cleaners and internet tools our website features all the latest soft wares for safe and free downloading enjoy.

    ReplyDelete
  2. 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
  3. 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
  4. Hey keep posting such good and meaningful articles.

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

    ReplyDelete
  6. 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
  7. 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
  8. Hey keep posting such good and meaningful articles.

    ReplyDelete
  9. 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
  10. 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
  11. MD.Shameem Mridha
    Thank you, I’ve just been searching for information about this topic for a while and yours is the greatest I’ve discovered till now.

    ReplyDelete