|
2 | 2 | // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
3 | 3 |
|
4 | 4 | using System;
|
5 |
| -using System.Collections.Generic; |
6 | 5 | using System.ComponentModel;
|
| 6 | +using System.Linq; |
7 | 7 | using Microsoft.AspNetCore.Connections;
|
8 | 8 | using Microsoft.AspNetCore.SignalR.Internal.Protocol;
|
9 |
| -using Newtonsoft.Json; |
| 9 | +using Microsoft.Extensions.DependencyInjection; |
| 10 | +using Microsoft.Extensions.DependencyInjection.Extensions; |
| 11 | +using Microsoft.Extensions.Logging; |
| 12 | +using Microsoft.Extensions.Logging.Abstractions; |
10 | 13 |
|
11 | 14 | namespace Microsoft.AspNetCore.SignalR.Client
|
12 | 15 | {
|
13 | 16 | public class HubConnectionBuilder : IHubConnectionBuilder
|
14 | 17 | {
|
15 |
| - private readonly Dictionary<KeyValuePair<string, Type>, object> _settings = new Dictionary<KeyValuePair<string, Type>, object>(); |
16 |
| - private Func<IConnection> _connectionFactoryDelegate; |
| 18 | + private bool _hubConnectionBuilt; |
17 | 19 |
|
18 |
| - public void ConfigureConnectionFactory(Func<IConnection> connectionFactoryDelegate) => |
19 |
| - _connectionFactoryDelegate = connectionFactoryDelegate; |
| 20 | + public IServiceCollection Services { get; } |
20 | 21 |
|
21 |
| - public void AddSetting<T>(string name, T value) |
| 22 | + public HubConnectionBuilder() |
22 | 23 | {
|
23 |
| - _settings[new KeyValuePair<string, Type>(name, typeof(T))] = value; |
| 24 | + Services = new ServiceCollection(); |
| 25 | + Services.AddSingleton<ILoggerFactory>(NullLoggerFactory.Instance); |
| 26 | + Services.AddSingleton<IHubProtocol, JsonHubProtocol>(); |
| 27 | + Services.AddSingleton<HubConnection>(); |
24 | 28 | }
|
25 | 29 |
|
26 |
| - public bool TryGetSetting<T>(string name, out T value) |
| 30 | + public HubConnection Build() |
27 | 31 | {
|
28 |
| - value = default; |
29 |
| - if (!_settings.TryGetValue(new KeyValuePair<string, Type>(name, typeof(T)), out var setting)) |
| 32 | + // Build can only be used once |
| 33 | + if (_hubConnectionBuilt) |
30 | 34 | {
|
31 |
| - return false; |
| 35 | + throw new InvalidOperationException("HubConnectionBuilder allows creation only of a single instance of HubConnection."); |
32 | 36 | }
|
33 | 37 |
|
34 |
| - value = (T)setting; |
35 |
| - return true; |
36 |
| - } |
| 38 | + _hubConnectionBuilt = true; |
37 | 39 |
|
38 |
| - public HubConnection Build() |
39 |
| - { |
40 |
| - if (_connectionFactoryDelegate == null) |
| 40 | + // The service provider is disposed by the HubConnection |
| 41 | + var serviceProvider = Services.BuildServiceProvider(); |
| 42 | + |
| 43 | + var connectionFactory = serviceProvider.GetService<Func<IConnection>>(); |
| 44 | + if (connectionFactory == null) |
41 | 45 | {
|
42 |
| - throw new InvalidOperationException("Cannot create IConnection instance. The connection factory was not configured."); |
| 46 | + throw new InvalidOperationException("Cannot create HubConnection instance. A connection was not configured."); |
43 | 47 | }
|
44 | 48 |
|
45 |
| - IHubConnectionBuilder builder = this; |
46 |
| - |
47 |
| - var loggerFactory = builder.GetLoggerFactory(); |
48 |
| - var hubProtocol = builder.GetHubProtocol(); |
49 |
| - |
50 |
| - return new HubConnection(_connectionFactoryDelegate, hubProtocol ?? new JsonHubProtocol(), loggerFactory); |
| 49 | + return serviceProvider.GetService<HubConnection>(); |
51 | 50 | }
|
52 | 51 |
|
| 52 | + // Prevents from being displayed in intellisense |
53 | 53 | [EditorBrowsable(EditorBrowsableState.Never)]
|
54 | 54 | public override int GetHashCode()
|
55 | 55 | {
|
56 | 56 | return base.GetHashCode();
|
57 | 57 | }
|
58 | 58 |
|
| 59 | + // Prevents from being displayed in intellisense |
59 | 60 | [EditorBrowsable(EditorBrowsableState.Never)]
|
60 | 61 | public override bool Equals(object obj)
|
61 | 62 | {
|
62 | 63 | return base.Equals(obj);
|
63 | 64 | }
|
64 | 65 |
|
| 66 | + // Prevents from being displayed in intellisense |
65 | 67 | [EditorBrowsable(EditorBrowsableState.Never)]
|
66 | 68 | public override string ToString()
|
67 | 69 | {
|
68 | 70 | return base.ToString();
|
69 | 71 | }
|
70 | 72 |
|
| 73 | + // Prevents from being displayed in intellisense |
71 | 74 | [EditorBrowsable(EditorBrowsableState.Never)]
|
72 | 75 | public new Type GetType()
|
73 | 76 | {
|
|
0 commit comments