Code
·
62 lines
·
1592 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
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
56
57
58
59
60
61
62using Microsoft.Extensions.Hosting;
namespace Microsoft.Extensions.DependencyInjection;
/// <summary>
/// Wrapper for an IHostedService, designed for session-based execution.
/// This wrapper ensures that a hosted service can be started and stopped
/// based on the lifecycle of a session, providing greater control over
/// session-specific tasks. It integrates seamlessly with session management
/// by allowing services to be dynamically tied to a session's lifetime.
/// </summary>
public class SessionScopedWrapper(IHostedService hostedService)
{
/// <summary>
/// The hosted service that is being wrapped.
/// </summary>
public readonly IHostedService hostedService = hostedService;
/// <summary>
/// A CancellationTokenSource used to cancel the task.
/// </summary>
private CancellationTokenSource? cts;
/// <summary>
/// The task that is running for the hosted service.
/// </summary>
private Task? runningTask;
/// <summary>
/// Starts the hosted service.
/// </summary>
public void Start()
{
cts = new CancellationTokenSource();
runningTask = hostedService.StartAsync(cts.Token);
}
/// <summary>
/// Stops the hosted service and cancels the task.
/// </summary>
/// <returns>A task representing the asynchronous operation.</returns>
public async Task StopAsync()
{
if (cts != null)
{
cts.Cancel();
try
{
await hostedService.StopAsync(cts.Token);
if (runningTask != null)
await runningTask;
}
catch (AggregateException)
{
// Suppress exceptions caused by task cancellation
}
cts.Dispose();
}
}
}