I am using a custom ModelBinder in an MVC 4 application, but it is invoked on a different thread than the request event handlers in global.asax, and this makes setting up a performance profiling context in a ThreadLocal fail.
The threadId is in brackets at the start of each line, and you can see that the thread changes when the model binding is invoked, and that is the thread in which the controller action (Index) is executed.
[33] | ERROR | MyApp.MvcApplication | Application_BeginRequest
[33] | ERROR | MyApp.MvcApplication | Application_AuthenticateRequest
[33] | ERROR | MyApp.MvcApplication | Application_AuthorizeRequest
[33] | ERROR | MyApp.MvcApplication | Application_ResolveRequestCache
[33] | ERROR | MyApp.MvcApplication | Application_AcquireRequestState
[33] | ERROR | MyApp.MvcApplication | Application_PreRequestHandlerExecute
[52] | ERROR | Binders.MyModelBinder | ModelBinder
[52] | ERROR | MyApp.Controllers.MyController | Index
Any idea why this is happening? My expectation was that the controller methods would always be invoked on the same thread as the application event handlers in global.asax
Turns out that my core assumption was incorrect - the controller actions are not invoked on the same thread as the event handlers.
Here is a snippet from Environment.StackTrace that shows the controller action being invoked by an asynchronous process (on another thread).
Looks like I'll be storing my performance profiling context in the session instead.