From e7b84b753bf11739cbb2ef10fc0708b375ec816d Mon Sep 17 00:00:00 2001 From: James Newton-King Date: Wed, 4 Apr 2018 21:19:13 +1200 Subject: [PATCH] Refactor HubConnectionBuilder (#1830) --- SignalR.sln | 14 - .../BenchmarkServer/BenchmarkServer.csproj | 2 +- .../HubConnectionSendBenchmark.cs | 10 +- .../HubConnectionStartBenchmark.cs | 7 +- .../ts/FunctionalTests/FunctionalTests.csproj | 2 +- .../Tcp/TcpHubConnectionBuilderExtensions.cs | 8 +- samples/JwtClientSample/Program.cs | 10 +- samples/SignalRSamples/SignalRSamples.csproj | 2 +- .../HttpConnection.cs | 6 +- .../HttpOptions.cs | 4 +- .../HubConnection.cs | 10 +- .../HubConnectionBuilder.cs | 55 +-- .../HubConnectionBuilderDefaults.cs | 11 - .../HubConnectionBuilderExtensions.cs | 40 +-- .../IHubConnectionBuilder.cs | 8 +- ...soft.AspNetCore.SignalR.Client.Core.csproj | 1 + ...kProtocolHubConnectionBuilderExtensions.cs | 19 -- ...t.AspNetCore.SignalR.Client.MsgPack.csproj | 13 - .../HttpConnectionOptions.cs | 68 ++++ .../HubConnectionBuilderHttpExtensions.cs | 318 ++++-------------- .../ISignalRBuilder.cs | 0 .../ISignalRServerBuilder.cs | 9 + ...onProtocolDependencyInjectionExtensions.cs | 5 +- .../SignalRBuilder.cs | 4 +- .../SignalRDependencyInjectionExtensions.cs | 4 +- ...icrosoft.AspNetCore.SignalR.MsgPack.csproj | 14 - ...ckProtocolDependencyInjectionExtensions.cs | 5 +- .../RedisDependencyInjectionExtensions.cs | 6 +- ...HubOptionsDependencyInjectionExtensions.cs | 2 +- .../SignalRDependencyInjectionExtensions.cs | 4 +- .../HubConnectionTests.cs | 102 +++--- ...Core.SignalR.Client.FunctionalTests.csproj | 1 - .../HttpConnectionTests.Helpers.cs | 2 +- .../HttpConnectionTests.cs | 4 +- .../HubConnectionBuilderExtensionsTests.cs | 156 ++++----- .../HubConnectionBuilderTests.cs | 75 +++-- .../HubConnectionTests.ConnectionLifecycle.cs | 36 +- .../HubConnectionTests.Helpers.cs | 11 +- .../HubConnectionTests.cs | 6 +- ...oft.AspNetCore.SignalR.Client.Tests.csproj | 1 - ...soft.AspNetCore.SignalR.Redis.Tests.csproj | 1 - .../RedisEndToEnd.cs | 3 +- .../EndToEndTests.cs | 5 +- .../Microsoft.AspNetCore.SignalR.Tests.csproj | 1 - 44 files changed, 462 insertions(+), 603 deletions(-) delete mode 100644 src/Microsoft.AspNetCore.SignalR.Client.Core/HubConnectionBuilderDefaults.cs delete mode 100644 src/Microsoft.AspNetCore.SignalR.Client.MsgPack/MessagePackProtocolHubConnectionBuilderExtensions.cs delete mode 100644 src/Microsoft.AspNetCore.SignalR.Client.MsgPack/Microsoft.AspNetCore.SignalR.Client.MsgPack.csproj create mode 100644 src/Microsoft.AspNetCore.SignalR.Client/HttpConnectionOptions.cs rename src/{Microsoft.AspNetCore.SignalR.Core => Microsoft.AspNetCore.SignalR.Common}/ISignalRBuilder.cs (100%) create mode 100644 src/Microsoft.AspNetCore.SignalR.Core/ISignalRServerBuilder.cs delete mode 100644 src/Microsoft.AspNetCore.SignalR.MsgPack/Microsoft.AspNetCore.SignalR.MsgPack.csproj rename src/{Microsoft.AspNetCore.SignalR.MsgPack => Microsoft.AspNetCore.SignalR.Protocols.MsgPack}/MsgPackProtocolDependencyInjectionExtensions.cs (82%) diff --git a/SignalR.sln b/SignalR.sln index be48af096d..6f0eff3555 100644 --- a/SignalR.sln +++ b/SignalR.sln @@ -81,10 +81,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.AspNetCore.Signal EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.AspNetCore.SignalR.Protocols.MsgPack", "src\Microsoft.AspNetCore.SignalR.Protocols.MsgPack\Microsoft.AspNetCore.SignalR.Protocols.MsgPack.csproj", "{55DB4B6F-12E5-4A27-97F4-E97E135470FF}" EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.AspNetCore.SignalR.MsgPack", "src\Microsoft.AspNetCore.SignalR.MsgPack\Microsoft.AspNetCore.SignalR.MsgPack.csproj", "{FDDB4E1F-2FD2-49E8-B0FF-874B0931369A}" -EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.AspNetCore.SignalR.Client.MsgPack", "src\Microsoft.AspNetCore.SignalR.Client.MsgPack\Microsoft.AspNetCore.SignalR.Client.MsgPack.csproj", "{4DBF918E-BD37-4309-B448-BA68C935944D}" -EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FunctionalTests", "clients\ts\FunctionalTests\FunctionalTests.csproj", "{D0C7B22E-B0B6-4D62-BF7D-79EE4AAF1981}" EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BenchmarkServer", "benchmarks\BenchmarkServer\BenchmarkServer.csproj", "{B5286020-C218-443C-91A9-B65751FB9B29}" @@ -195,14 +191,6 @@ Global {55DB4B6F-12E5-4A27-97F4-E97E135470FF}.Debug|Any CPU.Build.0 = Debug|Any CPU {55DB4B6F-12E5-4A27-97F4-E97E135470FF}.Release|Any CPU.ActiveCfg = Release|Any CPU {55DB4B6F-12E5-4A27-97F4-E97E135470FF}.Release|Any CPU.Build.0 = Release|Any CPU - {FDDB4E1F-2FD2-49E8-B0FF-874B0931369A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {FDDB4E1F-2FD2-49E8-B0FF-874B0931369A}.Debug|Any CPU.Build.0 = Debug|Any CPU - {FDDB4E1F-2FD2-49E8-B0FF-874B0931369A}.Release|Any CPU.ActiveCfg = Release|Any CPU - {FDDB4E1F-2FD2-49E8-B0FF-874B0931369A}.Release|Any CPU.Build.0 = Release|Any CPU - {4DBF918E-BD37-4309-B448-BA68C935944D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {4DBF918E-BD37-4309-B448-BA68C935944D}.Debug|Any CPU.Build.0 = Debug|Any CPU - {4DBF918E-BD37-4309-B448-BA68C935944D}.Release|Any CPU.ActiveCfg = Release|Any CPU - {4DBF918E-BD37-4309-B448-BA68C935944D}.Release|Any CPU.Build.0 = Release|Any CPU {D0C7B22E-B0B6-4D62-BF7D-79EE4AAF1981}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {D0C7B22E-B0B6-4D62-BF7D-79EE4AAF1981}.Debug|Any CPU.Build.0 = Debug|Any CPU {D0C7B22E-B0B6-4D62-BF7D-79EE4AAF1981}.Release|Any CPU.ActiveCfg = Release|Any CPU @@ -241,8 +229,6 @@ Global {1A953296-E869-4DE2-A693-FD5FCDE27057} = {C4BC9889-B49F-41B6-806B-F84941B2549B} {0A0A6135-EA24-4307-95C2-CE1B7E164A5E} = {6A35B453-52EC-48AF-89CA-D4A69800F131} {55DB4B6F-12E5-4A27-97F4-E97E135470FF} = {DA69F624-5398-4884-87E4-B816698CDE65} - {FDDB4E1F-2FD2-49E8-B0FF-874B0931369A} = {DA69F624-5398-4884-87E4-B816698CDE65} - {4DBF918E-BD37-4309-B448-BA68C935944D} = {DA69F624-5398-4884-87E4-B816698CDE65} {D0C7B22E-B0B6-4D62-BF7D-79EE4AAF1981} = {3A76C5A2-79ED-49BC-8BDC-6A3A766FFA1B} {B5286020-C218-443C-91A9-B65751FB9B29} = {8A4582C8-DC59-4B61-BCE7-119FBAA99EFB} EndGlobalSection diff --git a/benchmarks/BenchmarkServer/BenchmarkServer.csproj b/benchmarks/BenchmarkServer/BenchmarkServer.csproj index 271cf4719a..41b716153f 100644 --- a/benchmarks/BenchmarkServer/BenchmarkServer.csproj +++ b/benchmarks/BenchmarkServer/BenchmarkServer.csproj @@ -9,7 +9,7 @@ - + diff --git a/benchmarks/Microsoft.AspNetCore.SignalR.Microbenchmarks/HubConnectionSendBenchmark.cs b/benchmarks/Microsoft.AspNetCore.SignalR.Microbenchmarks/HubConnectionSendBenchmark.cs index 6ed4d551fd..6902aa2e6b 100644 --- a/benchmarks/Microsoft.AspNetCore.SignalR.Microbenchmarks/HubConnectionSendBenchmark.cs +++ b/benchmarks/Microsoft.AspNetCore.SignalR.Microbenchmarks/HubConnectionSendBenchmark.cs @@ -1,15 +1,18 @@ // Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. +using System; using System.Buffers; using System.IO.Pipelines; using System.Threading.Tasks; using BenchmarkDotNet.Attributes; +using Microsoft.AspNetCore.Connections; using Microsoft.AspNetCore.Connections.Features; using Microsoft.AspNetCore.SignalR.Client; using Microsoft.AspNetCore.SignalR.Internal; using Microsoft.AspNetCore.SignalR.Internal.Protocol; using Microsoft.AspNetCore.SignalR.Microbenchmarks.Shared; +using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging.Abstractions; namespace Microsoft.AspNetCore.SignalR.Microbenchmarks @@ -47,7 +50,12 @@ namespace Microsoft.AspNetCore.SignalR.Microbenchmarks connection.Transport = _pipe; var protocol = Protocol == "json" ? (IHubProtocol)new JsonHubProtocol() : new MessagePackHubProtocol(); - _hubConnection = new HubConnection(() => connection, protocol, new NullLoggerFactory()); + + var hubConnectionBuilder = new HubConnectionBuilder(); + hubConnectionBuilder.WithHubProtocol(protocol); + hubConnectionBuilder.WithConnectionFactory(() => connection); + + _hubConnection = hubConnectionBuilder.Build(); _hubConnection.StartAsync().GetAwaiter().GetResult(); _arguments = new object[ArgumentCount]; diff --git a/benchmarks/Microsoft.AspNetCore.SignalR.Microbenchmarks/HubConnectionStartBenchmark.cs b/benchmarks/Microsoft.AspNetCore.SignalR.Microbenchmarks/HubConnectionStartBenchmark.cs index d4a91e4f44..bbeffd1c8f 100644 --- a/benchmarks/Microsoft.AspNetCore.SignalR.Microbenchmarks/HubConnectionStartBenchmark.cs +++ b/benchmarks/Microsoft.AspNetCore.SignalR.Microbenchmarks/HubConnectionStartBenchmark.cs @@ -13,6 +13,7 @@ using Microsoft.AspNetCore.SignalR.Client; using Microsoft.AspNetCore.SignalR.Internal; using Microsoft.AspNetCore.SignalR.Internal.Protocol; using Microsoft.AspNetCore.SignalR.Microbenchmarks.Shared; +using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging.Abstractions; namespace Microsoft.AspNetCore.SignalR.Microbenchmarks @@ -44,7 +45,11 @@ namespace Microsoft.AspNetCore.SignalR.Microbenchmarks connection.Features.Set(new TestConnectionInherentKeepAliveFeature()); connection.Transport = _pipe; - _hubConnection = new HubConnection(() => connection, new JsonHubProtocol(), new NullLoggerFactory()); + var hubConnectionBuilder = new HubConnectionBuilder(); + hubConnectionBuilder.WithHubProtocol(new JsonHubProtocol()); + hubConnectionBuilder.WithConnectionFactory(() => connection); + + _hubConnection = hubConnectionBuilder.Build(); } private void AddHandshakeResponse() diff --git a/clients/ts/FunctionalTests/FunctionalTests.csproj b/clients/ts/FunctionalTests/FunctionalTests.csproj index 8dd6198af8..c186a37764 100644 --- a/clients/ts/FunctionalTests/FunctionalTests.csproj +++ b/clients/ts/FunctionalTests/FunctionalTests.csproj @@ -19,7 +19,7 @@ - + diff --git a/samples/ClientSample/Tcp/TcpHubConnectionBuilderExtensions.cs b/samples/ClientSample/Tcp/TcpHubConnectionBuilderExtensions.cs index 775ef6f6ab..ceaf76c0bf 100644 --- a/samples/ClientSample/Tcp/TcpHubConnectionBuilderExtensions.cs +++ b/samples/ClientSample/Tcp/TcpHubConnectionBuilderExtensions.cs @@ -1,5 +1,9 @@ -using System.Net; +using System; +using System.Net; using ClientSample; +using Microsoft.AspNetCore.Connections; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.DependencyInjection.Extensions; namespace Microsoft.AspNetCore.SignalR.Client { @@ -7,7 +11,7 @@ namespace Microsoft.AspNetCore.SignalR.Client { public static IHubConnectionBuilder WithEndPoint(this IHubConnectionBuilder builder, IPEndPoint endPoint) { - builder.ConfigureConnectionFactory(() => new TcpConnection(endPoint)); + builder.WithConnectionFactory(() => new TcpConnection(endPoint)); return builder; } diff --git a/samples/JwtClientSample/Program.cs b/samples/JwtClientSample/Program.cs index 45b7a88e0a..7c32cdda72 100644 --- a/samples/JwtClientSample/Program.cs +++ b/samples/JwtClientSample/Program.cs @@ -28,13 +28,15 @@ namespace JwtClientSample private async Task RunConnection(HttpTransportType transportType) { - var userId = "C#" + transportType.ToString(); + var userId = "C#" + transportType; _tokens[userId] = await GetJwtToken(userId); var hubConnection = new HubConnectionBuilder() - .WithUrl(ServerUrl + "/broadcast") - .WithTransport(transportType) - .WithAccessToken(() => _tokens[userId]) + .WithUrl(ServerUrl + "/broadcast", options => + { + options.Transport = transportType; + options.AccessTokenFactory = () => _tokens[userId]; + }) .Build(); var closedTcs = new TaskCompletionSource(); diff --git a/samples/SignalRSamples/SignalRSamples.csproj b/samples/SignalRSamples/SignalRSamples.csproj index 1d087e50a0..dcb92be3a5 100644 --- a/samples/SignalRSamples/SignalRSamples.csproj +++ b/samples/SignalRSamples/SignalRSamples.csproj @@ -7,7 +7,7 @@ - + diff --git a/src/Microsoft.AspNetCore.Http.Connections.Client/HttpConnection.cs b/src/Microsoft.AspNetCore.Http.Connections.Client/HttpConnection.cs index 8521df7d82..8cef636e3e 100644 --- a/src/Microsoft.AspNetCore.Http.Connections.Client/HttpConnection.cs +++ b/src/Microsoft.AspNetCore.Http.Connections.Client/HttpConnection.cs @@ -391,12 +391,12 @@ namespace Microsoft.AspNetCore.Http.Connections.Client } httpMessageHandler = httpClientHandler; - if (_httpOptions.HttpMessageHandler != null) + if (_httpOptions.HttpMessageHandlerFactory != null) { - httpMessageHandler = _httpOptions.HttpMessageHandler(httpClientHandler); + httpMessageHandler = _httpOptions.HttpMessageHandlerFactory(httpClientHandler); if (httpMessageHandler == null) { - throw new InvalidOperationException("Configured HttpMessageHandler did not return a value."); + throw new InvalidOperationException("Configured HttpMessageHandlerFactory did not return a value."); } } } diff --git a/src/Microsoft.AspNetCore.Http.Connections.Client/HttpOptions.cs b/src/Microsoft.AspNetCore.Http.Connections.Client/HttpOptions.cs index efa127f0be..fabff436b1 100644 --- a/src/Microsoft.AspNetCore.Http.Connections.Client/HttpOptions.cs +++ b/src/Microsoft.AspNetCore.Http.Connections.Client/HttpOptions.cs @@ -13,10 +13,10 @@ namespace Microsoft.AspNetCore.Http.Connections.Client public class HttpOptions { /// - /// Gets or sets a delegate for wrapping or replacing the + /// Gets or sets a delegate for wrapping or replacing the /// that will make HTTP requests the server. /// - public Func HttpMessageHandler { get; set; } + public Func HttpMessageHandlerFactory { get; set; } public IReadOnlyCollection> Headers { get; set; } public Func AccessTokenFactory { get; set; } diff --git a/src/Microsoft.AspNetCore.SignalR.Client.Core/HubConnection.cs b/src/Microsoft.AspNetCore.SignalR.Client.Core/HubConnection.cs index d7a221763f..5e6e0154c9 100644 --- a/src/Microsoft.AspNetCore.SignalR.Client.Core/HubConnection.cs +++ b/src/Microsoft.AspNetCore.SignalR.Client.Core/HubConnection.cs @@ -14,6 +14,7 @@ using Microsoft.AspNetCore.Connections; using Microsoft.AspNetCore.Connections.Features; using Microsoft.AspNetCore.SignalR.Internal; using Microsoft.AspNetCore.SignalR.Internal.Protocol; +using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging.Abstractions; @@ -30,6 +31,7 @@ namespace Microsoft.AspNetCore.SignalR.Client private readonly ILoggerFactory _loggerFactory; private readonly ILogger _logger; private readonly IHubProtocol _protocol; + private readonly IServiceProvider _serviceProvider; private readonly Func _connectionFactory; private readonly ConcurrentDictionary> _handlers = new ConcurrentDictionary>(); private bool _disposed; @@ -45,14 +47,11 @@ namespace Microsoft.AspNetCore.SignalR.Client /// public TimeSpan ServerTimeout { get; set; } = DefaultServerTimeout; - public HubConnection(Func connectionFactory, IHubProtocol protocol) : this(connectionFactory, protocol, NullLoggerFactory.Instance) - { - } - - public HubConnection(Func connectionFactory, IHubProtocol protocol, ILoggerFactory loggerFactory) + public HubConnection(Func connectionFactory, IHubProtocol protocol, IServiceProvider serviceProvider, ILoggerFactory loggerFactory) { _connectionFactory = connectionFactory ?? throw new ArgumentNullException(nameof(connectionFactory)); _protocol = protocol ?? throw new ArgumentNullException(nameof(protocol)); + _serviceProvider = serviceProvider ?? throw new ArgumentNullException(nameof(serviceProvider)); _loggerFactory = loggerFactory ?? NullLoggerFactory.Instance; _logger = _loggerFactory.CreateLogger(); @@ -185,6 +184,7 @@ namespace Microsoft.AspNetCore.SignalR.Client if (disposing) { + (_serviceProvider as IDisposable)?.Dispose(); _disposed = true; } } diff --git a/src/Microsoft.AspNetCore.SignalR.Client.Core/HubConnectionBuilder.cs b/src/Microsoft.AspNetCore.SignalR.Client.Core/HubConnectionBuilder.cs index 02c6902721..b193331e28 100644 --- a/src/Microsoft.AspNetCore.SignalR.Client.Core/HubConnectionBuilder.cs +++ b/src/Microsoft.AspNetCore.SignalR.Client.Core/HubConnectionBuilder.cs @@ -2,72 +2,75 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; -using System.Collections.Generic; using System.ComponentModel; +using System.Linq; using Microsoft.AspNetCore.Connections; using Microsoft.AspNetCore.SignalR.Internal.Protocol; -using Newtonsoft.Json; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.DependencyInjection.Extensions; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Logging.Abstractions; namespace Microsoft.AspNetCore.SignalR.Client { public class HubConnectionBuilder : IHubConnectionBuilder { - private readonly Dictionary, object> _settings = new Dictionary, object>(); - private Func _connectionFactoryDelegate; + private bool _hubConnectionBuilt; - public void ConfigureConnectionFactory(Func connectionFactoryDelegate) => - _connectionFactoryDelegate = connectionFactoryDelegate; + public IServiceCollection Services { get; } - public void AddSetting(string name, T value) + public HubConnectionBuilder() { - _settings[new KeyValuePair(name, typeof(T))] = value; - } - - public bool TryGetSetting(string name, out T value) - { - value = default; - if (!_settings.TryGetValue(new KeyValuePair(name, typeof(T)), out var setting)) - { - return false; - } - - value = (T)setting; - return true; + Services = new ServiceCollection(); + Services.AddSingleton(NullLoggerFactory.Instance); + Services.AddSingleton(); + Services.AddSingleton(); } public HubConnection Build() { - if (_connectionFactoryDelegate == null) + // Build can only be used once + if (_hubConnectionBuilt) { - throw new InvalidOperationException("Cannot create IConnection instance. The connection factory was not configured."); + throw new InvalidOperationException("HubConnectionBuilder allows creation only of a single instance of HubConnection."); } - IHubConnectionBuilder builder = this; + _hubConnectionBuilt = true; - var loggerFactory = builder.GetLoggerFactory(); - var hubProtocol = builder.GetHubProtocol(); + // The service provider is disposed by the HubConnection + var serviceProvider = Services.BuildServiceProvider(); - return new HubConnection(_connectionFactoryDelegate, hubProtocol ?? new JsonHubProtocol(), loggerFactory); + var connectionFactory = serviceProvider.GetService>(); + if (connectionFactory == null) + { + throw new InvalidOperationException("Cannot create HubConnection instance. A connection was not configured."); + } + + return serviceProvider.GetService(); } + // Prevents from being displayed in intellisense [EditorBrowsable(EditorBrowsableState.Never)] public override int GetHashCode() { return base.GetHashCode(); } + // Prevents from being displayed in intellisense [EditorBrowsable(EditorBrowsableState.Never)] public override bool Equals(object obj) { return base.Equals(obj); } + // Prevents from being displayed in intellisense [EditorBrowsable(EditorBrowsableState.Never)] public override string ToString() { return base.ToString(); } + // Prevents from being displayed in intellisense [EditorBrowsable(EditorBrowsableState.Never)] public new Type GetType() { diff --git a/src/Microsoft.AspNetCore.SignalR.Client.Core/HubConnectionBuilderDefaults.cs b/src/Microsoft.AspNetCore.SignalR.Client.Core/HubConnectionBuilderDefaults.cs deleted file mode 100644 index 07b79b4ea9..0000000000 --- a/src/Microsoft.AspNetCore.SignalR.Client.Core/HubConnectionBuilderDefaults.cs +++ /dev/null @@ -1,11 +0,0 @@ -// Copyright (c) .NET Foundation. All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -namespace Microsoft.AspNetCore.SignalR.Client -{ - public static class HubConnectionBuilderDefaults - { - public static readonly string LoggerFactoryKey = "LoggerFactory"; - public static readonly string HubProtocolKey = "HubProtocol"; - } -} \ No newline at end of file diff --git a/src/Microsoft.AspNetCore.SignalR.Client.Core/HubConnectionBuilderExtensions.cs b/src/Microsoft.AspNetCore.SignalR.Client.Core/HubConnectionBuilderExtensions.cs index 69ed4535fc..5cc0e7fb66 100644 --- a/src/Microsoft.AspNetCore.SignalR.Client.Core/HubConnectionBuilderExtensions.cs +++ b/src/Microsoft.AspNetCore.SignalR.Client.Core/HubConnectionBuilderExtensions.cs @@ -2,7 +2,9 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; +using Microsoft.AspNetCore.Connections; using Microsoft.AspNetCore.SignalR.Internal.Protocol; +using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; @@ -10,46 +12,34 @@ namespace Microsoft.AspNetCore.SignalR.Client { public static class HubConnectionBuilderExtensions { - public static IHubConnectionBuilder WithHubProtocol(this IHubConnectionBuilder hubConnectionBuilder, IHubProtocol hubProtocol) + public static IHubConnectionBuilder WithConnectionFactory(this IHubConnectionBuilder hubConnectionBuilder, Func connectionFactory) { - hubConnectionBuilder.AddSetting(HubConnectionBuilderDefaults.HubProtocolKey, hubProtocol); + hubConnectionBuilder.Services.AddSingleton(connectionFactory); return hubConnectionBuilder; } - public static IHubConnectionBuilder WithJsonProtocol(this IHubConnectionBuilder hubConnectionBuilder) + public static IHubConnectionBuilder WithHubProtocol(this IHubConnectionBuilder hubConnectionBuilder, IHubProtocol hubProtocol) { - return hubConnectionBuilder.WithHubProtocol(new JsonHubProtocol()); - } - - public static IHubConnectionBuilder WithJsonProtocol(this IHubConnectionBuilder hubConnectionBuilder, JsonHubProtocolOptions options) - { - return hubConnectionBuilder.WithHubProtocol(new JsonHubProtocol(Options.Create(options))); + hubConnectionBuilder.Services.AddSingleton(hubProtocol); + return hubConnectionBuilder; } public static IHubConnectionBuilder WithLoggerFactory(this IHubConnectionBuilder hubConnectionBuilder, ILoggerFactory loggerFactory) { - hubConnectionBuilder.AddSetting(HubConnectionBuilderDefaults.LoggerFactoryKey, - loggerFactory ?? throw new ArgumentNullException(nameof(loggerFactory))); + if (loggerFactory == null) + { + throw new ArgumentNullException(nameof(loggerFactory)); + } + + hubConnectionBuilder.Services.AddSingleton(loggerFactory); return hubConnectionBuilder; } public static IHubConnectionBuilder WithLogger(this IHubConnectionBuilder hubConnectionBuilder, Action configureLogging) { - var loggerFactory = hubConnectionBuilder.GetLoggerFactory() ?? new LoggerFactory(); + var loggerFactory = new LoggerFactory(); configureLogging(loggerFactory); return hubConnectionBuilder.WithLoggerFactory(loggerFactory); } - - public static ILoggerFactory GetLoggerFactory(this IHubConnectionBuilder hubConnectionBuilder) - { - hubConnectionBuilder.TryGetSetting(HubConnectionBuilderDefaults.LoggerFactoryKey, out var loggerFactory); - return loggerFactory; - } - - public static IHubProtocol GetHubProtocol(this IHubConnectionBuilder hubConnectionBuilder) - { - hubConnectionBuilder.TryGetSetting(HubConnectionBuilderDefaults.HubProtocolKey, out var hubProtocol); - return hubProtocol; - } } -} +} \ No newline at end of file diff --git a/src/Microsoft.AspNetCore.SignalR.Client.Core/IHubConnectionBuilder.cs b/src/Microsoft.AspNetCore.SignalR.Client.Core/IHubConnectionBuilder.cs index 37f24b13b1..efb3ed5bc9 100644 --- a/src/Microsoft.AspNetCore.SignalR.Client.Core/IHubConnectionBuilder.cs +++ b/src/Microsoft.AspNetCore.SignalR.Client.Core/IHubConnectionBuilder.cs @@ -1,16 +1,10 @@ // Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. -using System; -using Microsoft.AspNetCore.Connections; - namespace Microsoft.AspNetCore.SignalR.Client { - public interface IHubConnectionBuilder + public interface IHubConnectionBuilder : ISignalRBuilder { - void AddSetting(string name, T value); - bool TryGetSetting(string name, out T value); - void ConfigureConnectionFactory(Func connectionFactoryDelegate); HubConnection Build(); } } diff --git a/src/Microsoft.AspNetCore.SignalR.Client.Core/Microsoft.AspNetCore.SignalR.Client.Core.csproj b/src/Microsoft.AspNetCore.SignalR.Client.Core/Microsoft.AspNetCore.SignalR.Client.Core.csproj index e74f553f14..e709124f48 100644 --- a/src/Microsoft.AspNetCore.SignalR.Client.Core/Microsoft.AspNetCore.SignalR.Client.Core.csproj +++ b/src/Microsoft.AspNetCore.SignalR.Client.Core/Microsoft.AspNetCore.SignalR.Client.Core.csproj @@ -16,6 +16,7 @@ + diff --git a/src/Microsoft.AspNetCore.SignalR.Client.MsgPack/MessagePackProtocolHubConnectionBuilderExtensions.cs b/src/Microsoft.AspNetCore.SignalR.Client.MsgPack/MessagePackProtocolHubConnectionBuilderExtensions.cs deleted file mode 100644 index c92e5da68f..0000000000 --- a/src/Microsoft.AspNetCore.SignalR.Client.MsgPack/MessagePackProtocolHubConnectionBuilderExtensions.cs +++ /dev/null @@ -1,19 +0,0 @@ -// Copyright (c) .NET Foundation. All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using Microsoft.AspNetCore.SignalR.Internal.Protocol; -using Microsoft.Extensions.Options; - -namespace Microsoft.AspNetCore.SignalR.Client -{ - public static class MessagePackProtocolHubConnectionBuilderExtensions - { - public static IHubConnectionBuilder WithMessagePackProtocol(this IHubConnectionBuilder builder) => - WithMessagePackProtocol(builder, new MessagePackHubProtocolOptions()); - - public static IHubConnectionBuilder WithMessagePackProtocol(this IHubConnectionBuilder builder, MessagePackHubProtocolOptions options) - { - return builder.WithHubProtocol(new MessagePackHubProtocol(Options.Create(options))); - } - } -} diff --git a/src/Microsoft.AspNetCore.SignalR.Client.MsgPack/Microsoft.AspNetCore.SignalR.Client.MsgPack.csproj b/src/Microsoft.AspNetCore.SignalR.Client.MsgPack/Microsoft.AspNetCore.SignalR.Client.MsgPack.csproj deleted file mode 100644 index bf971bf27e..0000000000 --- a/src/Microsoft.AspNetCore.SignalR.Client.MsgPack/Microsoft.AspNetCore.SignalR.Client.MsgPack.csproj +++ /dev/null @@ -1,13 +0,0 @@ - - - - Provides client-side support for the MsgPack protocol in ASP.NET Core SignalR - netstandard2.0 - - - - - - - - diff --git a/src/Microsoft.AspNetCore.SignalR.Client/HttpConnectionOptions.cs b/src/Microsoft.AspNetCore.SignalR.Client/HttpConnectionOptions.cs new file mode 100644 index 0000000000..17ad35ad63 --- /dev/null +++ b/src/Microsoft.AspNetCore.SignalR.Client/HttpConnectionOptions.cs @@ -0,0 +1,68 @@ +// Copyright (c) .NET Foundation. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using System; +using System.Collections.Generic; +using System.Net; +using System.Net.Http; +using System.Net.WebSockets; +using System.Security.Cryptography.X509Certificates; +using Microsoft.AspNetCore.Http.Connections; + +namespace Microsoft.AspNetCore.SignalR.Client +{ + public class HttpConnectionOptions + { + internal X509CertificateCollection _clientCertificates; + internal IDictionary _headers; + internal CookieContainer _cookies; + + public Uri Url { get; set; } + public HttpTransportType? Transport { get; set; } + public Func MessageHandlerFactory { get; set; } + public bool? UseDefaultCredentials { get; set; } + public ICredentials Credentials { get; set; } + public IWebProxy Proxy { get; set; } + public Func AccessTokenFactory { get; set; } + public Action WebSocketOptions { get; set; } + + public X509CertificateCollection ClientCertificates + { + get + { + if (_clientCertificates == null) + { + _clientCertificates = new X509CertificateCollection(); + } + + return _clientCertificates; + } + } + + public CookieContainer Cookies + { + get + { + if (_cookies == null) + { + _cookies = new CookieContainer(); + } + + return _cookies; + } + } + + public IDictionary Headers + { + get + { + if (_headers == null) + { + _headers = new Dictionary(); + } + + return _headers; + } + } + } +} \ No newline at end of file diff --git a/src/Microsoft.AspNetCore.SignalR.Client/HubConnectionBuilderHttpExtensions.cs b/src/Microsoft.AspNetCore.SignalR.Client/HubConnectionBuilderHttpExtensions.cs index 899dbf6396..73e8dd0c1e 100644 --- a/src/Microsoft.AspNetCore.SignalR.Client/HubConnectionBuilderHttpExtensions.cs +++ b/src/Microsoft.AspNetCore.SignalR.Client/HubConnectionBuilderHttpExtensions.cs @@ -2,283 +2,101 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; -using System.Collections.Generic; using System.Collections.ObjectModel; -using System.Net; -using System.Net.Http; -using System.Net.WebSockets; -using System.Security.Cryptography.X509Certificates; +using Microsoft.AspNetCore.Connections; using Microsoft.AspNetCore.Http.Connections; using Microsoft.AspNetCore.Http.Connections.Client; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.DependencyInjection.Extensions; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Options; namespace Microsoft.AspNetCore.SignalR.Client { public static class HubConnectionBuilderHttpExtensions { - public static readonly string TransportTypeKey = "TransportType"; - public static readonly string HttpMessageHandlerKey = "HttpMessageHandler"; - public static readonly string HeadersKey = "Headers"; - public static readonly string AccessTokenFactoryKey = "AccessTokenFactory"; - public static readonly string WebSocketOptionsKey = "WebSocketOptions"; - public static readonly string CookiesKey = "Cookies"; - public static readonly string ProxyKey = "Proxy"; - public static readonly string ClientCertificatesKey = "ClientCertificates"; - public static readonly string CredentialsKey = "Credentials"; - public static readonly string UseDefaultCredentialsKey = "UseDefaultCredentials"; - public static IHubConnectionBuilder WithUrl(this IHubConnectionBuilder hubConnectionBuilder, string url) { - if (url == null) - { - throw new ArgumentNullException(nameof(url)); - } + hubConnectionBuilder.WithUrl(new Uri(url), null, _ => { }); + return hubConnectionBuilder; + } - return hubConnectionBuilder.WithUrl(new Uri(url)); + public static IHubConnectionBuilder WithUrl(this IHubConnectionBuilder hubConnectionBuilder, string url, Action configureHttpConnection) + { + hubConnectionBuilder.WithUrl(new Uri(url), null, configureHttpConnection); + return hubConnectionBuilder; + } + + public static IHubConnectionBuilder WithUrl(this IHubConnectionBuilder hubConnectionBuilder, string url, HttpTransportType? transportType) + { + hubConnectionBuilder.WithUrl(new Uri(url), transportType, _ => { }); + return hubConnectionBuilder; + } + + public static IHubConnectionBuilder WithUrl(this IHubConnectionBuilder hubConnectionBuilder, string url, HttpTransportType? transportType, Action configureHttpConnection) + { + hubConnectionBuilder.WithUrl(new Uri(url), transportType, configureHttpConnection); + return hubConnectionBuilder; } public static IHubConnectionBuilder WithUrl(this IHubConnectionBuilder hubConnectionBuilder, Uri url) { - if (url == null) + hubConnectionBuilder.WithUrl(url, null, _ => { }); + return hubConnectionBuilder; + } + + public static IHubConnectionBuilder WithUrl(this IHubConnectionBuilder hubConnectionBuilder, Uri url, Action configureHttpConnection) + { + hubConnectionBuilder.WithUrl(url, null, configureHttpConnection); + return hubConnectionBuilder; + } + + public static IHubConnectionBuilder WithUrl(this IHubConnectionBuilder hubConnectionBuilder, Uri url, HttpTransportType? transportType) + { + hubConnectionBuilder.WithUrl(url, null, _ => { }); + return hubConnectionBuilder; + } + + public static IHubConnectionBuilder WithUrl(this IHubConnectionBuilder hubConnectionBuilder, Uri url, HttpTransportType? transportType, Action configureHttpConnection) + { + hubConnectionBuilder.Services.Configure(o => { - throw new ArgumentNullException(nameof(url)); + o.Url = url; + o.Transport = transportType; + }); + + if (configureHttpConnection != null) + { + hubConnectionBuilder.Services.Configure(configureHttpConnection); } - hubConnectionBuilder.ConfigureConnectionFactory(() => + hubConnectionBuilder.Services.AddSingleton(services => { - var headers = hubConnectionBuilder.GetHeaders(); + var value = services.GetService>().Value; + var httpOptions = new HttpOptions { - HttpMessageHandler = hubConnectionBuilder.GetMessageHandler(), - Headers = headers != null ? new ReadOnlyDictionary(headers) : null, - AccessTokenFactory = hubConnectionBuilder.GetAccessTokenFactory(), - WebSocketOptions = hubConnectionBuilder.GetWebSocketOptions(), - Cookies = hubConnectionBuilder.GetCookies(), - Proxy = hubConnectionBuilder.GetProxy(), - UseDefaultCredentials = hubConnectionBuilder.GetUseDefaultCredentials(), - ClientCertificates = hubConnectionBuilder.GetClientCertificates(), - Credentials = hubConnectionBuilder.GetCredentials(), + HttpMessageHandlerFactory = value.MessageHandlerFactory, + Headers = value._headers != null ? new ReadOnlyDictionary(value._headers) : null, + AccessTokenFactory = value.AccessTokenFactory, + WebSocketOptions = value.WebSocketOptions, + Cookies = value._cookies, + Proxy = value.Proxy, + UseDefaultCredentials = value.UseDefaultCredentials, + ClientCertificates = value._clientCertificates, + Credentials = value.Credentials, }; - return new HttpConnection(url, - hubConnectionBuilder.GetTransport(), - hubConnectionBuilder.GetLoggerFactory(), + Func createConnection = () => new HttpConnection( + value.Url, + value.Transport ?? HttpTransportType.All, + services.GetService(), httpOptions); + + return createConnection; }); - return hubConnectionBuilder; - } - - public static IHubConnectionBuilder WithTransport(this IHubConnectionBuilder hubConnectionBuilder, HttpTransportType transportType) - { - hubConnectionBuilder.AddSetting(TransportTypeKey, transportType); - return hubConnectionBuilder; - } - - /// - /// Sets a delegate for wrapping or replacing the that will make HTTP requests the server. - /// - /// The . - /// A delegate for wrapping or replacing the that will make HTTP requests the server. - /// The . - public static IHubConnectionBuilder WithMessageHandler(this IHubConnectionBuilder hubConnectionBuilder, Func configurehttpMessageHandler) - { - hubConnectionBuilder.AddSetting(HttpMessageHandlerKey, configurehttpMessageHandler); - return hubConnectionBuilder; - } - - public static IHubConnectionBuilder WithHeader(this IHubConnectionBuilder hubConnectionBuilder, string name, string value) - { - if (string.IsNullOrEmpty(name)) - { - throw new ArgumentException("Header name cannot be null or empty string.", nameof(name)); - } - - var headers = hubConnectionBuilder.GetHeaders(); - if (headers == null) - { - headers = new Dictionary(); - hubConnectionBuilder.AddSetting(HeadersKey, headers); - } - - headers.Add(name, value); return hubConnectionBuilder; } - - public static IHubConnectionBuilder WithUseDefaultCredentials(this IHubConnectionBuilder hubConnectionBuilder, bool useDefaultCredentials) - { - hubConnectionBuilder.AddSetting(UseDefaultCredentialsKey, useDefaultCredentials); - return hubConnectionBuilder; - } - - public static IHubConnectionBuilder WithCredentials(this IHubConnectionBuilder hubConnectionBuilder, ICredentials credentials) - { - hubConnectionBuilder.AddSetting(CredentialsKey, credentials); - return hubConnectionBuilder; - } - - public static IHubConnectionBuilder WithProxy(this IHubConnectionBuilder hubConnectionBuilder, IWebProxy proxy) - { - hubConnectionBuilder.AddSetting(ProxyKey, proxy); - return hubConnectionBuilder; - } - - public static IHubConnectionBuilder WithCookie(this IHubConnectionBuilder hubConnectionBuilder, Cookie cookie) - { - if (cookie == null) - { - throw new ArgumentNullException(nameof(cookie)); - } - - var cookies = hubConnectionBuilder.GetCookies(); - if (cookies == null) - { - cookies = new CookieContainer(); - hubConnectionBuilder.AddSetting(CookiesKey, cookies); - } - - cookies.Add(cookie); - - return hubConnectionBuilder; - } - - public static IHubConnectionBuilder WithClientCertificate(this IHubConnectionBuilder hubConnectionBuilder, X509Certificate clientCertificate) - { - if (clientCertificate == null) - { - throw new ArgumentNullException(nameof(clientCertificate)); - } - - var clientCertificates = hubConnectionBuilder.GetClientCertificates(); - if (clientCertificates == null) - { - clientCertificates = new X509CertificateCollection(); - hubConnectionBuilder.AddSetting(ClientCertificatesKey, clientCertificates); - } - - clientCertificates.Add(clientCertificate); - - return hubConnectionBuilder; - } - - public static IHubConnectionBuilder WithAccessToken(this IHubConnectionBuilder hubConnectionBuilder, Func accessTokenFactory) - { - if (accessTokenFactory == null) - { - throw new ArgumentNullException(nameof(accessTokenFactory)); - } - - hubConnectionBuilder.AddSetting(AccessTokenFactoryKey, accessTokenFactory); - - return hubConnectionBuilder; - } - - public static IHubConnectionBuilder WithWebSocketOptions(this IHubConnectionBuilder hubConnectionBuilder, Action configureWebSocketOptions) - { - if (configureWebSocketOptions == null) - { - throw new ArgumentNullException(nameof(configureWebSocketOptions)); - } - - hubConnectionBuilder.AddSetting(WebSocketOptionsKey, configureWebSocketOptions); - - return hubConnectionBuilder; - } - - public static HttpTransportType GetTransport(this IHubConnectionBuilder hubConnectionBuilder) - { - if (hubConnectionBuilder.TryGetSetting(TransportTypeKey, out var transportType)) - { - return transportType; - } - - return HttpTransportType.All; - } - - /// - /// Gets a delegate for wrapping or replacing the that will make HTTP requests the server. - /// - /// The . - /// A delegate for wrapping or replacing the that will make HTTP requests the server. - public static Func GetMessageHandler(this IHubConnectionBuilder hubConnectionBuilder) - { - hubConnectionBuilder.TryGetSetting>(HttpMessageHandlerKey, out var messageHandler); - return messageHandler; - } - - public static IDictionary GetHeaders(this IHubConnectionBuilder hubConnectionBuilder) - { - if (hubConnectionBuilder.TryGetSetting>(HeadersKey, out var headers)) - { - return headers; - } - - return null; - } - - public static IWebProxy GetProxy(this IHubConnectionBuilder hubConnectionBuilder) - { - if (hubConnectionBuilder.TryGetSetting(ProxyKey, out var proxy)) - { - return proxy; - } - - return null; - } - - public static bool? GetUseDefaultCredentials(this IHubConnectionBuilder hubConnectionBuilder) - { - if (hubConnectionBuilder.TryGetSetting(UseDefaultCredentialsKey, out var useDefaultCredentials)) - { - return useDefaultCredentials; - } - - return null; - } - - public static CookieContainer GetCookies(this IHubConnectionBuilder hubConnectionBuilder) - { - if (hubConnectionBuilder.TryGetSetting(CookiesKey, out var cookies)) - { - return cookies; - } - - return null; - } - - public static ICredentials GetCredentials(this IHubConnectionBuilder hubConnectionBuilder) - { - if (hubConnectionBuilder.TryGetSetting(CredentialsKey, out var credentials)) - { - return credentials; - } - - return null; - } - - public static X509CertificateCollection GetClientCertificates(this IHubConnectionBuilder hubConnectionBuilder) - { - if (hubConnectionBuilder.TryGetSetting(ClientCertificatesKey, out var clientCertificates)) - { - return clientCertificates; - } - - return null; - } - - public static Func GetAccessTokenFactory(this IHubConnectionBuilder hubConnectionBuilder) - { - if (hubConnectionBuilder.TryGetSetting>(AccessTokenFactoryKey, out var factory)) - { - return factory; - } - - return null; - } - - public static Action GetWebSocketOptions(this IHubConnectionBuilder hubConnectionBuilder) - { - hubConnectionBuilder.TryGetSetting>(WebSocketOptionsKey, out var webSocketOptions); - return webSocketOptions; - } } } diff --git a/src/Microsoft.AspNetCore.SignalR.Core/ISignalRBuilder.cs b/src/Microsoft.AspNetCore.SignalR.Common/ISignalRBuilder.cs similarity index 100% rename from src/Microsoft.AspNetCore.SignalR.Core/ISignalRBuilder.cs rename to src/Microsoft.AspNetCore.SignalR.Common/ISignalRBuilder.cs diff --git a/src/Microsoft.AspNetCore.SignalR.Core/ISignalRServerBuilder.cs b/src/Microsoft.AspNetCore.SignalR.Core/ISignalRServerBuilder.cs new file mode 100644 index 0000000000..f3ad4235a2 --- /dev/null +++ b/src/Microsoft.AspNetCore.SignalR.Core/ISignalRServerBuilder.cs @@ -0,0 +1,9 @@ +// Copyright (c) .NET Foundation. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +namespace Microsoft.AspNetCore.SignalR +{ + public interface ISignalRServerBuilder : ISignalRBuilder + { + } +} diff --git a/src/Microsoft.AspNetCore.SignalR.Core/JsonProtocolDependencyInjectionExtensions.cs b/src/Microsoft.AspNetCore.SignalR.Core/JsonProtocolDependencyInjectionExtensions.cs index b755f40815..8a6ff8130f 100644 --- a/src/Microsoft.AspNetCore.SignalR.Core/JsonProtocolDependencyInjectionExtensions.cs +++ b/src/Microsoft.AspNetCore.SignalR.Core/JsonProtocolDependencyInjectionExtensions.cs @@ -16,7 +16,8 @@ namespace Microsoft.Extensions.DependencyInjection /// /// The representing the SignalR server to add JSON protocol support to. /// The value of - public static ISignalRBuilder AddJsonProtocol(this ISignalRBuilder builder) => AddJsonProtocol(builder, _ => { }); + public static TBuilder AddJsonProtocol(this TBuilder builder) where TBuilder : ISignalRBuilder + => AddJsonProtocol(builder, _ => { }); /// /// Enables the JSON protocol for SignalR and allows options for the JSON protocol to be configured. @@ -27,7 +28,7 @@ namespace Microsoft.Extensions.DependencyInjection /// The representing the SignalR server to add JSON protocol support to. /// A delegate that can be used to configure the /// The value of - public static ISignalRBuilder AddJsonProtocol(this ISignalRBuilder builder, Action configure) + public static TBuilder AddJsonProtocol(this TBuilder builder, Action configure) where TBuilder : ISignalRBuilder { builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton()); builder.Services.Configure(configure); diff --git a/src/Microsoft.AspNetCore.SignalR.Core/SignalRBuilder.cs b/src/Microsoft.AspNetCore.SignalR.Core/SignalRBuilder.cs index cada0ef821..51f0fb5502 100644 --- a/src/Microsoft.AspNetCore.SignalR.Core/SignalRBuilder.cs +++ b/src/Microsoft.AspNetCore.SignalR.Core/SignalRBuilder.cs @@ -9,9 +9,9 @@ using Microsoft.Extensions.DependencyInjection; namespace Microsoft.AspNetCore.SignalR { - public class SignalRBuilder : ISignalRBuilder + public class SignalRServerBuilder : ISignalRServerBuilder { - public SignalRBuilder(IServiceCollection services) + public SignalRServerBuilder(IServiceCollection services) { Services = services; } diff --git a/src/Microsoft.AspNetCore.SignalR.Core/SignalRDependencyInjectionExtensions.cs b/src/Microsoft.AspNetCore.SignalR.Core/SignalRDependencyInjectionExtensions.cs index 7d3fc13fb3..e369c0c1ca 100644 --- a/src/Microsoft.AspNetCore.SignalR.Core/SignalRDependencyInjectionExtensions.cs +++ b/src/Microsoft.AspNetCore.SignalR.Core/SignalRDependencyInjectionExtensions.cs @@ -9,7 +9,7 @@ namespace Microsoft.Extensions.DependencyInjection { public static class SignalRDependencyInjectionExtensions { - public static ISignalRBuilder AddSignalRCore(this IServiceCollection services) + public static ISignalRServerBuilder AddSignalRCore(this IServiceCollection services) { services.AddSingleton(typeof(HubLifetimeManager<>), typeof(DefaultHubLifetimeManager<>)); services.AddSingleton(typeof(IHubProtocolResolver), typeof(DefaultHubProtocolResolver)); @@ -22,7 +22,7 @@ namespace Microsoft.Extensions.DependencyInjection services.AddAuthorization(); - return new SignalRBuilder(services); + return new SignalRServerBuilder(services); } } } diff --git a/src/Microsoft.AspNetCore.SignalR.MsgPack/Microsoft.AspNetCore.SignalR.MsgPack.csproj b/src/Microsoft.AspNetCore.SignalR.MsgPack/Microsoft.AspNetCore.SignalR.MsgPack.csproj deleted file mode 100644 index f7df7d9aef..0000000000 --- a/src/Microsoft.AspNetCore.SignalR.MsgPack/Microsoft.AspNetCore.SignalR.MsgPack.csproj +++ /dev/null @@ -1,14 +0,0 @@ - - - - Provides server-side support for the MsgPack protocol in ASP.NET Core SignalR - netstandard2.0 - true - - - - - - - - diff --git a/src/Microsoft.AspNetCore.SignalR.MsgPack/MsgPackProtocolDependencyInjectionExtensions.cs b/src/Microsoft.AspNetCore.SignalR.Protocols.MsgPack/MsgPackProtocolDependencyInjectionExtensions.cs similarity index 82% rename from src/Microsoft.AspNetCore.SignalR.MsgPack/MsgPackProtocolDependencyInjectionExtensions.cs rename to src/Microsoft.AspNetCore.SignalR.Protocols.MsgPack/MsgPackProtocolDependencyInjectionExtensions.cs index c5420f7310..ff1d986de7 100644 --- a/src/Microsoft.AspNetCore.SignalR.MsgPack/MsgPackProtocolDependencyInjectionExtensions.cs +++ b/src/Microsoft.AspNetCore.SignalR.Protocols.MsgPack/MsgPackProtocolDependencyInjectionExtensions.cs @@ -16,7 +16,8 @@ namespace Microsoft.Extensions.DependencyInjection /// /// The representing the SignalR server to add MsgPack protocol support to. /// The value of - public static ISignalRBuilder AddMessagePackProtocol(this ISignalRBuilder builder) => AddMessagePackProtocol(builder, _ => { }); + public static TBuilder AddMessagePackProtocol(this TBuilder builder) where TBuilder : ISignalRBuilder + => AddMessagePackProtocol(builder, _ => { }); /// /// Enables the MsgPack protocol for SignalR and allows options for the MsgPack protocol to be configured. @@ -27,7 +28,7 @@ namespace Microsoft.Extensions.DependencyInjection /// The representing the SignalR server to add MsgPack protocol support to. /// A delegate that can be used to configure the /// The value of - public static ISignalRBuilder AddMessagePackProtocol(this ISignalRBuilder builder, Action configure) + public static TBuilder AddMessagePackProtocol(this TBuilder builder, Action configure) where TBuilder : ISignalRBuilder { builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton()); builder.Services.Configure(configure); diff --git a/src/Microsoft.AspNetCore.SignalR.Redis/RedisDependencyInjectionExtensions.cs b/src/Microsoft.AspNetCore.SignalR.Redis/RedisDependencyInjectionExtensions.cs index 824899d73e..300970ea16 100644 --- a/src/Microsoft.AspNetCore.SignalR.Redis/RedisDependencyInjectionExtensions.cs +++ b/src/Microsoft.AspNetCore.SignalR.Redis/RedisDependencyInjectionExtensions.cs @@ -10,12 +10,12 @@ namespace Microsoft.Extensions.DependencyInjection { public static class RedisDependencyInjectionExtensions { - public static ISignalRBuilder AddRedis(this ISignalRBuilder builder) + public static ISignalRServerBuilder AddRedis(this ISignalRServerBuilder builder) { return AddRedis(builder, o => { }); } - public static ISignalRBuilder AddRedis(this ISignalRBuilder builder, string redisConnectionString) + public static ISignalRServerBuilder AddRedis(this ISignalRServerBuilder builder, string redisConnectionString) { return AddRedis(builder, o => { @@ -23,7 +23,7 @@ namespace Microsoft.Extensions.DependencyInjection }); } - public static ISignalRBuilder AddRedis(this ISignalRBuilder builder, Action configure) + public static ISignalRServerBuilder AddRedis(this ISignalRServerBuilder builder, Action configure) { builder.Services.Configure(configure); builder.Services.AddSingleton(typeof(HubLifetimeManager<>), typeof(RedisHubLifetimeManager<>)); diff --git a/src/Microsoft.AspNetCore.SignalR/HubOptionsDependencyInjectionExtensions.cs b/src/Microsoft.AspNetCore.SignalR/HubOptionsDependencyInjectionExtensions.cs index d8186fa566..c7af96616a 100644 --- a/src/Microsoft.AspNetCore.SignalR/HubOptionsDependencyInjectionExtensions.cs +++ b/src/Microsoft.AspNetCore.SignalR/HubOptionsDependencyInjectionExtensions.cs @@ -9,7 +9,7 @@ namespace Microsoft.Extensions.DependencyInjection { public static class HubOptionsDependencyInjectionExtensions { - public static ISignalRBuilder AddHubOptions(this ISignalRBuilder signalrBuilder, Action> options) where THub : Hub + public static ISignalRServerBuilder AddHubOptions(this ISignalRServerBuilder signalrBuilder, Action> options) where THub : Hub { signalrBuilder.Services.AddSingleton>, HubOptionsSetup>(); signalrBuilder.Services.Configure(options); diff --git a/src/Microsoft.AspNetCore.SignalR/SignalRDependencyInjectionExtensions.cs b/src/Microsoft.AspNetCore.SignalR/SignalRDependencyInjectionExtensions.cs index ae2498ecde..931bd9a20a 100644 --- a/src/Microsoft.AspNetCore.SignalR/SignalRDependencyInjectionExtensions.cs +++ b/src/Microsoft.AspNetCore.SignalR/SignalRDependencyInjectionExtensions.cs @@ -9,7 +9,7 @@ namespace Microsoft.Extensions.DependencyInjection { public static class SignalRDependencyInjectionExtensions { - public static ISignalRBuilder AddSignalR(this IServiceCollection services) + public static ISignalRServerBuilder AddSignalR(this IServiceCollection services) { services.AddConnections(); services.AddSingleton(); @@ -18,7 +18,7 @@ namespace Microsoft.Extensions.DependencyInjection .AddJsonProtocol(); } - public static ISignalRBuilder AddSignalR(this IServiceCollection services, Action options) + public static ISignalRServerBuilder AddSignalR(this IServiceCollection services, Action options) { return services.Configure(options) .AddSignalR(); diff --git a/test/Microsoft.AspNetCore.SignalR.Client.FunctionalTests/HubConnectionTests.cs b/test/Microsoft.AspNetCore.SignalR.Client.FunctionalTests/HubConnectionTests.cs index 5ff777e08e..ee5208252f 100644 --- a/test/Microsoft.AspNetCore.SignalR.Client.FunctionalTests/HubConnectionTests.cs +++ b/test/Microsoft.AspNetCore.SignalR.Client.FunctionalTests/HubConnectionTests.cs @@ -14,6 +14,7 @@ using Microsoft.AspNetCore.Http.Connections.Client; using Microsoft.AspNetCore.SignalR.Internal.Protocol; using Microsoft.AspNetCore.SignalR.Tests; using Microsoft.AspNetCore.Testing.xunit; +using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging.Testing; using Xunit; @@ -42,6 +43,25 @@ namespace Microsoft.AspNetCore.SignalR.Client.FunctionalTests _serverFixture = serverFixture; } + private HubConnection CreateHubConnection( + string path = null, + HttpTransportType? transportType = null, + IHubProtocol protocol = null, + ILoggerFactory loggerFactory = null) + { + var hubConnectionBuilder = new HubConnectionBuilder(); + hubConnectionBuilder.WithHubProtocol(protocol); + hubConnectionBuilder.WithLoggerFactory(loggerFactory); + hubConnectionBuilder.WithConnectionFactory(GetHttpConnectionFactory(loggerFactory, path, transportType ?? HttpTransportType.All)); + + return hubConnectionBuilder.Build(); + } + + private Func GetHttpConnectionFactory(ILoggerFactory loggerFactory, string path, HttpTransportType transportType) + { + return () => new HttpConnection(new Uri(_serverFixture.Url + path), transportType, loggerFactory); + } + [Theory] [MemberData(nameof(HubProtocolsAndTransportsAndHubPaths))] public async Task CheckFixedMessage(string protocolName, HttpTransportType transportType, string path) @@ -50,10 +70,9 @@ namespace Microsoft.AspNetCore.SignalR.Client.FunctionalTests using (StartLog(out var loggerFactory, $"{nameof(CheckFixedMessage)}_{protocol.Name}_{transportType}_{path.TrimStart('/')}")) { var connection = new HubConnectionBuilder() - .WithUrl(_serverFixture.Url + path) - .WithTransport(transportType) .WithLoggerFactory(loggerFactory) .WithHubProtocol(protocol) + .WithUrl(_serverFixture.Url + path, transportType) .Build(); try @@ -84,7 +103,7 @@ namespace Microsoft.AspNetCore.SignalR.Client.FunctionalTests using (StartLog(out var loggerFactory, $"{nameof(CanSendAndReceiveMessage)}_{protocol.Name}_{transportType}_{path.TrimStart('/')}")) { const string originalMessage = "SignalR"; - var connection = new HubConnection(GetHttpConnectionFactory(loggerFactory, path, transportType), protocol, loggerFactory); + var connection = CreateHubConnection(path, transportType, protocol, loggerFactory); try { await connection.StartAsync().OrTimeout(); @@ -113,7 +132,7 @@ namespace Microsoft.AspNetCore.SignalR.Client.FunctionalTests using (StartLog(out var loggerFactory, LogLevel.Trace, $"{nameof(CanStopAndStartConnection)}_{protocol.Name}_{transportType}_{path.TrimStart('/')}")) { const string originalMessage = "SignalR"; - var connection = new HubConnection(GetHttpConnectionFactory(loggerFactory, path, transportType), protocol, loggerFactory); + var connection = CreateHubConnection(path, transportType, protocol, loggerFactory); try { await connection.StartAsync().OrTimeout(); @@ -146,7 +165,7 @@ namespace Microsoft.AspNetCore.SignalR.Client.FunctionalTests var logger = loggerFactory.CreateLogger(); const string originalMessage = "SignalR"; - var connection = new HubConnection(GetHttpConnectionFactory(loggerFactory, "/default", transportType), new JsonHubProtocol(), loggerFactory); + var connection = CreateHubConnection(path, transportType, protocol, loggerFactory); var restartTcs = new TaskCompletionSource(); connection.Closed += async e => { @@ -189,11 +208,6 @@ namespace Microsoft.AspNetCore.SignalR.Client.FunctionalTests } } - private Func GetHttpConnectionFactory(ILoggerFactory loggerFactory, string path, HttpTransportType transportType) - { - return () => new HttpConnection(new Uri(_serverFixture.Url + path), transportType, loggerFactory); - } - [Theory] [MemberData(nameof(HubProtocolsAndTransportsAndHubPaths))] public async Task MethodsAreCaseInsensitive(string protocolName, HttpTransportType transportType, string path) @@ -202,8 +216,7 @@ namespace Microsoft.AspNetCore.SignalR.Client.FunctionalTests using (StartLog(out var loggerFactory, $"{nameof(MethodsAreCaseInsensitive)}_{protocol.Name}_{transportType}_{path.TrimStart('/')}")) { const string originalMessage = "SignalR"; - var uriString = "http://test/" + path; - var connection = new HubConnection(GetHttpConnectionFactory(loggerFactory, path, transportType), protocol, loggerFactory); + var connection = CreateHubConnection(path, transportType, protocol, loggerFactory); try { await connection.StartAsync().OrTimeout(); @@ -233,7 +246,7 @@ namespace Microsoft.AspNetCore.SignalR.Client.FunctionalTests { const string originalMessage = "SignalR"; - var connection = new HubConnection(GetHttpConnectionFactory(loggerFactory, path, transportType), protocol, loggerFactory); + var connection = CreateHubConnection(path, transportType, protocol, loggerFactory); try { await connection.StartAsync().OrTimeout(); @@ -264,7 +277,7 @@ namespace Microsoft.AspNetCore.SignalR.Client.FunctionalTests var protocol = HubProtocols[protocolName]; using (StartLog(out var loggerFactory, LogLevel.Trace, $"{nameof(InvokeNonExistantClientMethodFromServer)}_{protocol.Name}_{transportType}_{path.TrimStart('/')}")) { - var connection = new HubConnection(GetHttpConnectionFactory(loggerFactory, path, transportType), protocol, loggerFactory); + var connection = CreateHubConnection(path, transportType, protocol, loggerFactory); var closeTcs = new TaskCompletionSource(); connection.Closed += e => { @@ -304,7 +317,7 @@ namespace Microsoft.AspNetCore.SignalR.Client.FunctionalTests var protocol = HubProtocols[protocolName]; using (StartLog(out var loggerFactory, LogLevel.Trace, $"{nameof(CanStreamClientMethodFromServer)}_{protocol.Name}_{transportType}_{path.TrimStart('/')}")) { - var connection = new HubConnection(GetHttpConnectionFactory(loggerFactory, path, transportType), protocol, loggerFactory); + var connection = CreateHubConnection(path, transportType, protocol, loggerFactory); try { await connection.StartAsync().OrTimeout(); @@ -333,7 +346,7 @@ namespace Microsoft.AspNetCore.SignalR.Client.FunctionalTests var protocol = HubProtocols[protocolName]; using (StartLog(out var loggerFactory, $"{nameof(CanCloseStreamMethodEarly)}_{protocol.Name}_{transportType}_{path.TrimStart('/')}")) { - var connection = new HubConnection(GetHttpConnectionFactory(loggerFactory, path, transportType), protocol, loggerFactory); + var connection = CreateHubConnection(path, transportType, protocol, loggerFactory); try { await connection.StartAsync().OrTimeout(); @@ -373,10 +386,7 @@ namespace Microsoft.AspNetCore.SignalR.Client.FunctionalTests var protocol = HubProtocols[protocolName]; using (StartLog(out var loggerFactory, LogLevel.Trace, $"{nameof(StreamDoesNotStartIfTokenAlreadyCanceled)}_{protocol.Name}_{transportType}_{path.TrimStart('/')}")) { - var connection = - new HubConnection( - GetHttpConnectionFactory(loggerFactory, path, transportType), protocol, - loggerFactory); + var connection = CreateHubConnection(path, transportType, protocol, loggerFactory); try { await connection.StartAsync().OrTimeout(); @@ -408,7 +418,7 @@ namespace Microsoft.AspNetCore.SignalR.Client.FunctionalTests var protocol = HubProtocols[protocolName]; using (StartLog(out var loggerFactory, $"{nameof(ExceptionFromStreamingSentToClient)}_{protocol.Name}_{transportType}_{path.TrimStart('/')}")) { - var connection = new HubConnection(GetHttpConnectionFactory(loggerFactory, path, transportType), protocol, loggerFactory); + var connection = CreateHubConnection(path, transportType, protocol, loggerFactory); try { await connection.StartAsync().OrTimeout(); @@ -436,7 +446,7 @@ namespace Microsoft.AspNetCore.SignalR.Client.FunctionalTests var hubProtocol = HubProtocols[hubProtocolName]; using (StartLog(out var loggerFactory, $"{nameof(ServerThrowsHubExceptionIfHubMethodCannotBeResolved)}_{hubProtocol.Name}_{transportType}_{hubPath.TrimStart('/')}")) { - var connection = new HubConnection(GetHttpConnectionFactory(loggerFactory, hubPath, transportType), hubProtocol, loggerFactory); + var connection = CreateHubConnection(hubPath, transportType, hubProtocol, loggerFactory); try { await connection.StartAsync().OrTimeout(); @@ -463,7 +473,7 @@ namespace Microsoft.AspNetCore.SignalR.Client.FunctionalTests var hubProtocol = HubProtocols[hubProtocolName]; using (StartLog(out var loggerFactory, $"{nameof(ServerThrowsHubExceptionOnHubMethodArgumentCountMismatch)}_{hubProtocol.Name}_{transportType}_{hubPath.TrimStart('/')}")) { - var connection = new HubConnection(GetHttpConnectionFactory(loggerFactory, hubPath, transportType), hubProtocol, loggerFactory); + var connection = CreateHubConnection(hubPath, transportType, hubProtocol, loggerFactory); try { await connection.StartAsync().OrTimeout(); @@ -490,7 +500,7 @@ namespace Microsoft.AspNetCore.SignalR.Client.FunctionalTests var hubProtocol = HubProtocols[hubProtocolName]; using (StartLog(out var loggerFactory, $"{nameof(ServerThrowsHubExceptionOnHubMethodArgumentTypeMismatch)}_{hubProtocol.Name}_{transportType}_{hubPath.TrimStart('/')}")) { - var connection = new HubConnection(GetHttpConnectionFactory(loggerFactory, hubPath, transportType), hubProtocol, loggerFactory); + var connection = CreateHubConnection(hubPath, transportType, hubProtocol, loggerFactory); try { await connection.StartAsync().OrTimeout(); @@ -517,7 +527,7 @@ namespace Microsoft.AspNetCore.SignalR.Client.FunctionalTests var hubProtocol = HubProtocols[hubProtocolName]; using (StartLog(out var loggerFactory, $"{nameof(ServerThrowsHubExceptionIfStreamingHubMethodCannotBeResolved)}_{hubProtocol.Name}_{transportType}_{hubPath.TrimStart('/')}")) { - var connection = new HubConnection(GetHttpConnectionFactory(loggerFactory, hubPath, transportType), hubProtocol, loggerFactory); + var connection = CreateHubConnection(hubPath, transportType, hubProtocol, loggerFactory); try { await connection.StartAsync().OrTimeout(); @@ -546,7 +556,7 @@ namespace Microsoft.AspNetCore.SignalR.Client.FunctionalTests using (StartLog(out var loggerFactory, $"{nameof(ServerThrowsHubExceptionOnStreamingHubMethodArgumentCountMismatch)}_{hubProtocol.Name}_{transportType}_{hubPath.TrimStart('/')}")) { loggerFactory.AddConsole(LogLevel.Trace); - var connection = new HubConnection(GetHttpConnectionFactory(loggerFactory, hubPath, transportType), hubProtocol, loggerFactory); + var connection = CreateHubConnection(hubPath, transportType, hubProtocol, loggerFactory); try { await connection.StartAsync().OrTimeout(); @@ -574,7 +584,7 @@ namespace Microsoft.AspNetCore.SignalR.Client.FunctionalTests var hubProtocol = HubProtocols[hubProtocolName]; using (StartLog(out var loggerFactory, $"{nameof(ServerThrowsHubExceptionOnStreamingHubMethodArgumentTypeMismatch)}_{hubProtocol.Name}_{transportType}_{hubPath.TrimStart('/')}")) { - var connection = new HubConnection(GetHttpConnectionFactory(loggerFactory, hubPath, transportType), hubProtocol, loggerFactory); + var connection = CreateHubConnection(hubPath, transportType, hubProtocol, loggerFactory); try { await connection.StartAsync().OrTimeout(); @@ -602,7 +612,7 @@ namespace Microsoft.AspNetCore.SignalR.Client.FunctionalTests var hubProtocol = HubProtocols[hubProtocolName]; using (StartLog(out var loggerFactory, $"{nameof(ServerThrowsHubExceptionIfNonStreamMethodInvokedWithStreamAsync)}_{hubProtocol.Name}_{transportType}_{hubPath.TrimStart('/')}")) { - var connection = new HubConnection(GetHttpConnectionFactory(loggerFactory, hubPath, transportType), hubProtocol, loggerFactory); + var connection = CreateHubConnection(hubPath, transportType, hubProtocol, loggerFactory); try { await connection.StartAsync().OrTimeout(); @@ -629,7 +639,7 @@ namespace Microsoft.AspNetCore.SignalR.Client.FunctionalTests var hubProtocol = HubProtocols[hubProtocolName]; using (StartLog(out var loggerFactory, $"{nameof(ServerThrowsHubExceptionIfStreamMethodInvokedWithInvoke)}_{hubProtocol.Name}_{transportType}_{hubPath.TrimStart('/')}")) { - var connection = new HubConnection(GetHttpConnectionFactory(loggerFactory, hubPath, transportType), hubProtocol, loggerFactory); + var connection = CreateHubConnection(hubPath, transportType, hubProtocol, loggerFactory); try { await connection.StartAsync().OrTimeout(); @@ -656,7 +666,7 @@ namespace Microsoft.AspNetCore.SignalR.Client.FunctionalTests var hubProtocol = HubProtocols[hubProtocolName]; using (StartLog(out var loggerFactory, $"{nameof(ServerThrowsHubExceptionIfBuildingAsyncEnumeratorIsNotPossible)}_{hubProtocol.Name}_{transportType}_{hubPath.TrimStart('/')}")) { - var connection = new HubConnection(GetHttpConnectionFactory(loggerFactory, hubPath, transportType), hubProtocol, loggerFactory); + var connection = CreateHubConnection(hubPath, transportType, hubProtocol, loggerFactory); try { await connection.StartAsync().OrTimeout(); @@ -687,10 +697,11 @@ namespace Microsoft.AspNetCore.SignalR.Client.FunctionalTests var token = await httpResponse.Content.ReadAsStringAsync(); var hubConnection = new HubConnectionBuilder() - .WithUrl(_serverFixture.Url + "/authorizedhub") - .WithTransport(transportType) .WithLoggerFactory(loggerFactory) - .WithAccessToken(() => token) + .WithUrl(_serverFixture.Url + "/authorizedhub", transportType, options => + { + options.AccessTokenFactory = () => token; + }) .Build(); try { @@ -717,11 +728,12 @@ namespace Microsoft.AspNetCore.SignalR.Client.FunctionalTests using (StartLog(out var loggerFactory, $"{nameof(ClientCanSendHeaders)}_{transportType}")) { var hubConnection = new HubConnectionBuilder() - .WithUrl(_serverFixture.Url + "/default") - .WithTransport(transportType) .WithLoggerFactory(loggerFactory) - .WithHeader("X-test", "42") - .WithHeader("X-42", "test") + .WithUrl(_serverFixture.Url + "/default", transportType, options => + { + options.Headers["X-test"] = "42"; + options.Headers["X-42"] = "test"; + }) .Build(); try { @@ -747,15 +759,16 @@ namespace Microsoft.AspNetCore.SignalR.Client.FunctionalTests { using (StartLog(out var loggerFactory, $"{nameof(WebSocketOptionsAreApplied)}")) { - // System.Net has a TransportType type which means we need to fully-qualify this rather than 'use' the namespace + // System.Net has a HttpTransportType type which means we need to fully-qualify this rather than 'use' the namespace var cookieJar = new System.Net.CookieContainer(); cookieJar.Add(new System.Net.Cookie("Foo", "Bar", "/", new Uri(_serverFixture.Url).Host)); var hubConnection = new HubConnectionBuilder() - .WithUrl(_serverFixture.Url + "/default") - .WithTransport(HttpTransportType.WebSockets) .WithLoggerFactory(loggerFactory) - .WithWebSocketOptions(options => options.Cookies = cookieJar) + .WithUrl(_serverFixture.Url + "/default", HttpTransportType.WebSockets, options => + { + options.WebSocketOptions = o => o.Cookies = cookieJar; + }) .Build(); try { @@ -782,9 +795,8 @@ namespace Microsoft.AspNetCore.SignalR.Client.FunctionalTests using (StartLog(out var loggerFactory, $"{nameof(CheckHttpConnectionFeatures)}_{transportType}")) { var hubConnection = new HubConnectionBuilder() - .WithUrl(_serverFixture.Url + "/default") - .WithTransport(transportType) .WithLoggerFactory(loggerFactory) + .WithUrl(_serverFixture.Url + "/default", transportType) .Build(); try { @@ -819,9 +831,9 @@ namespace Microsoft.AspNetCore.SignalR.Client.FunctionalTests using (StartLog(out var loggerFactory)) { var hubConnection = new HubConnectionBuilder() - .WithUrl(_serverFixture.Url + "/default-nowebsockets") - .WithHubProtocol(new MessagePackHubProtocol()) .WithLoggerFactory(loggerFactory) + .WithHubProtocol(new MessagePackHubProtocol()) + .WithUrl(_serverFixture.Url + "/default-nowebsockets") .Build(); try { diff --git a/test/Microsoft.AspNetCore.SignalR.Client.FunctionalTests/Microsoft.AspNetCore.SignalR.Client.FunctionalTests.csproj b/test/Microsoft.AspNetCore.SignalR.Client.FunctionalTests/Microsoft.AspNetCore.SignalR.Client.FunctionalTests.csproj index 1dbad6f72e..43a83b0f72 100644 --- a/test/Microsoft.AspNetCore.SignalR.Client.FunctionalTests/Microsoft.AspNetCore.SignalR.Client.FunctionalTests.csproj +++ b/test/Microsoft.AspNetCore.SignalR.Client.FunctionalTests/Microsoft.AspNetCore.SignalR.Client.FunctionalTests.csproj @@ -15,7 +15,6 @@ - diff --git a/test/Microsoft.AspNetCore.SignalR.Client.Tests/HttpConnectionTests.Helpers.cs b/test/Microsoft.AspNetCore.SignalR.Client.Tests/HttpConnectionTests.Helpers.cs index 7a7171583d..d7459e7de9 100644 --- a/test/Microsoft.AspNetCore.SignalR.Client.Tests/HttpConnectionTests.Helpers.cs +++ b/test/Microsoft.AspNetCore.SignalR.Client.Tests/HttpConnectionTests.Helpers.cs @@ -17,7 +17,7 @@ namespace Microsoft.AspNetCore.SignalR.Client.Tests { var httpOptions = new HttpOptions() { - HttpMessageHandler = (httpMessageHandler) => httpHandler ?? TestHttpMessageHandler.CreateDefault(), + HttpMessageHandlerFactory = (httpMessageHandler) => httpHandler ?? TestHttpMessageHandler.CreateDefault(), }; return CreateConnection(httpOptions, loggerFactory, url, transport, transportFactory, transportType); diff --git a/test/Microsoft.AspNetCore.SignalR.Client.Tests/HttpConnectionTests.cs b/test/Microsoft.AspNetCore.SignalR.Client.Tests/HttpConnectionTests.cs index 7ea566ff1d..e8604465f9 100644 --- a/test/Microsoft.AspNetCore.SignalR.Client.Tests/HttpConnectionTests.cs +++ b/test/Microsoft.AspNetCore.SignalR.Client.Tests/HttpConnectionTests.cs @@ -63,7 +63,7 @@ namespace Microsoft.AspNetCore.SignalR.Client.Tests HttpClientHandler httpClientHandler = null; var httpOptions = new HttpOptions(); - httpOptions.HttpMessageHandler = inner => + httpOptions.HttpMessageHandlerFactory = inner => { httpClientHandler = (HttpClientHandler)inner; return testHttpHandler; @@ -99,7 +99,7 @@ namespace Microsoft.AspNetCore.SignalR.Client.Tests testHttpHandler.OnNegotiate((request, cancellationToken) => ResponseUtils.CreateResponse(HttpStatusCode.BadGateway)); var httpOptions = new HttpOptions(); - httpOptions.HttpMessageHandler = inner => testHttpHandler; + httpOptions.HttpMessageHandlerFactory = inner => testHttpHandler; const string loggerName = "Microsoft.AspNetCore.Http.Connections.Client.Internal.LoggingHttpMessageHandler"; var testSink = new TestSink(); diff --git a/test/Microsoft.AspNetCore.SignalR.Client.Tests/HubConnectionBuilderExtensionsTests.cs b/test/Microsoft.AspNetCore.SignalR.Client.Tests/HubConnectionBuilderExtensionsTests.cs index 8c690ededf..fc966a0b39 100644 --- a/test/Microsoft.AspNetCore.SignalR.Client.Tests/HubConnectionBuilderExtensionsTests.cs +++ b/test/Microsoft.AspNetCore.SignalR.Client.Tests/HubConnectionBuilderExtensionsTests.cs @@ -2,14 +2,12 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; -using System.Linq; using System.Net; -using System.Net.Http; -using System.Security.Cryptography.X509Certificates; using Microsoft.AspNetCore.Http.Connections; using Microsoft.AspNetCore.SignalR.Internal.Protocol; +using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; -using Microsoft.Extensions.Logging.Console; +using Microsoft.Extensions.Options; using Moq; using Xunit; @@ -18,114 +16,100 @@ namespace Microsoft.AspNetCore.SignalR.Client.Tests public class HubConnectionBuilderExtensionsTests { [Fact] - public void WithProxyRegistersGivenProxy() + public void WithHttpConnectionSetsUrl() { var connectionBuilder = new HubConnectionBuilder(); + connectionBuilder.WithUrl("http://tempuri.org"); + + var serviceProvider = connectionBuilder.Services.BuildServiceProvider(); + + var value = serviceProvider.GetService>().Value; + + Assert.Equal(new Uri("http://tempuri.org"), value.Url); + } + + [Fact] + public void WithHttpConnectionSetsTransport() + { + var connectionBuilder = new HubConnectionBuilder(); + connectionBuilder.WithUrl("http://tempuri.org", HttpTransportType.LongPolling); + + var serviceProvider = connectionBuilder.Services.BuildServiceProvider(); + + var value = serviceProvider.GetService>().Value; + + Assert.Equal(HttpTransportType.LongPolling, value.Transport); + } + + [Fact] + public void WithHttpConnectionCallsConfigure() + { var proxy = Mock.Of(); - connectionBuilder.WithProxy(proxy); - Assert.Same(proxy, connectionBuilder.GetProxy()); + + var connectionBuilder = new HubConnectionBuilder(); + connectionBuilder.WithUrl("http://tempuri.org", options => { options.Proxy = proxy; }); + + var serviceProvider = connectionBuilder.Services.BuildServiceProvider(); + + var value = serviceProvider.GetService>().Value; + + Assert.Same(proxy, value.Proxy); } [Fact] - public void WithCredentialsRegistersGivenCredentials() + public void WithConsoleLoggerAddsLogger() { + var loggingFactory = Mock.Of(); + var connectionBuilder = new HubConnectionBuilder(); - var credentials = Mock.Of(); - connectionBuilder.WithCredentials(credentials); - Assert.Same(credentials, connectionBuilder.GetCredentials()); + connectionBuilder.WithLoggerFactory(loggingFactory); + + var serviceProvider = connectionBuilder.Services.BuildServiceProvider(); + + var resolvedLoggingFactory = serviceProvider.GetService(); + + Assert.Same(resolvedLoggingFactory, loggingFactory); } [Fact] - public void WithUseDefaultCredentialsRegistersGivenUseDefaultCredentials() + public void WithHubProtocolAddsProtocol() { - var connectionBuilder = new HubConnectionBuilder(); - var useDefaultCredentials = true; - connectionBuilder.WithUseDefaultCredentials(useDefaultCredentials); - Assert.Equal(useDefaultCredentials, connectionBuilder.GetUseDefaultCredentials()); - } - - [Fact] - public void WithClientCertificateRegistersGivenClientCertificate() - { - var connectionBuilder = new HubConnectionBuilder(); - var certificate = new X509Certificate(); - connectionBuilder.WithClientCertificate(certificate); - Assert.Contains(certificate, connectionBuilder.GetClientCertificates().Cast()); - } - - [Fact] - public void WithCookieRegistersGivenCookie() - { - var connectionBuilder = new HubConnectionBuilder(); - var cookie = new Cookie("Name!", "Value!", string.Empty, "www.contoso.com"); - connectionBuilder.WithCookie(cookie); - Assert.Equal(1, connectionBuilder.GetCookies().Count); - } - - [Fact] - public void WithHubProtocolRegistersGivenProtocol() - { - var connectionBuilder = new HubConnectionBuilder(); var hubProtocol = Mock.Of(); + + var connectionBuilder = new HubConnectionBuilder(); connectionBuilder.WithHubProtocol(hubProtocol); - Assert.Same(hubProtocol, connectionBuilder.GetHubProtocol()); + + var serviceProvider = connectionBuilder.Services.BuildServiceProvider(); + + var resolvedHubProtocol = serviceProvider.GetService(); + + Assert.Same(hubProtocol, resolvedHubProtocol); } [Fact] - public void WithJsonProtocolRegistersJsonProtocol() + public void AddJsonProtocolAddsProtocol() { var connectionBuilder = new HubConnectionBuilder(); - connectionBuilder.WithJsonProtocol(); - Assert.IsType(connectionBuilder.GetHubProtocol()); + connectionBuilder.AddJsonProtocol(); + + var serviceProvider = connectionBuilder.Services.BuildServiceProvider(); + + var resolvedHubProtocol = serviceProvider.GetService(); + + Assert.IsType(resolvedHubProtocol); } [Fact] - public void WithMessagePackProtocolRegistersMessagePackProtocol() + public void AddMessagePackProtocolAddsProtocol() { var connectionBuilder = new HubConnectionBuilder(); - connectionBuilder.WithMessagePackProtocol(); - Assert.IsType(connectionBuilder.GetHubProtocol()); - } + connectionBuilder.AddMessagePackProtocol(); - [Fact] - public void WithLoggerRegistersGivenLogger() - { - var connectionBuilder = new HubConnectionBuilder(); - var loggerFactory = Mock.Of(); - connectionBuilder.WithLoggerFactory(loggerFactory); - Assert.Same(loggerFactory, connectionBuilder.GetLoggerFactory()); - } + var serviceProvider = connectionBuilder.Services.BuildServiceProvider(); - [Fact] - public void WithConsoleLoggerRegistersConsoleLogger() - { - var connectionBuilder = new HubConnectionBuilder(); - var mockLoggerFactory = new Mock(); - connectionBuilder.WithLoggerFactory(mockLoggerFactory.Object); - connectionBuilder.WithConsoleLogger(); - mockLoggerFactory.Verify(f => f.AddProvider(It.IsAny()), Times.Once); - } + var resolvedHubProtocol = serviceProvider.GetService(); - [Fact] - public void WithMsgHandlerRegistersGivenMessageHandler() - { - var messageHandler = new Func(httpMessageHandler => default); - - var connectionBuilder = new HubConnectionBuilder(); - connectionBuilder.WithMessageHandler(messageHandler); - Assert.Same(messageHandler, connectionBuilder.GetMessageHandler()); - } - - [Theory] - [InlineData(HttpTransportType.All)] - [InlineData(HttpTransportType.WebSockets)] - [InlineData(HttpTransportType.ServerSentEvents)] - [InlineData(HttpTransportType.LongPolling)] - public void WithTransportRegistersGivenTransportType(HttpTransportType transportType) - { - var connectionBuilder = new HubConnectionBuilder(); - connectionBuilder.WithTransport(transportType); - Assert.Equal(transportType, connectionBuilder.GetTransport()); + Assert.IsType(resolvedHubProtocol); } } -} +} \ No newline at end of file diff --git a/test/Microsoft.AspNetCore.SignalR.Client.Tests/HubConnectionBuilderTests.cs b/test/Microsoft.AspNetCore.SignalR.Client.Tests/HubConnectionBuilderTests.cs index 25996b9452..958e1c7237 100644 --- a/test/Microsoft.AspNetCore.SignalR.Client.Tests/HubConnectionBuilderTests.cs +++ b/test/Microsoft.AspNetCore.SignalR.Client.Tests/HubConnectionBuilderTests.cs @@ -2,7 +2,10 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; +using System.Linq; +using Microsoft.AspNetCore.Connections; using Microsoft.AspNetCore.SignalR.Internal.Protocol; +using Microsoft.Extensions.DependencyInjection; using MsgPack.Serialization; using Newtonsoft.Json; using Newtonsoft.Json.Serialization; @@ -16,16 +19,7 @@ namespace Microsoft.AspNetCore.SignalR.Client.Tests public void HubConnectionBuiderThrowsIfConnectionFactoryNotConfigured() { var ex = Assert.Throws(() => new HubConnectionBuilder().Build()); - Assert.Equal("Cannot create IConnection instance. The connection factory was not configured.", ex.Message); - } - - [Fact] - public void WithUrlThrowsForNullUrls() - { - Assert.Equal("url", - Assert.Throws(() => new HubConnectionBuilder().WithUrl((string)null)).ParamName); - Assert.Equal("url", - Assert.Throws(() => new HubConnectionBuilder().WithUrl((Uri)null)).ParamName); + Assert.Equal("Cannot create HubConnection instance. A connection was not configured.", ex.Message); } [Fact] @@ -36,50 +30,71 @@ namespace Microsoft.AspNetCore.SignalR.Client.Tests } [Fact] - public void WithJsonHubProtocolSetsHubProtocolToJsonWithDefaultOptions() + public void AddJsonProtocolSetsHubProtocolToJsonWithDefaultOptions() { - Assert.True(new HubConnectionBuilder().WithJsonProtocol().TryGetSetting(HubConnectionBuilderDefaults.HubProtocolKey, out var hubProtocol)); - var actualProtocol = Assert.IsType(hubProtocol); + var serviceProvider = new HubConnectionBuilder().AddJsonProtocol().Services.BuildServiceProvider(); + + var actualProtocol = Assert.IsType(serviceProvider.GetService()); Assert.IsType(actualProtocol.PayloadSerializer.ContractResolver); } [Fact] - public void WithJsonHubProtocolSetsHubProtocolToJsonWithProvidedOptions() + public void AddJsonProtocolSetsHubProtocolToJsonWithProvidedOptions() { - var expectedOptions = new JsonHubProtocolOptions() + var serviceProvider = new HubConnectionBuilder().AddJsonProtocol(options => { - PayloadSerializerSettings = new JsonSerializerSettings() + options.PayloadSerializerSettings = new JsonSerializerSettings { DateFormatString = "JUST A TEST" - } - }; + }; + }).Services.BuildServiceProvider(); - Assert.True(new HubConnectionBuilder().WithJsonProtocol(expectedOptions).TryGetSetting(HubConnectionBuilderDefaults.HubProtocolKey, out var hubProtocol)); - var actualProtocol = Assert.IsType(hubProtocol); + var actualProtocol = Assert.IsType(serviceProvider.GetService()); Assert.Equal("JUST A TEST", actualProtocol.PayloadSerializer.DateFormatString); } [Fact] - public void WithMessagePackHubProtocolSetsHubProtocolToMsgPackWithDefaultOptions() + public void WithConnectionFactorySetsConnectionFactory() { - Assert.True(new HubConnectionBuilder().WithMessagePackProtocol().TryGetSetting(HubConnectionBuilderDefaults.HubProtocolKey, out var hubProtocol)); - var actualProtocol = Assert.IsType(hubProtocol); + Func connectionFactory = () => null; + + var serviceProvider = new HubConnectionBuilder().WithConnectionFactory(connectionFactory).Services.BuildServiceProvider(); + + Assert.Equal(connectionFactory, serviceProvider.GetService>()); + } + + [Fact] + public void BuildCanOnlyBeCalledOnce() + { + var builder = new HubConnectionBuilder().WithConnectionFactory(() => null); + + Assert.NotNull(builder.Build()); + + var ex = Assert.Throws(() => builder.Build()); + Assert.Equal("HubConnectionBuilder allows creation only of a single instance of HubConnection.", ex.Message); + } + + [Fact] + public void AddMessagePackProtocolSetsHubProtocolToMsgPackWithDefaultOptions() + { + var serviceProvider = new HubConnectionBuilder().AddMessagePackProtocol().Services.BuildServiceProvider(); + + var actualProtocol = Assert.IsType(serviceProvider.GetService()); Assert.Equal(SerializationMethod.Map, actualProtocol.SerializationContext.SerializationMethod); } [Fact] - public void WithMessagePackHubProtocolSetsHubProtocolToMsgPackWithProvidedOptions() + public void AddMessagePackProtocolSetsHubProtocolToMsgPackWithProvidedOptions() { - var expectedOptions = new MessagePackHubProtocolOptions() + var serviceProvider = new HubConnectionBuilder().AddMessagePackProtocol(options => { - SerializationContext = new SerializationContext() + options.SerializationContext = new SerializationContext { SerializationMethod = SerializationMethod.Array - } - }; + }; + }).Services.BuildServiceProvider(); - Assert.True(new HubConnectionBuilder().WithMessagePackProtocol(expectedOptions).TryGetSetting(HubConnectionBuilderDefaults.HubProtocolKey, out var hubProtocol)); - var actualProtocol = Assert.IsType(hubProtocol); + var actualProtocol = Assert.IsType(serviceProvider.GetService()); Assert.Equal(SerializationMethod.Array, actualProtocol.SerializationContext.SerializationMethod); } } diff --git a/test/Microsoft.AspNetCore.SignalR.Client.Tests/HubConnectionTests.ConnectionLifecycle.cs b/test/Microsoft.AspNetCore.SignalR.Client.Tests/HubConnectionTests.ConnectionLifecycle.cs index a672ec4bcc..13882a283b 100644 --- a/test/Microsoft.AspNetCore.SignalR.Client.Tests/HubConnectionTests.ConnectionLifecycle.cs +++ b/test/Microsoft.AspNetCore.SignalR.Client.Tests/HubConnectionTests.ConnectionLifecycle.cs @@ -4,6 +4,7 @@ using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Connections; using Microsoft.AspNetCore.SignalR.Internal.Protocol; +using Microsoft.Extensions.DependencyInjection; using Newtonsoft.Json.Linq; using Xunit; @@ -23,11 +24,18 @@ namespace Microsoft.AspNetCore.SignalR.Client.Tests public static IEnumerable MethodsNamesThatRequireActiveConnection => MethodsThatRequireActiveConnection.Keys.Select(k => new object[] { k }); + private HubConnection CreateHubConnection(Func connectionFactory) + { + var builder = new HubConnectionBuilder(); + builder.WithConnectionFactory(connectionFactory); + return builder.Build(); + } + [Fact] public async Task StartAsyncStartsTheUnderlyingConnection() { var testConnection = new TestConnection(); - await AsyncUsing(new HubConnection(() => testConnection, new JsonHubProtocol()), async connection => + await AsyncUsing(CreateHubConnection(() => testConnection), async connection => { await connection.StartAsync(); Assert.True(testConnection.Started.IsCompleted); @@ -39,7 +47,7 @@ namespace Microsoft.AspNetCore.SignalR.Client.Tests { // Set up StartAsync to wait on the syncPoint when starting var testConnection = new TestConnection(onStart: SyncPoint.Create(out var syncPoint)); - await AsyncUsing(new HubConnection(() => testConnection, new JsonHubProtocol()), async connection => + await AsyncUsing(CreateHubConnection(() => testConnection), async connection => { var firstStart = connection.StartAsync().OrTimeout(); Assert.False(firstStart.IsCompleted); @@ -71,7 +79,7 @@ namespace Microsoft.AspNetCore.SignalR.Client.Tests return new TestConnection(); } - await AsyncUsing(new HubConnection(ConnectionFactory, new JsonHubProtocol()), async connection => + await AsyncUsing(CreateHubConnection(ConnectionFactory), async connection => { await connection.StartAsync().OrTimeout(); Assert.Equal(1, createCount); @@ -94,7 +102,7 @@ namespace Microsoft.AspNetCore.SignalR.Client.Tests return new TestConnection(onDispose: createCount == 1 ? onDisposeForFirstConnection : null); } - await AsyncUsing(new HubConnection(ConnectionFactory, new JsonHubProtocol()), async connection => + await AsyncUsing(CreateHubConnection(ConnectionFactory), async connection => { await connection.StartAsync().OrTimeout(); Assert.Equal(1, createCount); @@ -119,7 +127,7 @@ namespace Microsoft.AspNetCore.SignalR.Client.Tests public async Task StartAsyncWithFailedHandshakeCanBeStopped() { var testConnection = new TestConnection(autoHandshake: false); - await AsyncUsing(new HubConnection(() => testConnection, new JsonHubProtocol()), async connection => + await AsyncUsing(CreateHubConnection(() => testConnection), async connection => { testConnection.Transport.Input.Complete(); try @@ -141,7 +149,7 @@ namespace Microsoft.AspNetCore.SignalR.Client.Tests var method = MethodsThatRequireActiveConnection[name]; var testConnection = new TestConnection(); - await AsyncUsing(new HubConnection(() => testConnection, new JsonHubProtocol()), async connection => + await AsyncUsing(CreateHubConnection(() => testConnection), async connection => { var ex = await Assert.ThrowsAsync(() => method(connection)); Assert.Equal($"The '{name}' method cannot be called if the connection is not active", ex.Message); @@ -156,7 +164,7 @@ namespace Microsoft.AspNetCore.SignalR.Client.Tests // Set up StartAsync to wait on the syncPoint when starting var testConnection = new TestConnection(onStart: SyncPoint.Create(out var syncPoint)); - await AsyncUsing(new HubConnection(() => testConnection, new JsonHubProtocol()), async connection => + await AsyncUsing(CreateHubConnection(() => testConnection), async connection => { // Start, and wait for the sync point to be hit var startTask = connection.StartAsync().OrTimeout(); @@ -187,7 +195,7 @@ namespace Microsoft.AspNetCore.SignalR.Client.Tests public async Task StopAsyncStopsConnection() { var testConnection = new TestConnection(); - await AsyncUsing(new HubConnection(() => testConnection, new JsonHubProtocol()), async connection => + await AsyncUsing(CreateHubConnection(() => testConnection), async connection => { await connection.StartAsync().OrTimeout(); Assert.True(testConnection.Started.IsCompleted); @@ -201,7 +209,7 @@ namespace Microsoft.AspNetCore.SignalR.Client.Tests public async Task StopAsyncNoOpsIfConnectionNotYetStarted() { var testConnection = new TestConnection(); - await AsyncUsing(new HubConnection(() => testConnection, new JsonHubProtocol()), async connection => + await AsyncUsing(CreateHubConnection(() => testConnection), async connection => { await connection.StopAsync().OrTimeout(); Assert.False(testConnection.Disposed.IsCompleted); @@ -212,7 +220,7 @@ namespace Microsoft.AspNetCore.SignalR.Client.Tests public async Task StopAsyncNoOpsIfConnectionAlreadyStopped() { var testConnection = new TestConnection(); - await AsyncUsing(new HubConnection(() => testConnection, new JsonHubProtocol()), async connection => + await AsyncUsing(CreateHubConnection(() => testConnection), async connection => { await connection.StartAsync().OrTimeout(); Assert.True(testConnection.Started.IsCompleted); @@ -229,7 +237,7 @@ namespace Microsoft.AspNetCore.SignalR.Client.Tests { var testConnection = new TestConnection(); var closed = new TaskCompletionSource(); - await AsyncUsing(new HubConnection(() => testConnection, new JsonHubProtocol()), async connection => + await AsyncUsing(CreateHubConnection(() => testConnection), async connection => { connection.Closed += (e) => closed.TrySetResult(null); await connection.StartAsync().OrTimeout(); @@ -251,7 +259,7 @@ namespace Microsoft.AspNetCore.SignalR.Client.Tests var testConnection = new TestConnection(); var testConnectionClosed = new TaskCompletionSource(); var connectionClosed = new TaskCompletionSource(); - await AsyncUsing(new HubConnection(() => testConnection, new JsonHubProtocol()), async connection => + await AsyncUsing(CreateHubConnection(() => testConnection), async connection => { // We're hooking the TestConnection shutting down here because the HubConnection one will be blocked on the lock testConnection.Transport.Input.OnWriterCompleted((_, __) => testConnectionClosed.TrySetResult(null), null); @@ -286,7 +294,7 @@ namespace Microsoft.AspNetCore.SignalR.Client.Tests { var testConnection = new TestConnection(); var connectionClosed = new TaskCompletionSource(); - await AsyncUsing(new HubConnection(() => testConnection, new JsonHubProtocol()), async connection => + await AsyncUsing(CreateHubConnection(() => testConnection), async connection => { connection.Closed += (e) => connectionClosed.TrySetResult(null); @@ -319,7 +327,7 @@ namespace Microsoft.AspNetCore.SignalR.Client.Tests // Set up StartAsync to wait on the syncPoint when starting var testConnection = new TestConnection(onDispose: SyncPoint.Create(out var syncPoint)); - await AsyncUsing(new HubConnection(() => testConnection, new JsonHubProtocol()), async connection => + await AsyncUsing(CreateHubConnection(() => testConnection), async connection => { await connection.StartAsync().OrTimeout(); diff --git a/test/Microsoft.AspNetCore.SignalR.Client.Tests/HubConnectionTests.Helpers.cs b/test/Microsoft.AspNetCore.SignalR.Client.Tests/HubConnectionTests.Helpers.cs index 6aa20afff0..62ce394625 100644 --- a/test/Microsoft.AspNetCore.SignalR.Client.Tests/HubConnectionTests.Helpers.cs +++ b/test/Microsoft.AspNetCore.SignalR.Client.Tests/HubConnectionTests.Helpers.cs @@ -1,7 +1,9 @@ using System; using System.Collections.Generic; using System.Text; +using Microsoft.AspNetCore.Connections; using Microsoft.AspNetCore.SignalR.Internal.Protocol; +using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; namespace Microsoft.AspNetCore.SignalR.Client.Tests @@ -10,7 +12,14 @@ namespace Microsoft.AspNetCore.SignalR.Client.Tests { private static HubConnection CreateHubConnection(TestConnection connection, IHubProtocol protocol = null) { - return new HubConnection(() => connection, protocol ?? new JsonHubProtocol(), new LoggerFactory()); + var builder = new HubConnectionBuilder(); + builder.WithConnectionFactory(() => connection); + if (protocol != null) + { + builder.WithHubProtocol(protocol); + } + + return builder.Build(); } } } diff --git a/test/Microsoft.AspNetCore.SignalR.Client.Tests/HubConnectionTests.cs b/test/Microsoft.AspNetCore.SignalR.Client.Tests/HubConnectionTests.cs index d7701603b9..cf2a49955f 100644 --- a/test/Microsoft.AspNetCore.SignalR.Client.Tests/HubConnectionTests.cs +++ b/test/Microsoft.AspNetCore.SignalR.Client.Tests/HubConnectionTests.cs @@ -8,6 +8,7 @@ using System.Threading.Tasks; using Microsoft.AspNetCore.Connections; using Microsoft.AspNetCore.SignalR.Internal; using Microsoft.AspNetCore.SignalR.Internal.Protocol; +using Microsoft.Extensions.DependencyInjection; using Moq; using Xunit; @@ -42,7 +43,10 @@ namespace Microsoft.AspNetCore.SignalR.Client.Tests [Fact] public async Task ClosedEventRaisedWhenTheClientIsStopped() { - var hubConnection = new HubConnection(() => new TestConnection(), Mock.Of(), null); + var builder = new HubConnectionBuilder(); + builder.WithConnectionFactory(() => new TestConnection()); + + var hubConnection = builder.Build(); var closedEventTcs = new TaskCompletionSource(); hubConnection.Closed += e => closedEventTcs.SetResult(e); diff --git a/test/Microsoft.AspNetCore.SignalR.Client.Tests/Microsoft.AspNetCore.SignalR.Client.Tests.csproj b/test/Microsoft.AspNetCore.SignalR.Client.Tests/Microsoft.AspNetCore.SignalR.Client.Tests.csproj index ac67f9b7a7..4ebefdd9b0 100644 --- a/test/Microsoft.AspNetCore.SignalR.Client.Tests/Microsoft.AspNetCore.SignalR.Client.Tests.csproj +++ b/test/Microsoft.AspNetCore.SignalR.Client.Tests/Microsoft.AspNetCore.SignalR.Client.Tests.csproj @@ -11,7 +11,6 @@ - diff --git a/test/Microsoft.AspNetCore.SignalR.Redis.Tests/Microsoft.AspNetCore.SignalR.Redis.Tests.csproj b/test/Microsoft.AspNetCore.SignalR.Redis.Tests/Microsoft.AspNetCore.SignalR.Redis.Tests.csproj index 35cc000165..a7248dfc65 100644 --- a/test/Microsoft.AspNetCore.SignalR.Redis.Tests/Microsoft.AspNetCore.SignalR.Redis.Tests.csproj +++ b/test/Microsoft.AspNetCore.SignalR.Redis.Tests/Microsoft.AspNetCore.SignalR.Redis.Tests.csproj @@ -11,7 +11,6 @@ - diff --git a/test/Microsoft.AspNetCore.SignalR.Redis.Tests/RedisEndToEnd.cs b/test/Microsoft.AspNetCore.SignalR.Redis.Tests/RedisEndToEnd.cs index 4f05f9cc96..93b9702526 100644 --- a/test/Microsoft.AspNetCore.SignalR.Redis.Tests/RedisEndToEnd.cs +++ b/test/Microsoft.AspNetCore.SignalR.Redis.Tests/RedisEndToEnd.cs @@ -92,10 +92,9 @@ namespace Microsoft.AspNetCore.SignalR.Redis.Tests private static HubConnection CreateConnection(string url, HttpTransportType transportType, IHubProtocol protocol, ILoggerFactory loggerFactory) { return new HubConnectionBuilder() - .WithUrl(url) - .WithTransport(transportType) .WithHubProtocol(protocol) .WithLoggerFactory(loggerFactory) + .WithUrl(url, transportType) .Build(); } diff --git a/test/Microsoft.AspNetCore.SignalR.Tests/EndToEndTests.cs b/test/Microsoft.AspNetCore.SignalR.Tests/EndToEndTests.cs index 367aca1a5d..47ff1b8058 100644 --- a/test/Microsoft.AspNetCore.SignalR.Tests/EndToEndTests.cs +++ b/test/Microsoft.AspNetCore.SignalR.Tests/EndToEndTests.cs @@ -170,7 +170,7 @@ namespace Microsoft.AspNetCore.SignalR.Tests .Returns( (request, cancellationToken) => Task.FromException(new InvalidOperationException("HTTP requests should not be sent."))); - var connection = new HttpConnection(new Uri(url), HttpTransportType.WebSockets, loggerFactory, new HttpOptions { HttpMessageHandler = (httpMessageHandler) => mockHttpHandler.Object }); + var connection = new HttpConnection(new Uri(url), HttpTransportType.WebSockets, loggerFactory, new HttpOptions { HttpMessageHandlerFactory = (httpMessageHandler) => mockHttpHandler.Object }); try { @@ -339,9 +339,8 @@ namespace Microsoft.AspNetCore.SignalR.Tests var url = _serverFixture.Url + "/uncreatable"; var connection = new HubConnectionBuilder() - .WithUrl(new Uri(url)) - .WithTransport(transportType) .WithLoggerFactory(loggerFactory) + .WithUrl(url, transportType) .Build(); try { diff --git a/test/Microsoft.AspNetCore.SignalR.Tests/Microsoft.AspNetCore.SignalR.Tests.csproj b/test/Microsoft.AspNetCore.SignalR.Tests/Microsoft.AspNetCore.SignalR.Tests.csproj index 0a852ed063..b8d8ef3466 100644 --- a/test/Microsoft.AspNetCore.SignalR.Tests/Microsoft.AspNetCore.SignalR.Tests.csproj +++ b/test/Microsoft.AspNetCore.SignalR.Tests/Microsoft.AspNetCore.SignalR.Tests.csproj @@ -26,7 +26,6 @@ -