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