-
Notifications
You must be signed in to change notification settings - Fork 387
/
Copy pathSignalRService.cs
54 lines (47 loc) · 2.07 KB
/
SignalRService.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
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Azure.SignalR.Management;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
namespace NegotiationServer
{
public interface IHubContextStore
{
public ServiceHubContext HubContext { get; }
public ServiceHubContext<IMessageClient> StronglyTypedHubContext { get; }
}
public class SignalRService : IHostedService, IHubContextStore
{
private const string StronglyTypedHub = "StronglyTypedHub";
private const string Hub = "Hub";
private readonly IConfiguration _configuration;
private readonly ILoggerFactory _loggerFactory;
public ServiceHubContext HubContext { get; private set; }
public ServiceHubContext<IMessageClient> StronglyTypedHubContext { get; private set; }
public SignalRService(IConfiguration configuration, ILoggerFactory loggerFactory)
{
_configuration = configuration;
_loggerFactory = loggerFactory;
}
async Task IHostedService.StartAsync(CancellationToken cancellationToken)
{
using var serviceManager = new ServiceManagerBuilder()
.WithConfiguration(_configuration)
//or .WithOptions(o=>o.ConnectionString = _configuration["Azure:SignalR:ConnectionString"]
.WithLoggerFactory(_loggerFactory)
.BuildServiceManager();
HubContext = await serviceManager.CreateHubContextAsync(Hub, cancellationToken);
StronglyTypedHubContext = await serviceManager.CreateHubContextAsync<IMessageClient>(StronglyTypedHub, cancellationToken);
}
Task IHostedService.StopAsync(CancellationToken cancellationToken)
{
HubContext.Dispose();
StronglyTypedHubContext.Dispose();
return Task.CompletedTask;
}
}
}