-
Notifications
You must be signed in to change notification settings - Fork 298
/
LifecycleHookServiceCollectionExtensions.cs
55 lines (49 loc) · 3.02 KB
/
LifecycleHookServiceCollectionExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
namespace Aspire.Hosting.Lifecycle;
/// <summary>
/// Provides extension methods for adding lifecycle hooks to the <see cref="IServiceCollection"/>.
/// </summary>
public static class LifecycleHookServiceCollectionExtensions
{
/// <summary>
/// Adds a distributed application lifecycle hook to the service collection.
/// </summary>
/// <typeparam name="T">The type of the distributed application lifecycle hook to add.</typeparam>
/// <param name="services">The <see cref="IServiceCollection"/> to add the distributed application lifecycle hook to.</param>
public static void AddLifecycleHook<T>(this IServiceCollection services) where T : class, IDistributedApplicationLifecycleHook
{
services.AddSingleton<IDistributedApplicationLifecycleHook, T>();
}
/// <summary>
/// Attempts to add a distributed application lifecycle hook to the service collection.
/// </summary>
/// <typeparam name="T">The type of the distributed application lifecycle hook to add.</typeparam>
/// <param name="services">The <see cref="IServiceCollection"/> to add the distributed application lifecycle hook to.</param>
public static void TryAddLifecycleHook<T>(this IServiceCollection services) where T : class, IDistributedApplicationLifecycleHook
{
services.TryAddEnumerable(ServiceDescriptor.Singleton<IDistributedApplicationLifecycleHook, T>());
}
/// <summary>
/// Adds a distributed application lifecycle hook to the service collection.
/// </summary>
/// <typeparam name="T">The type of the distributed application lifecycle hook.</typeparam>
/// <param name="services">The service collection to add the hook to.</param>
/// <param name="implementationFactory">A factory function that creates the hook implementation.</param>
public static void AddLifecycleHook<T>(this IServiceCollection services, Func<IServiceProvider, T> implementationFactory) where T : class, IDistributedApplicationLifecycleHook
{
services.AddSingleton<IDistributedApplicationLifecycleHook, T>(implementationFactory);
}
/// <summary>
/// Attempts to add a distributed application lifecycle hook to the service collection.
/// </summary>
/// <typeparam name="T">The type of the distributed application lifecycle hook.</typeparam>
/// <param name="services">The service collection to add the hook to.</param>
/// <param name="implementationFactory">A factory function that creates the hook implementation.</param>
public static void TryAddLifecycleHook<T>(this IServiceCollection services, Func<IServiceProvider, T> implementationFactory) where T : class, IDistributedApplicationLifecycleHook
{
services.TryAddEnumerable(ServiceDescriptor.Singleton<IDistributedApplicationLifecycleHook, T>(implementationFactory));
}
}