Skip to content

Commit b00aa5f

Browse files
authored
Auto activation readme (#4681)
* Add auto-activation README * Typos * Activate
1 parent 9104562 commit b00aa5f

File tree

3 files changed

+85
-5
lines changed

3 files changed

+85
-5
lines changed

src/Libraries/Microsoft.Extensions.DependencyInjection.AutoActivation/AutoActivationExtensions.Keyed.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace Microsoft.Extensions.DependencyInjection;
1212
public static partial class AutoActivationExtensions
1313
{
1414
/// <summary>
15-
/// Enforces keyed singleton activation at startup time rather then at runtime.
15+
/// Enforces keyed singleton activation at startup time rather than at runtime.
1616
/// </summary>
1717
/// <typeparam name="TService">The type of the service to activate.</typeparam>
1818
/// <param name="services">The service collection containing the service.</param>
@@ -49,7 +49,7 @@ public static IServiceCollection ActivateKeyedSingleton<TService>(
4949
}
5050

5151
/// <summary>
52-
/// Enforces keyed singleton activation at startup time rather then at runtime.
52+
/// Enforces keyed singleton activation at startup time rather than at runtime.
5353
/// </summary>
5454
/// <param name="services">The service collection to add the service to.</param>
5555
/// <param name="serviceType">The type of the service to activate.</param>

src/Libraries/Microsoft.Extensions.DependencyInjection.AutoActivation/AutoActivationExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ namespace Microsoft.Extensions.DependencyInjection;
1616
public static partial class AutoActivationExtensions
1717
{
1818
/// <summary>
19-
/// Enforces singleton activation at startup time rather then at runtime.
19+
/// Enforces singleton activation at startup time rather than at runtime.
2020
/// </summary>
2121
/// <typeparam name="TService">The type of the service to activate.</typeparam>
2222
/// <param name="services">The service collection containing the service.</param>
@@ -50,7 +50,7 @@ public static IServiceCollection ActivateSingleton<TService>(this IServiceCollec
5050
}
5151

5252
/// <summary>
53-
/// Enforces singleton activation at startup time rather then at runtime.
53+
/// Enforces singleton activation at startup time rather than at runtime.
5454
/// </summary>
5555
/// <param name="services">The service collection containing the service.</param>
5656
/// <param name="serviceType">The type of the service to activate.</param>

src/Libraries/Microsoft.Extensions.DependencyInjection.AutoActivation/README.md

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# Microsoft.Extensions.DependencyInjection.AutoActivation
22

3-
Extensions to auto-activate registered singletons in the dependency injection system.
3+
This provides the ability to instantiate registered singletons during startup instead of during the first time it is used.
4+
5+
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.
46

57
## Install the package
68

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

23+
## Usage Example
24+
25+
### Registering Services
26+
27+
The services to auto-activate can be registered using the following methods:
28+
29+
```csharp
30+
static IServiceCollection ActivateSingleton<TService>(this IServiceCollection services);
31+
static IServiceCollection ActivateSingleton(this IServiceCollection services, Type serviceType);
32+
static IServiceCollection AddActivatedSingleton<TService, TImplementation>(this IServiceCollection services, Func<IServiceProvider, TImplementation> implementationFactory);
33+
static IServiceCollection AddActivatedSingleton<TService, TImplementation>(this IServiceCollection services);
34+
static IServiceCollection AddActivatedSingleton<TService>(this IServiceCollection services, Func<IServiceProvider, TService> implementationFactory);
35+
static IServiceCollection AddActivatedSingleton<TService>(this IServiceCollection services);
36+
static IServiceCollection AddActivatedSingleton(this IServiceCollection services, Type serviceType);
37+
static IServiceCollection AddActivatedSingleton(this IServiceCollection services, Type serviceType, Func<IServiceProvider, object> implementationFactory);
38+
static IServiceCollection AddActivatedSingleton(this IServiceCollection services, Type serviceType, Type implementationType);
39+
static void TryAddActivatedSingleton(this IServiceCollection services, Type serviceType);
40+
static void TryAddActivatedSingleton(this IServiceCollection services, Type serviceType, Type implementationType);
41+
static void TryAddActivatedSingleton(this IServiceCollection services, Type serviceType, Func<IServiceProvider, object> implementationFactory);
42+
static void TryAddActivatedSingleton<TService>(this IServiceCollection services);
43+
static void TryAddActivatedSingleton<TService, TImplementation>(this IServiceCollection services);
44+
static void TryAddActivatedSingleton<TService>(this IServiceCollection services, Func<IServiceProvider, TService> implementationFactory);
45+
46+
static IServiceCollection ActivateKeyedSingleton<TService>(this IServiceCollection services, object? serviceKey);
47+
static IServiceCollection ActivateKeyedSingleton(this IServiceCollection services, Type serviceType, object? serviceKey);
48+
static IServiceCollection AddActivatedKeyedSingleton<TService, TImplementation>(this IServiceCollection services, object? serviceKey, Func<IServiceProvider, object?, TImplementation> implementationFactory);
49+
static IServiceCollection AddActivatedKeyedSingleton<TService, TImplementation>(this IServiceCollection services, object? serviceKey);
50+
static IServiceCollection AddActivatedKeyedSingleton<TService>(this IServiceCollection services, object? serviceKey, Func<IServiceProvider, object?, TService> implementationFactory);
51+
static IServiceCollection AddActivatedKeyedSingleton<TService>(this IServiceCollection services, object? serviceKey);
52+
static IServiceCollection AddActivatedKeyedSingleton(this IServiceCollection services, Type serviceType, object? serviceKey);
53+
static IServiceCollection AddActivatedKeyedSingleton(this IServiceCollection services, Type serviceType, object? serviceKey, Func<IServiceProvider, object?, object> implementationFactory);
54+
static IServiceCollection AddActivatedKeyedSingleton(this IServiceCollection services, Type serviceType, object? serviceKey, Type implementationType);
55+
static void TryAddActivatedKeyedSingleton(this IServiceCollection services, Type serviceType, object? serviceKey);
56+
static void TryAddActivatedKeyedSingleton(this IServiceCollection services, Type serviceType, object? serviceKey, Type implementationType);
57+
static void TryAddActivatedKeyedSingleton(this IServiceCollection services, Type serviceType, object? serviceKey, Func<IServiceProvider, object?, object> implementationFactory);
58+
static void TryAddActivatedKeyedSingleton<TService>(this IServiceCollection services, object? serviceKey);
59+
static void TryAddActivatedKeyedSingleton<TService, TImplementation>(this IServiceCollection services, object? serviceKey);
60+
TryAddActivatedKeyedSingleton<TService>(this IServiceCollection services, object? serviceKey, Func<IServiceProvider, object?, TService> implementationFactory)
61+
```
62+
63+
For example:
64+
65+
```csharp
66+
var builder = WebApplication.CreateBuilder(args);
67+
68+
builder.Services.AddActivatedSingleton<MyService>();
69+
70+
var app = builder.Build();
71+
72+
app.Run();
73+
74+
public class MyService
75+
{
76+
public MyService()
77+
{
78+
Console.WriteLine("MyService is created");
79+
}
80+
}
81+
```
82+
83+
Result:
84+
85+
```
86+
MyService is created
87+
info: Microsoft.Hosting.Lifetime[14]
88+
Now listening on: http://localhost:5297
89+
info: Microsoft.Hosting.Lifetime[0]
90+
Application started. Press Ctrl+C to shut down.
91+
```
92+
93+
Services that are already registered can also be auto-activated:
94+
95+
```csharp
96+
97+
builder.Services.AddSingleton<OtherService>();
98+
// ...
99+
builder.Services.ActivateSingleton<OtherService>();
100+
```
21101

22102
## Feedback & Contributing
23103

0 commit comments

Comments
 (0)