2

I'm debugging an ASP.NET MVC 2 application running on .NET 4.0 (Windows 7), currently inside of Cassini, the Visual Studio Debugging Web Server.

All was running well, when I suddenly was having exceptions in my Application.EndRequest handler. After investigating into this issue, I found the reason was that Cassini was calling EndRequest on a different thread than BeginRequest (then, a ThreadStatic variable which was set in BeginRequest and used in the EndRequest handler was null, since on this thread, BeginRequest was never called).

What could be the reason for Cassini to execute BeginRequest and EndRequest on different threads?

I know the reason could be an 'Async' Page directive (see Do ASP.NET Requests always BeginRequest and EndRequest on the same thread? , but I have no 'Async' attribute in my Page directives).

There must be another cause.

Thank you!

Community
  • 1
  • 1
Jonas Sourlier
  • 13,684
  • 16
  • 77
  • 148

1 Answers1

5

ASP.NET is thread-agile in general. You shouldn't assume that the whole request will be handled in one thread. Use the HTTP context instead of thread-static variables.

While there are only certain places where the thread can switch during request handling, it's a good idea to avoid making any assumptions IMO.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194