Microsoft.Extensions.DependencyInjection.AutoActivation 10.10.0

Microsoft.Extensions.DependencyInjection.AutoActivation

This provides the ability to instantiate registered singletons during startup instead of during the first time it is used.

A singleton is typically created when it is first used, which can lead to higher than usual latency in responding to incoming requests. Creating the instances on startup helps prevent the service from exceeding its SLA for the first set of requests it processes.

Install the package

From the command-line:

dotnet add package Microsoft.Extensions.DependencyInjection.AutoActivation

Or directly in the C# project file:

<ItemGroup>
  <PackageReference Include="Microsoft.Extensions.DependencyInjection.AutoActivation" Version="[CURRENTVERSION]" />
</ItemGroup>

Usage Example

Registering Services

The services to auto-activate can be registered using the following methods:

public static IServiceCollection ActivateSingleton<TService>(this IServiceCollection services)
public static IServiceCollection ActivateSingleton(this IServiceCollection services, Type serviceType)
public static IServiceCollection AddActivatedSingleton<TService, TImplementation>(this IServiceCollection services, Func<IServiceProvider, TImplementation> implementationFactory)
public static IServiceCollection AddActivatedSingleton<TService, TImplementation>(this IServiceCollection services)
public static IServiceCollection AddActivatedSingleton<TService>(this IServiceCollection services, Func<IServiceProvider, TService> implementationFactory)
public static IServiceCollection AddActivatedSingleton<TService>(this IServiceCollection services)
public static IServiceCollection AddActivatedSingleton(this IServiceCollection services, Type serviceType)
public static IServiceCollection AddActivatedSingleton(this IServiceCollection services, Type serviceType, Func<IServiceProvider, object> implementationFactory)
public static IServiceCollection AddActivatedSingleton(this IServiceCollection services, Type serviceType, Type implementationType)
public static void TryAddActivatedSingleton(this IServiceCollection services, Type serviceType)
public static void TryAddActivatedSingleton(this IServiceCollection services, Type serviceType, Type implementationType)
public static void TryAddActivatedSingleton(this IServiceCollection services, Type serviceType, Func<IServiceProvider, object> implementationFactory)
public static void TryAddActivatedSingleton<TService>(this IServiceCollection services)
public static void TryAddActivatedSingleton<TService, TImplementation>(this IServiceCollection services)
public static void TryAddActivatedSingleton<TService>(this IServiceCollection services, Func<IServiceProvider, TService> implementationFactory)
 
public static IServiceCollection ActivateKeyedSingleton<TService>(this IServiceCollection services, object? serviceKey)
public static IServiceCollection ActivateKeyedSingleton(this IServiceCollection services, Type serviceType, object? serviceKey)
public static IServiceCollection AddActivatedKeyedSingleton<TService, TImplementation>(this IServiceCollection services, object? serviceKey, Func<IServiceProvider, object?, TImplementation> implementationFactory)
public static IServiceCollection AddActivatedKeyedSingleton<TService, TImplementation>(this IServiceCollection services, object? serviceKey)
public static IServiceCollection AddActivatedKeyedSingleton<TService>(this IServiceCollection services, object? serviceKey, Func<IServiceProvider, object?, TService> implementationFactory)
public static IServiceCollection AddActivatedKeyedSingleton<TService>(this IServiceCollection services, object? serviceKey)
public static IServiceCollection AddActivatedKeyedSingleton(this IServiceCollection services, Type serviceType, object? serviceKey)
public static IServiceCollection AddActivatedKeyedSingleton(this IServiceCollection services, Type serviceType, object? serviceKey, Func<IServiceProvider, object?, object> implementationFactory)
public static IServiceCollection AddActivatedKeyedSingleton(this IServiceCollection services, Type serviceType, object? serviceKey, Type implementationType)
public static void TryAddActivatedKeyedSingleton(this IServiceCollection services, Type serviceType, object? serviceKey)
public static void TryAddActivatedKeyedSingleton(this IServiceCollection services, Type serviceType, object? serviceKey, Type implementationType)
public static void TryAddActivatedKeyedSingleton(this IServiceCollection services, Type serviceType, object? serviceKey, Func<IServiceProvider, object?, object> implementationFactory)
public static void TryAddActivatedKeyedSingleton<TService>(this IServiceCollection services, object? serviceKey)
public static void TryAddActivatedKeyedSingleton<TService, TImplementation>(this IServiceCollection services, object? serviceKey)
public static void TryAddActivatedKeyedSingleton<TService>(this IServiceCollection services, object? serviceKey, Func<IServiceProvider, object?, TService> implementationFactory)

For example:

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddActivatedSingleton<MyService>();

var app = builder.Build();

app.Run();

public class MyService
{
    public MyService()
    {
        Console.WriteLine("MyService is created");
    }
}

Result:

MyService is created
info: Microsoft.Hosting.Lifetime[14]
      Now listening on: http://localhost:5297
info: Microsoft.Hosting.Lifetime[0]
      Application started. Press Ctrl+C to shut down.

Services that are already registered can also be auto-activated:


builder.Services.AddSingleton<OtherService>();
// ...
builder.Services.ActivateSingleton<OtherService>();

Feedback & Contributing

We welcome feedback and contributions in our GitHub repo.

Showing the top 20 packages that depend on Microsoft.Extensions.DependencyInjection.AutoActivation.

Packages Downloads
Microsoft.Extensions.Http.Diagnostics
Telemetry support for HTTP Client.
123
Microsoft.Extensions.Http.Diagnostics
Telemetry support for HTTP Client.
126
Microsoft.Extensions.Http.Diagnostics
Telemetry support for HTTP Client.
133
Microsoft.Extensions.Http.Diagnostics
Telemetry support for HTTP Client.
135
Microsoft.Extensions.Http.Diagnostics
Telemetry support for HTTP Client.
141
Microsoft.Extensions.Http.Diagnostics
Telemetry support for HTTP Client.
143
Microsoft.Extensions.Http.Diagnostics
Telemetry support for HTTP Client.
144
Microsoft.Extensions.Telemetry
Provides canonical implementations of telemetry abstractions
125
Microsoft.Extensions.Telemetry
Provides canonical implementations of telemetry abstractions
132
Microsoft.Extensions.Telemetry
Provides canonical implementations of telemetry abstractions.
122
Microsoft.Extensions.Telemetry
Provides canonical implementations of telemetry abstractions.
123
Microsoft.Extensions.Telemetry
Provides canonical implementations of telemetry abstractions.
124
Microsoft.Extensions.Telemetry
Provides canonical implementations of telemetry abstractions.
125
Microsoft.Extensions.Telemetry
Provides canonical implementations of telemetry abstractions.
127
Microsoft.Extensions.Telemetry
Provides canonical implementations of telemetry abstractions.
128
Microsoft.Extensions.Telemetry
Provides canonical implementations of telemetry abstractions.
134
Microsoft.Extensions.Telemetry
Provides canonical implementations of telemetry abstractions.
140

Version Downloads Last updated
10.10.0 11 09/12/2026
10.9.0 19 08/15/2026
10.8.0 22 07/15/2026
10.7.0 25 06/11/2026
10.6.0 30 05/14/2026
10.5.0 35 04/15/2026
10.4.0 38 03/12/2026
10.3.0 42 02/10/2026
10.2.0 48 01/14/2026
10.1.0 59 12/11/2025
10.0.0 75 11/12/2025
9.10.0 76 10/15/2025
9.9.0 105 09/11/2025
9.8.0 86 08/14/2025
9.7.0 98 07/09/2025
9.6.0 125 06/12/2025
9.5.0 99 05/15/2025
9.4.0 111 04/09/2025
9.3.0 114 03/31/2025
9.2.0 115 04/01/2025
9.1.0 135 03/31/2025
9.0.0 110 03/31/2025
9.0.0-preview.9.24507.7 120 03/31/2025
9.0.0-preview.8.24460.1 99 04/01/2025
9.0.0-preview.7.24412.10 112 04/01/2025
9.0.0-preview.6.24353.1 109 03/31/2025
9.0.0-preview.5.24311.7 108 03/31/2025
9.0.0-preview.4.24271.2 106 03/31/2025
9.0.0-preview.3.24209.3 98 03/31/2025
9.0.0-preview.2.24157.4 109 03/31/2025
9.0.0-preview.1.24108.1 135 03/31/2025
8.10.0 102 03/31/2025
8.9.1 127 03/31/2025
8.9.0 111 03/31/2025
8.8.0 114 03/31/2025
8.7.0 128 04/01/2025
8.6.0 119 03/31/2025
8.5.0 130 03/31/2025
8.4.0 121 04/01/2025
8.3.0 116 03/31/2025
8.2.0 113 03/31/2025
8.1.0 116 03/31/2025
8.0.0 105 04/01/2025
8.0.0-rc.2.23510.2 118 03/31/2025
8.0.0-rc.1.23453.1 117 04/01/2025
8.0.0-preview.7.23407.5 116 03/31/2025
8.0.0-preview.6.23360.2 106 03/31/2025
8.0.0-preview.5.23308.3 114 04/01/2025
8.0.0-preview.4.23273.7 110 03/31/2025