I’m implementing a custom IControllerFactory to create my asp.net mvc controllers.
In this factory’s CreateController method, I’m relying on the fact that the thread
that creates the controller, is unique; meaning no other Controller is created on that thread before ReleaseController is called.
Now, under heavy load testing, I’m running into issues.
Say that I have created Controllers on threads 1,2,3, it appears that a new
Controller is being created on thread 1, before the first one has called
IControllerFactory.ReleaseController.
Is this an expected behavior?
Each request to the controller is handled on a separate thread, right?
How comes the same thread is being reused for a different request, before IControllerFactory’s ReleaseController method is called?
Thanks for your time,
Koen
You absolutely cannot rely on such behavior. There’s nothing guaranteeing you that. Things could get even worse if you use asynchronous controllers.
Yes.
No.
ASP.NET uses a thread pool to service requests. So for example a thread is drawn from this pool to service the request and later this thread is returned to the pool for reuse. So you could perfectly fine have the same thread execute the controller code for two completely separate requests.
You should absolutely never rely in an ASP.NET application on threads. If you want to store some per-request specific information you should use the HttpContext storage, not threads. Forget about threads in an ASP.NET application if you want to be safe.