Microsoft.Extensions.DependencyInjection.AutoActivation 8.4.0

The ID prefix of this package has been reserved for one of the owners of this package by NuGet.org. Prefix Reserved
There is a newer prerelease version of this package available.
See the version list below for details.
dotnet add package Microsoft.Extensions.DependencyInjection.AutoActivation --version 8.4.0
NuGet\Install-Package Microsoft.Extensions.DependencyInjection.AutoActivation -Version 8.4.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Microsoft.Extensions.DependencyInjection.AutoActivation" Version="8.4.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Microsoft.Extensions.DependencyInjection.AutoActivation --version 8.4.0
#r "nuget: Microsoft.Extensions.DependencyInjection.AutoActivation, 8.4.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Microsoft.Extensions.DependencyInjection.AutoActivation as a Cake Addin
#addin nuget:?package=Microsoft.Extensions.DependencyInjection.AutoActivation&version=8.4.0

// Install Microsoft.Extensions.DependencyInjection.AutoActivation as a Cake Tool
#tool nuget:?package=Microsoft.Extensions.DependencyInjection.AutoActivation&version=8.4.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.

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Framework net462 is compatible.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (7)

Showing the top 5 NuGet packages that depend on Microsoft.Extensions.DependencyInjection.AutoActivation:

Package Downloads
Microsoft.Extensions.Telemetry The ID prefix of this package has been reserved for one of the owners of this package by NuGet.org.

Provides canonical implementations of telemetry abstractions.

Microsoft.Extensions.Http.Diagnostics The ID prefix of this package has been reserved for one of the owners of this package by NuGet.org.

Telemetry support for HTTP Client.

Microsoft.Extensions.Diagnostics.ResourceMonitoring The ID prefix of this package has been reserved for one of the owners of this package by NuGet.org.

Measures processor and memory usage.

Microsoft.AspNetCore.Diagnostics.Middleware The ID prefix of this package has been reserved for one of the owners of this package by NuGet.org.

ASP.NET Core middleware for collecting high-quality telemetry.

Microsoft.Extensions.AsyncState The ID prefix of this package has been reserved for one of the owners of this package by NuGet.org.

Asynchronous feature store.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
9.0.0-preview.3.24209.3 2,397 4/11/2024
9.0.0-preview.2.24157.4 2,797 3/12/2024
9.0.0-preview.1.24108.1 1,919 2/13/2024
8.4.0 93,338 4/9/2024
8.3.0 127,447 3/12/2024
8.2.0 306,725 2/13/2024
8.1.0 210,941 1/9/2024
8.0.0 412,232 11/14/2023
8.0.0-rc.2.23510.2 4,936 10/10/2023
8.0.0-rc.1.23453.1 1,738 9/12/2023
8.0.0-preview.7.23407.5 1,162 8/8/2023
8.0.0-preview.6.23360.2 719 7/12/2023
8.0.0-preview.5.23308.3 2,593 6/14/2023
8.0.0-preview.4.23273.7 2,461 5/23/2023