When requested from an Asp.Net Core client, each time it re-creates the controller and resolves all its dependencies, dependencies, and so on.

In my opinion, this is a very expensive operation, well, it may not be very, but still it is unnecessary actions.

And I have a question, is it possible to somehow make sure that the controllers are singletones?

I understand why they are now Scoped, because of thread safety. But on the other hand, it is enough just not to make any state of the controllers, and this will also provide thread safety.

Perhaps the controllers in general in Asp.Net Core can not be made singletones in a simple way, to the point that all the infrastructure there is tied to what they are now. But maybe there are some approaches or solutions to this situation?

Maybe there are some custom third-party solutions (MVC) for this on .Net Core?

In this question, I am counting not so much on deciding the correct answer for the whole situation, but on a discussion about the possibility and usefulness of such.

While he studied this question, he found out that there are such things as

  1. IControllerActivator - as I understand it is responsible just for getting (creating) the controller. And in theory, it is possible not to create new controllers each time, but to pull out the object from the DI container.
  2. IControllerFactory

In the case of using IControllerActivator it is not very clear whether or not it will turn out, since such concepts as Request, Response as it is in Asp.Net Core are rigidly written inside the controller as its properties.

  • What purpose do you want to do this for? As a rule, creating a controller is not as expensive as processing the business logic of an application or, for example, accessing a data layer ... it can start optimizing there first, if there are performance problems, of course) Please note that all Scoped dependencies are created once per request, but not for every rezolv dependencies - Sultanov Shamil
  • @SultanovShamil in this matter is purely theoretical interest. On the other hand, I just don’t like how they are created now, I don’t think that I’ll redo it in my projects if there is no simple way. And also in general I want to know the opinion of other people on this topic. - Dmitry Polyanin

0