-
-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathGatewayClientHostedService.cs
More file actions
41 lines (31 loc) · 1.38 KB
/
GatewayClientHostedService.cs
File metadata and controls
41 lines (31 loc) · 1.38 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
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Options;
using NetCord.Gateway;
namespace NetCord.Hosting.Gateway;
internal partial class GatewayClientHostedService(IServiceProvider services) : IHostedService
{
public Task StartAsync(CancellationToken cancellationToken)
{
var client = services.GetRequiredService<GatewayClient>();
foreach (var handler in services.GetServices<IGatewayHandler>())
{
if (handler is IDelegateGatewayHandlerBase delegateHandler)
RegisterDelegateHandler(client, delegateHandler);
else
RegisterClassHandler(client, handler);
}
var options = services.GetRequiredService<IOptions<GatewayClientOptions>>().Value;
return options.AutoStartStop.GetValueOrDefault(true)
? client.StartAsync(cancellationToken: cancellationToken).AsTask()
: Task.CompletedTask;
}
public Task StopAsync(CancellationToken cancellationToken)
{
var options = services.GetRequiredService<IOptions<GatewayClientOptions>>().Value;
if (!options.AutoStartStop.GetValueOrDefault(true))
return Task.CompletedTask;
var client = services.GetRequiredService<GatewayClient>();
return client.CloseAsync(cancellationToken: cancellationToken).AsTask();
}
}