ASP.NET MVC: DefaultControllerFactory is not thread safe!
I am not sure if this is a bug or a “feature” of the ASP.NET MVC framework. Either way, this is something you should be aware of as it can cause some very hard to track concurrency issues which might leak information between your HttpRequests.
In my last project we used the ASP.NET MVC framework along with StructureMap, which made us create our own controller factory. I just blogged about it here.
If it was a good design decision or not I am not completely certain anymore, but we added some more framework logic to the controller factory, including checking state on the HttpRequest-object. This, it turned out, was a path to trouble. After a while we had unexpected behavior in our system, sometimes it leaked information between HttpRequests it seemed! But since this only happened during stress testing, it was hard to find the cause.
What we found was that the RequestContext.HttpContext-object was not all the way thread safe. When a request hits our custom controller, and the request goes to sleep to serve another request, the HttpContext of the factory changes to the second requests HttpContext. This means that the first request will work on the second requests HttpContext when it wakes up. This is bad news if you are dependent on the HttpContext in your custom controller factory.
The lesson is this: you have to do at least one of the two actions below if you want to access the HttpContext in your custom controller factory.
1)
The problem occurs when you set your controller factory like this (normally in the global.asax file)
1 | ControllerBuilder.Current.SetControllerFactory(new CustomControllerFactory()); |
If you send in an instance lik this, it will be the only instance of your controller factory, and this is where the HttpContext-concurrency issues arrive in the first place. What you can do, which will take away all the concurrency issues is to send in the type instead of an instance, like this:
1 | ControllerBuilder.Current.SetControllerFactory(typeof(CustomControllerFactory)); |
The drawback to sending in the type is a) your controller factory will be instantiated for every request and b) you cannot send in anything to your instance (since you’re not instantiating it). This might not be a problem, and if not, you’re good to go.
2)
If you have to use one instance only of your controller factory, and you need to access the HttpContext for a request, you MUST use the HttpContext.Current-instance and not the RequestContext.HttpContext. The HttpContext.Current is as far as I’ve tested thread safe.
Not thread safe:
1 | RequestContext.HttpContext |
The thread safe alternative:
1 | HttpContext.Current |
I just found out that this is a bug
- Tore Vestues


Love this community and have practised so much upon the practice of this week even-handed nigh viewing other people’s threads and comments
May 6th, 2010 at 1:37 (109)Since we are in the well-disposed for sharing I deliberation I’d appropriate something with you members of this forum. A position where people can efflux the public tv appear mad men
Link is under the sun, make use of guys!
watch mad men online
Hi
If you are looking for fast and easy money please visti my web site.
I’m offering good job in-house if you have Internet connection only.
Please contact me if you need money shot.
Best Regards
August 18th, 2010 at 9:06 (421)Adam posilki
It’s hard to search out knowledgeable individuals on this matter, however you sound like you already know what you’re talking about! Thanks
July 8th, 2011 at 19:13 (842)Man .. Beautiful .. Superb .. Jeg bokmerke nettstedet ditt og ta feeds additionallyI er glad for å søke ut så mange nyttige opplysninger rett her i innlegget, vil vi gjerne utvikle flere strategier i denne forbindelse , takk for deling. . . . . .
September 22nd, 2011 at 5:30 (271)Hey. I just ran into your page while browsing Yahoo . Ive bookmarked it. Ill definitely return. By the way, have you seen the new Twilight movie yet? I know this is off topic. I need to go by Red Box and buy it tonight. Its great. See ya.
January 31st, 2012 at 18:00 (792)Thanks for sharing!
Midtown manhattan hotels best midtown manhattan hotels, cheap hotel in miami beach budget hotels in miami.