Why Can WebMethod Access Session State Without EnableSessionState? Why Can WebMethod Access Session State Without EnableSessionState? asp.net asp.net

Why Can WebMethod Access Session State Without EnableSessionState?


On http://msdn.microsoft.com/en-us/library/system.web.services.webmethodattribute.enablesession(v=vs.90).aspx, you will see that this applies to XML Web services (i.e., classes derived from System.Web.Services.WebService).

[WebMethod(EnableSession=true)]

Because your page presumably extends System.Web.UI.Page, it is not necessary to explicitly enable the session. On http://msdn.microsoft.com/en-us/library/system.web.configuration.pagessection.enablesessionstate.aspx, you can see that EnableSessionState is enabled by default for Pages (which you probably already know).


http://forums.asp.net/t/1630792.aspx/1

Answer of gsndotnet:You are right but whatever you are saying is applicable to a method in context of WebServices. We also use same WebMethod attribute on the methods of a WebService (.asmx). So in context of Web Services when we want to allow the access to Session we have to add EnableSession = true. Whereas in context of PageMethods they already have access to Session as they are defined inside a class that inherits from Page class.

Your msdn link means that you use web service, i.e. class derived from System.Web.Services.WebService.In your code you add your method directly on page, so it has access to session.