From 126901a08f3ab47a107c2e02068ae5a3466aa56b Mon Sep 17 00:00:00 2001 From: Pawel Kadluczka Date: Wed, 20 Sep 2017 16:24:21 -0700 Subject: [PATCH] Adding default ctors for HubProtocols --- .../HubConnectionBuilder.cs | 2 +- .../HubConnectionBuilderExtensions.cs | 4 +-- .../Internal/Protocol/JsonHubProtocol.cs | 9 ++++++ .../Protocol/MessagePackHubProtocol.cs | 4 +++ .../HubConnectionTests.cs | 4 +-- .../HubConnectionExtensionsTests.cs | 4 +-- .../HubConnectionProtocolTests.cs | 30 +++++++++---------- .../HubConnectionTests.cs | 4 +-- .../Internal/Protocol/JsonHubProtocolTests.cs | 4 +-- .../Protocol/MessagePackHubProtocolTests.cs | 2 +- .../DefaultHubProtocolResolverTests.cs | 4 +-- .../TestClient.cs | 2 +- 12 files changed, 43 insertions(+), 30 deletions(-) diff --git a/src/Microsoft.AspNetCore.SignalR.Client.Core/HubConnectionBuilder.cs b/src/Microsoft.AspNetCore.SignalR.Client.Core/HubConnectionBuilder.cs index 2dca887414..81fb12c56b 100644 --- a/src/Microsoft.AspNetCore.SignalR.Client.Core/HubConnectionBuilder.cs +++ b/src/Microsoft.AspNetCore.SignalR.Client.Core/HubConnectionBuilder.cs @@ -46,7 +46,7 @@ namespace Microsoft.AspNetCore.SignalR.Client var loggerFactory = ((IHubConnectionBuilder)this).GetLoggerFactory(); var hubProtocol = ((IHubConnectionBuilder)this).GetHubProtocol(); - return new HubConnection(connection, hubProtocol ?? new JsonHubProtocol(new JsonSerializer()), loggerFactory); + return new HubConnection(connection, hubProtocol ?? new JsonHubProtocol(), loggerFactory); } [EditorBrowsable(EditorBrowsableState.Never)] diff --git a/src/Microsoft.AspNetCore.SignalR.Client.Core/HubConnectionBuilderExtensions.cs b/src/Microsoft.AspNetCore.SignalR.Client.Core/HubConnectionBuilderExtensions.cs index fdfe9aef73..d060f8dda6 100644 --- a/src/Microsoft.AspNetCore.SignalR.Client.Core/HubConnectionBuilderExtensions.cs +++ b/src/Microsoft.AspNetCore.SignalR.Client.Core/HubConnectionBuilderExtensions.cs @@ -18,13 +18,13 @@ namespace Microsoft.AspNetCore.SignalR.Client public static IHubConnectionBuilder WithJsonProtocol(this IHubConnectionBuilder hubConnectionBuilder) { - return hubConnectionBuilder.WithHubProtocol(new JsonHubProtocol(new JsonSerializer())); + return hubConnectionBuilder.WithHubProtocol(new JsonHubProtocol()); } public static IHubConnectionBuilder WithMessagePackProtocol(this IHubConnectionBuilder hubConnectionBuilder) { return hubConnectionBuilder.WithHubProtocol( - new MessagePackHubProtocol(MessagePackHubProtocol.CreateDefaultSerializationContext())); + new MessagePackHubProtocol()); } public static IHubConnectionBuilder WithLoggerFactory(this IHubConnectionBuilder hubConnectionBuilder, ILoggerFactory loggerFactory) diff --git a/src/Microsoft.AspNetCore.SignalR.Common/Internal/Protocol/JsonHubProtocol.cs b/src/Microsoft.AspNetCore.SignalR.Common/Internal/Protocol/JsonHubProtocol.cs index 4bf45cd274..a4fc7cbe88 100644 --- a/src/Microsoft.AspNetCore.SignalR.Common/Internal/Protocol/JsonHubProtocol.cs +++ b/src/Microsoft.AspNetCore.SignalR.Common/Internal/Protocol/JsonHubProtocol.cs @@ -28,6 +28,15 @@ namespace Microsoft.AspNetCore.SignalR.Internal.Protocol // ONLY to be used for application payloads (args, return values, etc.) private JsonSerializer _payloadSerializer; + /// + /// Creates an instance of the using the default + /// to serialize application payloads (arguments, results, etc.). The serialization of the outer protocol can + /// NOT be changed using this serializer. + /// + public JsonHubProtocol() + : this(new JsonSerializer()) + { } + /// /// Creates an instance of the using the specified /// to serialize application payloads (arguments, results, etc.). The serialization of the outer protocol can diff --git a/src/Microsoft.AspNetCore.SignalR.Common/Internal/Protocol/MessagePackHubProtocol.cs b/src/Microsoft.AspNetCore.SignalR.Common/Internal/Protocol/MessagePackHubProtocol.cs index 77ad478fc1..c75c681222 100644 --- a/src/Microsoft.AspNetCore.SignalR.Common/Internal/Protocol/MessagePackHubProtocol.cs +++ b/src/Microsoft.AspNetCore.SignalR.Common/Internal/Protocol/MessagePackHubProtocol.cs @@ -26,6 +26,10 @@ namespace Microsoft.AspNetCore.SignalR.Internal.Protocol public ProtocolType Type => ProtocolType.Binary; + public MessagePackHubProtocol() + : this(CreateDefaultSerializationContext()) + { } + public MessagePackHubProtocol(SerializationContext serializationContext) { _serializationContext = serializationContext; diff --git a/test/Microsoft.AspNetCore.SignalR.Client.FunctionalTests/HubConnectionTests.cs b/test/Microsoft.AspNetCore.SignalR.Client.FunctionalTests/HubConnectionTests.cs index 39f7ab26ac..b56fecd10e 100644 --- a/test/Microsoft.AspNetCore.SignalR.Client.FunctionalTests/HubConnectionTests.cs +++ b/test/Microsoft.AspNetCore.SignalR.Client.FunctionalTests/HubConnectionTests.cs @@ -245,8 +245,8 @@ namespace Microsoft.AspNetCore.SignalR.Client.FunctionalTests public static IEnumerable HubProtocols => new IHubProtocol[] { - new JsonHubProtocol(new JsonSerializer()), - new MessagePackHubProtocol(MessagePackHubProtocol.CreateDefaultSerializationContext()), + new JsonHubProtocol(), + new MessagePackHubProtocol(), }; public static IEnumerable TransportTypes() diff --git a/test/Microsoft.AspNetCore.SignalR.Client.Tests/HubConnectionExtensionsTests.cs b/test/Microsoft.AspNetCore.SignalR.Client.Tests/HubConnectionExtensionsTests.cs index 4c2f15fc2d..34e0a4b9ae 100644 --- a/test/Microsoft.AspNetCore.SignalR.Client.Tests/HubConnectionExtensionsTests.cs +++ b/test/Microsoft.AspNetCore.SignalR.Client.Tests/HubConnectionExtensionsTests.cs @@ -97,7 +97,7 @@ namespace Microsoft.AspNetCore.SignalR.Client.Tests private async Task InvokeOn(Action> onAction, object[] args) { var connection = new TestConnection(); - var hubConnection = new HubConnection(connection, new JsonHubProtocol(new JsonSerializer()), new LoggerFactory()); + var hubConnection = new HubConnection(connection, new JsonHubProtocol(), new LoggerFactory()); var handlerTcs = new TaskCompletionSource(); try { @@ -169,7 +169,7 @@ namespace Microsoft.AspNetCore.SignalR.Client.Tests public async Task ConnectionClosedOnCallbackArgumentTypeMismatch() { var connection = new TestConnection(); - var hubConnection = new HubConnection(connection, new JsonHubProtocol(new JsonSerializer()), new LoggerFactory()); + var hubConnection = new HubConnection(connection, new JsonHubProtocol(), new LoggerFactory()); var closeTcs = new TaskCompletionSource(); hubConnection.Closed += e => { diff --git a/test/Microsoft.AspNetCore.SignalR.Client.Tests/HubConnectionProtocolTests.cs b/test/Microsoft.AspNetCore.SignalR.Client.Tests/HubConnectionProtocolTests.cs index d88054e2b9..d51e309668 100644 --- a/test/Microsoft.AspNetCore.SignalR.Client.Tests/HubConnectionProtocolTests.cs +++ b/test/Microsoft.AspNetCore.SignalR.Client.Tests/HubConnectionProtocolTests.cs @@ -26,7 +26,7 @@ namespace Microsoft.AspNetCore.SignalR.Client.Tests public async Task SendAsyncSendsANonBlockingInvocationMessage() { var connection = new TestConnection(); - var hubConnection = new HubConnection(connection, new JsonHubProtocol(new JsonSerializer()), new LoggerFactory()); + var hubConnection = new HubConnection(connection, new JsonHubProtocol(), new LoggerFactory()); try { await hubConnection.StartAsync(); @@ -50,7 +50,7 @@ namespace Microsoft.AspNetCore.SignalR.Client.Tests public async Task ClientSendsNegotationMessageWhenStartingConnection() { var connection = new TestConnection(); - var hubConnection = new HubConnection(connection, new JsonHubProtocol(new JsonSerializer()), new LoggerFactory()); + var hubConnection = new HubConnection(connection, new JsonHubProtocol(), new LoggerFactory()); try { await hubConnection.StartAsync(); @@ -69,7 +69,7 @@ namespace Microsoft.AspNetCore.SignalR.Client.Tests public async Task InvokeSendsAnInvocationMessage() { var connection = new TestConnection(); - var hubConnection = new HubConnection(connection, new JsonHubProtocol(new JsonSerializer()), new LoggerFactory()); + var hubConnection = new HubConnection(connection, new JsonHubProtocol(), new LoggerFactory()); try { await hubConnection.StartAsync(); @@ -93,7 +93,7 @@ namespace Microsoft.AspNetCore.SignalR.Client.Tests public async Task StreamSendsAnInvocationMessage() { var connection = new TestConnection(); - var hubConnection = new HubConnection(connection, new JsonHubProtocol(new JsonSerializer()), new LoggerFactory()); + var hubConnection = new HubConnection(connection, new JsonHubProtocol(), new LoggerFactory()); try { await hubConnection.StartAsync(); @@ -121,7 +121,7 @@ namespace Microsoft.AspNetCore.SignalR.Client.Tests public async Task InvokeCompletedWhenCompletionMessageReceived() { var connection = new TestConnection(); - var hubConnection = new HubConnection(connection, new JsonHubProtocol(new JsonSerializer()), new LoggerFactory()); + var hubConnection = new HubConnection(connection, new JsonHubProtocol(), new LoggerFactory()); try { await hubConnection.StartAsync(); @@ -165,7 +165,7 @@ namespace Microsoft.AspNetCore.SignalR.Client.Tests public async Task InvokeYieldsResultWhenCompletionMessageReceived() { var connection = new TestConnection(); - var hubConnection = new HubConnection(connection, new JsonHubProtocol(new JsonSerializer()), new LoggerFactory()); + var hubConnection = new HubConnection(connection, new JsonHubProtocol(), new LoggerFactory()); try { await hubConnection.StartAsync(); @@ -187,7 +187,7 @@ namespace Microsoft.AspNetCore.SignalR.Client.Tests public async Task StreamFailsIfCompletionMessageHasPayload() { var connection = new TestConnection(); - var hubConnection = new HubConnection(connection, new JsonHubProtocol(new JsonSerializer()), new LoggerFactory()); + var hubConnection = new HubConnection(connection, new JsonHubProtocol(), new LoggerFactory()); try { await hubConnection.StartAsync(); @@ -210,7 +210,7 @@ namespace Microsoft.AspNetCore.SignalR.Client.Tests public async Task InvokeFailsWithExceptionWhenCompletionWithErrorReceived() { var connection = new TestConnection(); - var hubConnection = new HubConnection(connection, new JsonHubProtocol(new JsonSerializer()), new LoggerFactory()); + var hubConnection = new HubConnection(connection, new JsonHubProtocol(), new LoggerFactory()); try { await hubConnection.StartAsync(); @@ -233,7 +233,7 @@ namespace Microsoft.AspNetCore.SignalR.Client.Tests public async Task StreamFailsWithExceptionWhenCompletionWithErrorReceived() { var connection = new TestConnection(); - var hubConnection = new HubConnection(connection, new JsonHubProtocol(new JsonSerializer()), new LoggerFactory()); + var hubConnection = new HubConnection(connection, new JsonHubProtocol(), new LoggerFactory()); try { await hubConnection.StartAsync(); @@ -256,7 +256,7 @@ namespace Microsoft.AspNetCore.SignalR.Client.Tests public async Task InvokeFailsWithErrorWhenStreamingItemReceived() { var connection = new TestConnection(); - var hubConnection = new HubConnection(connection, new JsonHubProtocol(new JsonSerializer()), new LoggerFactory()); + var hubConnection = new HubConnection(connection, new JsonHubProtocol(), new LoggerFactory()); try { await hubConnection.StartAsync(); @@ -279,7 +279,7 @@ namespace Microsoft.AspNetCore.SignalR.Client.Tests public async Task StreamYieldsItemsAsTheyArrive() { var connection = new TestConnection(); - var hubConnection = new HubConnection(connection, new JsonHubProtocol(new JsonSerializer()), new LoggerFactory()); + var hubConnection = new HubConnection(connection, new JsonHubProtocol(), new LoggerFactory()); try { await hubConnection.StartAsync(); @@ -306,7 +306,7 @@ namespace Microsoft.AspNetCore.SignalR.Client.Tests public async Task HandlerRegisteredWithOnIsFiredWhenInvocationReceived() { var connection = new TestConnection(); - var hubConnection = new HubConnection(connection, new JsonHubProtocol(new JsonSerializer()), new LoggerFactory()); + var hubConnection = new HubConnection(connection, new JsonHubProtocol(), new LoggerFactory()); var handlerCalled = new TaskCompletionSource(); try { @@ -332,7 +332,7 @@ namespace Microsoft.AspNetCore.SignalR.Client.Tests var connection = new TestConnection(TransferMode.Text); var hubConnection = new HubConnection(connection, - new MessagePackHubProtocol(MessagePackHubProtocol.CreateDefaultSerializationContext()), new LoggerFactory()); + new MessagePackHubProtocol(), new LoggerFactory()); try { await hubConnection.StartAsync().OrTimeout(); @@ -363,7 +363,7 @@ namespace Microsoft.AspNetCore.SignalR.Client.Tests { var connection = new TestConnection(TransferMode.Text); var hubConnection = new HubConnection(connection, - new MessagePackHubProtocol(MessagePackHubProtocol.CreateDefaultSerializationContext()), new LoggerFactory()); + new MessagePackHubProtocol(), new LoggerFactory()); var invocationTcs = new TaskCompletionSource(); try @@ -373,7 +373,7 @@ namespace Microsoft.AspNetCore.SignalR.Client.Tests using (var ms = new MemoryStream()) { - new MessagePackHubProtocol(MessagePackHubProtocol.CreateDefaultSerializationContext()) + new MessagePackHubProtocol() .WriteMessage(new InvocationMessage("1", true, "MyMethod", 42), ms); var invokeMessage = Convert.ToBase64String(ms.ToArray()); diff --git a/test/Microsoft.AspNetCore.SignalR.Client.Tests/HubConnectionTests.cs b/test/Microsoft.AspNetCore.SignalR.Client.Tests/HubConnectionTests.cs index f21ffe604d..3a25409ee3 100644 --- a/test/Microsoft.AspNetCore.SignalR.Client.Tests/HubConnectionTests.cs +++ b/test/Microsoft.AspNetCore.SignalR.Client.Tests/HubConnectionTests.cs @@ -100,7 +100,7 @@ namespace Microsoft.AspNetCore.SignalR.Client.Tests public async Task CannotCallInvokeOnClosedHubConnection() { var connection = new TestConnection(); - var hubConnection = new HubConnection(connection, new JsonHubProtocol(new JsonSerializer()), new LoggerFactory()); + var hubConnection = new HubConnection(connection, new JsonHubProtocol(), new LoggerFactory()); await hubConnection.StartAsync(); await hubConnection.DisposeAsync(); @@ -114,7 +114,7 @@ namespace Microsoft.AspNetCore.SignalR.Client.Tests public async Task PendingInvocationsAreCancelledWhenConnectionClosesCleanly() { var connection = new TestConnection(); - var hubConnection = new HubConnection(connection, new JsonHubProtocol(new JsonSerializer()), new LoggerFactory()); + var hubConnection = new HubConnection(connection, new JsonHubProtocol(), new LoggerFactory()); await hubConnection.StartAsync(); var invokeTask = hubConnection.InvokeAsync("testMethod"); diff --git a/test/Microsoft.AspNetCore.SignalR.Common.Tests/Internal/Protocol/JsonHubProtocolTests.cs b/test/Microsoft.AspNetCore.SignalR.Common.Tests/Internal/Protocol/JsonHubProtocolTests.cs index cc652e1d42..c7d3e3cd20 100644 --- a/test/Microsoft.AspNetCore.SignalR.Common.Tests/Internal/Protocol/JsonHubProtocolTests.cs +++ b/test/Microsoft.AspNetCore.SignalR.Common.Tests/Internal/Protocol/JsonHubProtocolTests.cs @@ -119,7 +119,7 @@ namespace Microsoft.AspNetCore.SignalR.Common.Tests.Internal.Protocol input = Frame(input); var binder = new TestBinder(); - var protocol = new JsonHubProtocol(new JsonSerializer()); + var protocol = new JsonHubProtocol(); var ex = Assert.Throws(() => protocol.TryParseMessages(Encoding.UTF8.GetBytes(input), binder, out var messages)); Assert.Equal(expectedMessage, ex.Message); } @@ -133,7 +133,7 @@ namespace Microsoft.AspNetCore.SignalR.Common.Tests.Internal.Protocol input = Frame(input); var binder = new TestBinder(paramTypes: new[] { typeof(int), typeof(string) }, returnType: typeof(bool)); - var protocol = new JsonHubProtocol(new JsonSerializer()); + var protocol = new JsonHubProtocol(); var ex = Assert.Throws(() => protocol.TryParseMessages(Encoding.UTF8.GetBytes(input), binder, out var messages)); Assert.Equal(expectedMessage, ex.Message); } diff --git a/test/Microsoft.AspNetCore.SignalR.Common.Tests/Internal/Protocol/MessagePackHubProtocolTests.cs b/test/Microsoft.AspNetCore.SignalR.Common.Tests/Internal/Protocol/MessagePackHubProtocolTests.cs index 32c45ae11d..18c2464bd3 100644 --- a/test/Microsoft.AspNetCore.SignalR.Common.Tests/Internal/Protocol/MessagePackHubProtocolTests.cs +++ b/test/Microsoft.AspNetCore.SignalR.Common.Tests/Internal/Protocol/MessagePackHubProtocolTests.cs @@ -13,7 +13,7 @@ namespace Microsoft.AspNetCore.SignalR.Common.Tests.Internal.Protocol public class MessagePackHubProtocolTests { private static readonly MessagePackHubProtocol _hubProtocol - = new MessagePackHubProtocol(MessagePackHubProtocol.CreateDefaultSerializationContext()); + = new MessagePackHubProtocol(); public static IEnumerable TestMessages => new[] { diff --git a/test/Microsoft.AspNetCore.SignalR.Tests/Internal/DefaultHubProtocolResolverTests.cs b/test/Microsoft.AspNetCore.SignalR.Tests/Internal/DefaultHubProtocolResolverTests.cs index 0b3b604182..b0596994cd 100644 --- a/test/Microsoft.AspNetCore.SignalR.Tests/Internal/DefaultHubProtocolResolverTests.cs +++ b/test/Microsoft.AspNetCore.SignalR.Tests/Internal/DefaultHubProtocolResolverTests.cs @@ -41,8 +41,8 @@ namespace Microsoft.AspNetCore.SignalR.Common.Protocol.Tests public static IEnumerable HubProtocols => new[] { - new object[] { new JsonHubProtocol(new JsonSerializer()) }, - new object[] { new MessagePackHubProtocol(MessagePackHubProtocol.CreateDefaultSerializationContext()) }, + new object[] { new JsonHubProtocol() }, + new object[] { new MessagePackHubProtocol() }, }; } } diff --git a/test/Microsoft.AspNetCore.SignalR.Tests/TestClient.cs b/test/Microsoft.AspNetCore.SignalR.Tests/TestClient.cs index 94725fdebe..516a38cab4 100644 --- a/test/Microsoft.AspNetCore.SignalR.Tests/TestClient.cs +++ b/test/Microsoft.AspNetCore.SignalR.Tests/TestClient.cs @@ -41,7 +41,7 @@ namespace Microsoft.AspNetCore.SignalR.Tests Connection.User = new ClaimsPrincipal(new ClaimsIdentity(new[] { new Claim(ClaimTypes.Name, Interlocked.Increment(ref _id).ToString()) })); Connection.Metadata["ConnectedTask"] = new TaskCompletionSource(); - protocol = protocol ?? new JsonHubProtocol(new JsonSerializer()); + protocol = protocol ?? new JsonHubProtocol(); _protocolReaderWriter = new HubProtocolReaderWriter(protocol, new PassThroughEncoder()); _cts = new CancellationTokenSource();