Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When does an ASP.NET controller get destroyed? [duplicate]

According to this answer, ASP.NET MVC creates a new instance of the controller class to respond to each request.

My question is, when does the controller instance get destroyed?

So far, I've been assuming (perhaps incorrectly) that these instances are destroyed at the end of each response, but some database pool issues I'm having lead me to think that perhaps they are left for the garbage collector. Does anyone have any insights on this?

like image 482
Mark Micallef Avatar asked May 27 '26 13:05

Mark Micallef


1 Answers

The controller is left to be cleaned up by the garbage collector. But if you were to issue a new request before the old controller is cleaned up it should not affect your new request since a new controller with a fresh context will be created for that new request.

If you are having DB issues, it may come from how you handle reading/writing to the DB on your back-end. A DB context 'A' is not aware of the changes happening in another DB context 'B' if A was instantiated before changes in B took place. Not sure if that is the problem you are getting though.

like image 152
Sami Awwad Avatar answered May 30 '26 05:05

Sami Awwad