I just discovered that every request in an ASP.Net web application gets a Session lock at the beginning of a request, and then releases it at the end of the request!
In case the implications of this are lost on you, as it was for me at first, this basically means the following:
-
Any time an ASP.Net webpage is taking a long time to load (maybe due to a slow database call or whatever), and the user decides they want to navigate to a different page because they are tired of waiting, they can’t! The ASP.Net session lock forces the new page request to wait until the original request has finished its painfully slow load. Arrrgh.
-
Anytime an
UpdatePanelis loading slowly, and the user decides to navigate to a different page before theUpdatePanelhas finished updating… they can’t! The ASP.Net session lock forces the new page request to wait until the original request has finished its painfully slow load. Double Arrrgh!
So what are the options? So far I have come up with:
- Implement a Custom
SessionStateDataStore, which ASP.Net supports. I haven’t found too many out there to copy, and it seems kind of high risk and easy to mess up. - Keep track of all requests in progress, and if a request comes in from the same user, cancel the original request. Seems kind of extreme, but it would work (I think).
- Don’t use Session! When I need some kind of state for the user, I could just use
Cacheinstead, and key items on the authenticated username, or some such thing. Again seems kind of extreme.
I really can’t believe that the ASP.Net Microsoft team would have left such a huge performance bottleneck in the framework at version 4.0! Am I missing something obvious? How hard would it be to use a ThreadSafe collection for the Session?
OK, so big Props to Joel Muller for all his input. My ultimate solution was to use the Custom SessionStateModule detailed at the end of this MSDN article:
http://msdn.microsoft.com/en-us/library/system.web.sessionstate.sessionstateutility.aspx
This was:
This has made a HUGE difference to the feeling of "snapiness" to our application. I still can’t believe the custom implementation of ASP.Net Session locks the session for the whole request. This adds such a huge amount of sluggishness to websites. Judging from the amount of online research I had to do (and conversations with several really experienced ASP.Net developers), a lot of people have experienced this issue, but very few people have ever got to the bottom of the cause. Maybe I will write a letter to Scott Gu…