What is the scope of the controllers in .net project?
Solution 1:
The scope of a controller in all ASP.NET stacks since ASP.NET MVC 1.0 is the request - a new Controller instance is created by the ASP.NET middleware to handle each request.
The ASP.NET Core middleware pipeline is pretty deep though, and controller instances are only created in the last stage, in the Endpoint
middleware.
They controller and the action that handles the request are the User Code
box in the endpoint pipelines
You'll have to assume that the controller will be disposed very soon after an action method finishes processing a request. It won't be safe to access a controller instance from any other class after that point, even other scoped classes.
If you want to share data you'll have to store it in some other place, eg the Session, a cache, use common Options objects, a singleton Dictionary (essentially a cache), a database etc.