1
1
# Microsoft.Extensions.DependencyInjection.AutoActivation
2
2
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.
4
6
5
7
## Install the package
6
8
7
9
From the command-line:
8
10
9
- ``` dotnetcli
11
+ ``` console
10
12
dotnet add package Microsoft.Extensions.DependencyInjection.AutoActivation
11
13
```
12
14
@@ -18,6 +20,84 @@ Or directly in the C# project file:
18
20
</ItemGroup >
19
21
```
20
22
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
+ public static IServiceCollection ActivateSingleton <TService >(this IServiceCollection services )
31
+ public static IServiceCollection ActivateSingleton (this IServiceCollection services , Type serviceType )
32
+ public static IServiceCollection AddActivatedSingleton <TService , TImplementation >(this IServiceCollection services , Func < IServiceProvider , TImplementation > implementationFactory )
33
+ public static IServiceCollection AddActivatedSingleton <TService , TImplementation >(this IServiceCollection services )
34
+ public static IServiceCollection AddActivatedSingleton <TService >(this IServiceCollection services , Func < IServiceProvider , TService > implementationFactory )
35
+ public static IServiceCollection AddActivatedSingleton <TService >(this IServiceCollection services )
36
+ public static IServiceCollection AddActivatedSingleton (this IServiceCollection services , Type serviceType )
37
+ public static IServiceCollection AddActivatedSingleton (this IServiceCollection services , Type serviceType , Func < IServiceProvider , object > implementationFactory )
38
+ public static IServiceCollection AddActivatedSingleton (this IServiceCollection services , Type serviceType , Type implementationType )
39
+ public static void TryAddActivatedSingleton (this IServiceCollection services , Type serviceType )
40
+ public static void TryAddActivatedSingleton (this IServiceCollection services , Type serviceType , Type implementationType )
41
+ public static void TryAddActivatedSingleton (this IServiceCollection services , Type serviceType , Func < IServiceProvider , object > implementationFactory )
42
+ public static void TryAddActivatedSingleton <TService >(this IServiceCollection services )
43
+ public static void TryAddActivatedSingleton <TService , TImplementation >(this IServiceCollection services )
44
+ public static void TryAddActivatedSingleton <TService >(this IServiceCollection services , Func < IServiceProvider , TService > implementationFactory )
45
+
46
+ public static IServiceCollection ActivateKeyedSingleton <TService >(this IServiceCollection services , object ? serviceKey )
47
+ public static IServiceCollection ActivateKeyedSingleton (this IServiceCollection services , Type serviceType , object ? serviceKey )
48
+ public static IServiceCollection AddActivatedKeyedSingleton <TService , TImplementation >(this IServiceCollection services , object ? serviceKey , Func < IServiceProvider , object ? , TImplementation > implementationFactory )
49
+ public static IServiceCollection AddActivatedKeyedSingleton <TService , TImplementation >(this IServiceCollection services , object ? serviceKey )
50
+ public static IServiceCollection AddActivatedKeyedSingleton <TService >(this IServiceCollection services , object ? serviceKey , Func < IServiceProvider , object ? , TService > implementationFactory )
51
+ public static IServiceCollection AddActivatedKeyedSingleton <TService >(this IServiceCollection services , object ? serviceKey )
52
+ public static IServiceCollection AddActivatedKeyedSingleton (this IServiceCollection services , Type serviceType , object ? serviceKey )
53
+ public static IServiceCollection AddActivatedKeyedSingleton (this IServiceCollection services , Type serviceType , object ? serviceKey , Func < IServiceProvider , object ? , object > implementationFactory )
54
+ public static IServiceCollection AddActivatedKeyedSingleton (this IServiceCollection services , Type serviceType , object ? serviceKey , Type implementationType )
55
+ public static void TryAddActivatedKeyedSingleton (this IServiceCollection services , Type serviceType , object ? serviceKey )
56
+ public static void TryAddActivatedKeyedSingleton (this IServiceCollection services , Type serviceType , object ? serviceKey , Type implementationType )
57
+ public static void TryAddActivatedKeyedSingleton (this IServiceCollection services , Type serviceType , object ? serviceKey , Func < IServiceProvider , object ? , object > implementationFactory )
58
+ public static void TryAddActivatedKeyedSingleton <TService >(this IServiceCollection services , object ? serviceKey )
59
+ public static void TryAddActivatedKeyedSingleton <TService , TImplementation >(this IServiceCollection services , object ? serviceKey )
60
+ public static void 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
+ ```
21
101
22
102
## Feedback & Contributing
23
103
0 commit comments