Adding default ctors for HubProtocols

This commit is contained in:
Pawel Kadluczka 2017-09-20 16:24:21 -07:00
parent ba25dee141
commit 126901a08f
12 changed files with 43 additions and 30 deletions

View File

@ -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)]

View File

@ -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)

View File

@ -28,6 +28,15 @@ namespace Microsoft.AspNetCore.SignalR.Internal.Protocol
// ONLY to be used for application payloads (args, return values, etc.)
private JsonSerializer _payloadSerializer;
/// <summary>
/// Creates an instance of the <see cref="JsonHubProtocol"/> using the default <see cref="JsonSerializer"/>
/// to serialize application payloads (arguments, results, etc.). The serialization of the outer protocol can
/// NOT be changed using this serializer.
/// </summary>
public JsonHubProtocol()
: this(new JsonSerializer())
{ }
/// <summary>
/// Creates an instance of the <see cref="JsonHubProtocol"/> using the specified <see cref="JsonSerializer"/>
/// to serialize application payloads (arguments, results, etc.). The serialization of the outer protocol can

View File

@ -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;

View File

@ -245,8 +245,8 @@ namespace Microsoft.AspNetCore.SignalR.Client.FunctionalTests
public static IEnumerable<IHubProtocol> HubProtocols =>
new IHubProtocol[]
{
new JsonHubProtocol(new JsonSerializer()),
new MessagePackHubProtocol(MessagePackHubProtocol.CreateDefaultSerializationContext()),
new JsonHubProtocol(),
new MessagePackHubProtocol(),
};
public static IEnumerable<TransportType> TransportTypes()

View File

@ -97,7 +97,7 @@ namespace Microsoft.AspNetCore.SignalR.Client.Tests
private async Task InvokeOn(Action<HubConnection, TaskCompletionSource<object[]>> 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<object[]>();
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<Exception>();
hubConnection.Closed += e =>
{

View File

@ -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<object[]>();
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<int>();
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());

View File

@ -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<int>("testMethod");

View File

@ -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<FormatException>(() => 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<FormatException>(() => protocol.TryParseMessages(Encoding.UTF8.GetBytes(input), binder, out var messages));
Assert.Equal(expectedMessage, ex.Message);
}

View File

@ -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<object[]> TestMessages => new[]
{

View File

@ -41,8 +41,8 @@ namespace Microsoft.AspNetCore.SignalR.Common.Protocol.Tests
public static IEnumerable<object[]> HubProtocols =>
new[]
{
new object[] { new JsonHubProtocol(new JsonSerializer()) },
new object[] { new MessagePackHubProtocol(MessagePackHubProtocol.CreateDefaultSerializationContext()) },
new object[] { new JsonHubProtocol() },
new object[] { new MessagePackHubProtocol() },
};
}
}

View File

@ -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<bool>();
protocol = protocol ?? new JsonHubProtocol(new JsonSerializer());
protocol = protocol ?? new JsonHubProtocol();
_protocolReaderWriter = new HubProtocolReaderWriter(protocol, new PassThroughEncoder());
_cts = new CancellationTokenSource();