ThreadStaticAttribute in ASP.NET

Nope, Scott is right: a request definitely doesn’t have to run on a single thread for its entire duration. ASP.NET is thread-agile in that respect. There are only a few points where the switch can happen, but it definitely can happen. (I’ve tested it for myself.)

You may wish to read this blog post and this Spring forum thread for some more details.

Basically you should find a different way of capturing context. The relevant bit from your point of view is probably at the end of the blog post:

This is a major PITA, because as far as I can see it mean the only persistence option for ‘ThreadStatic’esque behavior in ASP.Net is to use HttpContext. So for your business objects, either you’re stuck with the if(HttpContext.Current!=null) and the System.Web reference (yuck) or you’ve got to come up with some kind of provider model for your static persistence, which will need setting up prior to the point that any of these singletons are accessed. Double yuck.

Leave a Comment