-
Notifications
You must be signed in to change notification settings - Fork 103
/
Copy pathServiceManagerBuilder.cs
188 lines (170 loc) · 7.35 KB
/
ServiceManagerBuilder.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
// 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.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using Microsoft.AspNetCore.SignalR.Protocol;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Logging;
namespace Microsoft.Azure.SignalR.Management
{
/// <summary>
/// A builder for configuring <see cref="ServiceManager"/> instances.
/// </summary>
#pragma warning disable CS0618 // Type or member is obsolete
public class ServiceManagerBuilder : IServiceManagerBuilder
#pragma warning restore CS0618 // Type or member is obsolete
{
private readonly IServiceCollection _services;
private Action<IServiceCollection> _configureAction;
internal ServiceManagerBuilder(IServiceCollection services)
{
_services = services;
}
public ServiceManagerBuilder() : this(new ServiceCollection())
{
_services.AddSignalRServiceManager();
}
/// <summary>
/// Registers an action used to configure <see cref="ServiceManager"/>.
/// </summary>
/// <param name="configure">A callback to configure the <see cref="ServiceManager"/>.</param>
/// <returns>The same instance of the <see cref="ServiceManagerBuilder"/> for chaining.</returns>
public ServiceManagerBuilder WithOptions(Action<ServiceManagerOptions> configure)
{
_services.Configure(configure);
return this;
}
public ServiceManagerBuilder WithLoggerFactory(ILoggerFactory loggerFactory)
{
_services.AddSingleton(loggerFactory);
return this;
}
public ServiceManagerBuilder WithConfiguration(IConfiguration configuration)
{
_services.AddSingleton(configuration);
return this;
}
public ServiceManagerBuilder WithRouter(IEndpointRouter router)
{
_services.AddSingleton(router);
return this;
}
/// <summary>
/// Uses Newtonsoft.Json library to serialize messages sent to SignalR.
/// </summary>
/// <param name="configure">A delegate that can be used to configure the <see cref="NewtonsoftServiceHubProtocolOptions"/>.</param>
/// <returns>The <see cref="ServiceManagerBuilder"/> instance itself.</returns>
public ServiceManagerBuilder WithNewtonsoftJson(Action<NewtonsoftServiceHubProtocolOptions> configure)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(configure);
#else
if (configure is null)
{
throw new ArgumentNullException(nameof(configure));
}
#endif
_services.AddNewtonsoftHubProtocol(configure);
return this;
}
/// <summary>
/// Uses Newtonsoft.Json library to serialize messages sent to SignalR clients.
/// </summary>
/// <returns>The <see cref="ServiceHubContextBuilder"/> instance itself.</returns>
public ServiceManagerBuilder WithNewtonsoftJson()
{
return WithNewtonsoftJson(o => { });
}
/// <summary>
/// Sets the SignalR hub protocols to serialize messages sent to SignalR clients.
/// </summary>
/// <remarks>Calling this method first clears the existing hub protocols, then adds the new protocols.</remarks>
/// <param name="hubProtocols">Only the protocols named "json" or "messagepack" are allowed.</param>
/// <returns>The <see cref="ServiceHubContextBuilder"/> instance itself.</returns>
public ServiceManagerBuilder WithHubProtocols(params IHubProtocol[] hubProtocols)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(hubProtocols);
#else
if (hubProtocols == null)
{
throw new ArgumentNullException(nameof(hubProtocols));
}
#endif
// Allows the user to use MessagePack only.
_services.RemoveAll<IHubProtocol>();
foreach (var hubProtocol in hubProtocols)
{
AddHubProtocol(hubProtocol);
}
return this;
}
/// <summary>
/// Add a SignalR hub protocol to serialize messages sent to SignalR clients.
/// </summary>
/// <param name="hubProtocol">Only the protocol named "json" or "messagepack" is allowed.</param>
/// <returns>The <see cref="ServiceHubContextBuilder"/> instance itself.</returns>
public ServiceManagerBuilder AddHubProtocol(IHubProtocol hubProtocol)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(hubProtocol);
#else
if (hubProtocol == null)
{
throw new ArgumentNullException(nameof(hubProtocol));
}
#endif
if (!hubProtocol.Name.Equals(Constants.Protocol.Json, StringComparison.OrdinalIgnoreCase) && !hubProtocol.Name.Equals(Constants.Protocol.MessagePack, StringComparison.OrdinalIgnoreCase))
{
throw new ArgumentException($"The name '{hubProtocol.Name}' of the hub protocol is not supported. Only '{Constants.Protocol.Json}' or '{Constants.Protocol.MessagePack}' is allowed.");
}
// If there are duplicate hub protocols with the same name, the last one added works.
_services.AddSingleton(hubProtocol);
return this;
}
[EditorBrowsable(EditorBrowsableState.Never)]
public ServiceManagerBuilder WithCallingAssembly()
{
var assembly = Assembly.GetCallingAssembly();
_services.WithAssembly(assembly);
return this;
}
[EditorBrowsable(EditorBrowsableState.Never)]
public ServiceManagerBuilder AddUserAgent(string userAgent)
{
_services.AddUserAgent(userAgent);
return this;
}
internal ServiceManagerBuilder ConfigureServices(Action<IServiceCollection> configureAction)
{
_configureAction = configureAction;
return this;
}
/// <summary>
/// Builds <see cref="IServiceManager"/> instances.
/// </summary>
/// <returns>The instance of the <see cref="IServiceManager"/>.</returns>
[Obsolete("Use BuildServiceManager() instead. See https://github.com/Azure/azure-signalr/blob/dev/docs/management-sdk-migration.md for migration guide.")]
public IServiceManager Build()
{
return (IServiceManager)BuildServiceManager();
}
/// <summary>
/// Builds <see cref="ServiceManager"/> instances.
/// </summary>
/// <returns>The instance of the <see cref="ServiceManager"/>.</returns>
public ServiceManager BuildServiceManager()
{
var serviceCollection = new ServiceCollection().Add(_services);
_configureAction?.Invoke(serviceCollection);
serviceCollection.AddSingleton(serviceCollection.ToList() as IReadOnlyCollection<ServiceDescriptor>);
return serviceCollection.BuildServiceProvider()
.GetRequiredService<IServiceManager>() as ServiceManager;
}
}
}