Sasuke Uchiha
Sasuke Uchiha

Reputation: 1103

How to make controllers scoped or singleton instead of transient in ASP.NET Core?

How to make controllers scoped or singleton instead of transient in ASP.NET Core?

I now that by default the controllers are registered with the default DI container with the transient lifetime.

In case I would like to register them with a different lifetime, how could I do that?

I want to know that solely for the educational purposes, to better wrap my head around the controller types management by the DI container.

Upvotes: 21

Views: 13484

Answers (2)

Steven
Steven

Reputation: 172875

I now that by default the controllers are registered with the default DI container with the transient lifetime.

By default, controllers are not registered at all. It is the default IControllerActivator that is creating that type without an explicit registration.

If you want to change this, you should call:

services.AddMvc().AddControllersAsServices();

This will ensure that controllers are registered and the original IControllerActivator is replaced with one (ServiceBasedControllerActivator) that resolves controllers from the DI container.

AddControllersAsServices, unfortunately, always registers the controllers using the Transient lifetime and there's no way to override that behavior. So you have to re-implement AddControllersAsServices:

public static IMvcBuilder AddControllersAsServices(
    this IMvcBuilder builder, ServiceLifetime lifetime)
{
    var feature = new ControllerFeature();
    builder.PartManager.PopulateFeature(feature);

    foreach (var controller in feature.Controllers.Select(c => c.AsType()))
    {
        builder.Services.Add(
            ServiceDescriptor.Describe(controller, controller, lifetime));
    }

    builder.Services.Replace(ServiceDescriptor
        .Transient<IControllerActivator, ServiceBasedControllerActivator>());

    return builder;
}

This new extension method can be used as follows:

services.AddMvc().AddControllersAsServices(ServiceLifetime.Singleton);

WARNING: Do not register your controllers as Singleton in case they derive from Controller or ControllerBase, as these classes contain state (as @MarcoPelegrini correctly notes in the comments) and will malfunction when reused by multiple requests.

Upvotes: 34

AlexDGE
AlexDGE

Reputation: 21

The validated answer strictly responds to the question because it implicitly asks for how to register all controllers with a different (and same) lifetime. For those who wants to do it only for some specific controllers, a simple way consist in using:

services.AddSingleton<MySingletonController>();

before:

services.AddMvc().AddControllersAsServices();

It works fine because AddControllersAsServices use builder.Services.TryAddTransient. Then it won’t register MySingletonController as transient because it’s already done under the Singleton Lifetime.

And, as mentioned, do not derive MySingletonController from Controller or ControllerBase as it should be stateless.

Upvotes: 1

Related Questions