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. 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. The changes were made, 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.
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.

(sursa: http://blogs.microsoft.co.il/blogs/)

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s