-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDIUtils.cs
More file actions
43 lines (38 loc) · 1.45 KB
/
DIUtils.cs
File metadata and controls
43 lines (38 loc) · 1.45 KB
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
using BetterHostedServices;
using Microsoft.Extensions.DependencyInjection;
namespace Utils
{
public static class DIUtils
{
/// <summary>
/// Register service both as <see cref="TType"/> and <see cref="TImplementation"/>
/// </summary>
/// <param name="services">Specified ServiceCollection to add</param>
/// <typeparam name="TType">Interface/abstract class type</typeparam>
/// <typeparam name="TImplementation">Implementation type</typeparam>
/// <returns>ServiceCollection to chaining</returns>
public static IServiceCollection AddTypeAndImplementation<TType, TImplementation>(
this IServiceCollection services)
where TImplementation : class, TType
where TType : class
=>
services.AddTransient<TType, TImplementation>().AddTransient<TImplementation>();
}
/// <summary>
/// CriticalBackgroundService without app ending behavior
/// </summary>
public abstract class NotEndingBackgroundService : CriticalBackgroundService
{
private static readonly FalseApplicationEnder ender = new();
public NotEndingBackgroundService() : base(ender)
{
}
protected abstract override Task ExecuteAsync(CancellationToken token);
private class FalseApplicationEnder : IApplicationEnder
{
public void ShutDownApplication()
{
}
}
}
}