Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auto activation readme #4681

Merged
merged 3 commits into from
Nov 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace Microsoft.Extensions.DependencyInjection;
public static partial class AutoActivationExtensions
{
/// <summary>
/// Enforces keyed singleton activation at startup time rather then at runtime.
/// Enforces keyed singleton activation at startup time rather than at runtime.
/// </summary>
/// <typeparam name="TService">The type of the service to activate.</typeparam>
/// <param name="services">The service collection containing the service.</param>
Expand Down Expand Up @@ -49,7 +49,7 @@ public static IServiceCollection ActivateKeyedSingleton<TService>(
}

/// <summary>
/// Enforces keyed singleton activation at startup time rather then at runtime.
/// Enforces keyed singleton activation at startup time rather than at runtime.
/// </summary>
/// <param name="services">The service collection to add the service to.</param>
/// <param name="serviceType">The type of the service to activate.</param>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace Microsoft.Extensions.DependencyInjection;
public static partial class AutoActivationExtensions
{
/// <summary>
/// Enforces singleton activation at startup time rather then at runtime.
/// Enforces singleton activation at startup time rather than at runtime.
/// </summary>
/// <typeparam name="TService">The type of the service to activate.</typeparam>
/// <param name="services">The service collection containing the service.</param>
Expand Down Expand Up @@ -50,7 +50,7 @@ public static IServiceCollection ActivateSingleton<TService>(this IServiceCollec
}

/// <summary>
/// Enforces singleton activation at startup time rather then at runtime.
/// Enforces singleton activation at startup time rather than at runtime.
/// </summary>
/// <param name="services">The service collection containing the service.</param>
/// <param name="serviceType">The type of the service to activate.</param>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Microsoft.Extensions.DependencyInjection.AutoActivation

Extensions to auto-activate registered singletons in the dependency injection system.
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

Expand All @@ -18,6 +20,84 @@ Or directly in the C# project file:
</ItemGroup>
```

## Usage Example

### Registering Services

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

```csharp
static IServiceCollection ActivateSingleton<TService>(this IServiceCollection services);
static IServiceCollection ActivateSingleton(this IServiceCollection services, Type serviceType);
static IServiceCollection AddActivatedSingleton<TService, TImplementation>(this IServiceCollection services, Func<IServiceProvider, TImplementation> implementationFactory);
static IServiceCollection AddActivatedSingleton<TService, TImplementation>(this IServiceCollection services);
static IServiceCollection AddActivatedSingleton<TService>(this IServiceCollection services, Func<IServiceProvider, TService> implementationFactory);
static IServiceCollection AddActivatedSingleton<TService>(this IServiceCollection services);
static IServiceCollection AddActivatedSingleton(this IServiceCollection services, Type serviceType);
static IServiceCollection AddActivatedSingleton(this IServiceCollection services, Type serviceType, Func<IServiceProvider, object> implementationFactory);
static IServiceCollection AddActivatedSingleton(this IServiceCollection services, Type serviceType, Type implementationType);
static void TryAddActivatedSingleton(this IServiceCollection services, Type serviceType);
static void TryAddActivatedSingleton(this IServiceCollection services, Type serviceType, Type implementationType);
static void TryAddActivatedSingleton(this IServiceCollection services, Type serviceType, Func<IServiceProvider, object> implementationFactory);
static void TryAddActivatedSingleton<TService>(this IServiceCollection services);
static void TryAddActivatedSingleton<TService, TImplementation>(this IServiceCollection services);
static void TryAddActivatedSingleton<TService>(this IServiceCollection services, Func<IServiceProvider, TService> implementationFactory);

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

For example:

```csharp
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:

```csharp

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

## Feedback & Contributing

Expand Down