-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathResolveInScope.cs
More file actions
29 lines (24 loc) · 874 Bytes
/
ResolveInScope.cs
File metadata and controls
29 lines (24 loc) · 874 Bytes
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
using BetterHostedServices;
using Microsoft.Extensions.DependencyInjection;
namespace Utils
{
/// <summary>
/// The class will resolve underlying <see cref="CriticalBackgroundService"/> in scope
/// </summary>
/// <typeparam name="T">Wrapped <see cref="CriticalBackgroundService"/></typeparam>
public class ResolveInScope<T> : NotEndingBackgroundService
where T : CriticalBackgroundService
{
private readonly IServiceProvider provider;
public ResolveInScope(IServiceProvider provider)
{
this.provider = provider;
}
protected override async Task ExecuteAsync(CancellationToken token)
{
using IServiceScope scope = provider.CreateScope();
T worker = ActivatorUtilities.CreateInstance<T>(provider);
await worker.StartAsync(token);
}
}
}