Skip to content

[API Proposal]: Startup activation of singletons #86075

@sebastienros

Description

@sebastienros

Background and motivation

Currently, singleton dependencies are resolved at runtime when they are first requested. This could be an issue for response times and one might want them to be ready when the application starts. Another scenario is when the creation of a singleton should not block multiple requests that could be executed in parallel and be blocked (thundering herd).

The proposal is to create new extension methods that will allow to register auto-activated dependencies. Internally it would use a specifically crafted IHostedService that would resolve them preemptively.

API Proposal

namespace Microsoft.Extensions.DependencyInjection;

public static class AutoActivationExtensions
{
    public static IServiceCollection Activate<TService>(this IServiceCollection services)
        where TService : class
        
    public static IServiceCollection AddActivatedSingleton<TService, TImplementation>(this IServiceCollection services, Func<IServiceProvider, TImplementation> implementationFactory)
        where TService : class
        where TImplementation : class, TService

    public static IServiceCollection AddActivatedSingleton<TService>(this IServiceCollection services, Func<IServiceProvider, TService> implementationFactory)
        where TService : class

    public static IServiceCollection AddActivatedSingleton<TService>(this IServiceCollection services)
        where TService : class

    public static IServiceCollection AddActivatedSingleton(this IServiceCollection services, Type serviceType)

    public static IServiceCollection AddActivatedSingleton<TService, TImplementation>(this IServiceCollection services)
        where TService : class
        where TImplementation : class, TService

    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)
        where TService : class

    public static void TryAddActivatedSingleton<TService, TImplementation>(this IServiceCollection services)
        where TService : class
        where TImplementation : class, TService

    public static void TryAddActivatedSingleton<TService>(this IServiceCollection services, Func<IServiceProvider, TService> implementationFactory)
        where TService : class
}

API Usage

// Registers and auto-activates a class
builder.Services.AddActivatedSingleton<WindowsCounters>();

// Auto-activates a pre-registered singleton. If the service is not registered, System.InvalidOperationException is thrown.
builder.Services.AddSingleton<IFakeService, FakeService>().Activate<IFakeService>());

Alternative Designs

User can already get this behavior by using available components: IHostedService.

Risks

No response

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions