DCSIMG
Ajax Page Methods Will Always Have Session - Doron's .NET Space

Ajax Page Methods Will Always Have Session

In my ongoing process of performance optimization, I noticed the use of an ASP.NET Ajax Page Method in a certain page. It looked something like this:

[WebMethod(true)]

public static bool HasDataBeenUpdated(DateTime lastUpdateTime)

{

   //Checks something in the cache against this date

   //....

}

Page methods are a way to Ajax-ly call a static method on the page, instead of creating a specific web-service for this. As you can see, this method was marked with [WebMethod(true)]. This property is usually used in web services, but ASP.NET Ajax employs it for page-methods as well (it is rather the same thing). The constructor we used for this attribute is defined like this:

public WebMethodAttribute (

   bool enableSession

)

That means that passing true will cause ASP.NET to load the Session for this request, making it more costly. I noticed that the HasDataBeenUpdated method can be changed so it won't need the Session. Since it was being called every 5 seconds from the client, it had to be very efficient. I made the change, and was able to turn off Session by using [WebMethod(false)]. Thing is, it didn't really get turned off. The request was still slower than it should have been. Apparently, if you use a page-method, Session will be loaded even if you explicitly pass false to the enableSession parameter.

The documentation doesn't mention this, but the example there is about Session access, so that should have probably tipped me off. This issue seems rather weird to me. I understand that maybe the defaults should change (web-services methods default to no-session) when you write [WebMethod] and nothing else. Maybe. But since I'm explicitly asking for no-Session mode, the result is surprising.

Personally, I think that if they wanted page-methods to behave differently from web-service methods, they should have used a different attribute for it. Anyway, the solution is simple: I ended up moving the method to an .asmx web-service and calling it instead.

Published Tuesday, May 06, 2008 8:54 PM by dorony
תגים:,

Comments

# re: Ajax Page Methods Will Always Have Session

Wow, I figured Session wasn't loaded unless you passed in true.  Thanks for posting this.

-Damien

Friday, September 05, 2008 9:51 PM by Damien White

# re: Ajax Page Methods Will Always Have Session

Thanks... Its a wonderful piece of info.. Keep posting..

Tuesday, November 18, 2008 6:04 PM by Russel

# re: Ajax Page Methods Will Always Have Session

How do i pass session information to a asp.net  application which stores session id as a query string to a PageMethod?

Wednesday, November 26, 2008 9:12 AM by ToolDevelopment

# re: Ajax Page Methods Will Always Have Session

Thanks for doing this research.  Just a moment ago, I noticed that I had some Page Methods that were successfully accessing the Session object, even though I didn't specify True for the argument.  I thought, hypothesized, then did a quick Internet search to find this page on top of the list.  Thanks for the confirmation (so I don't have to research this myself!)

Friday, October 23, 2009 3:46 AM by Barry Pekin

Leave a Comment

(required) 
(required) 
(optional)
(required) 

Enter the numbers above: