From c1a4b25a668b207689c4977ddb0b05b61849d81f Mon Sep 17 00:00:00 2001 From: Mikael Mengistu Date: Tue, 28 Feb 2017 10:48:45 -0800 Subject: [PATCH] Remove logger factory requirement from HubConnection (#238) --- .../HubConnection.cs | 19 ++++++++++--------- .../Connection.cs | 1 + .../{ => Internal}/NullLoggerFactory.cs | 4 ++-- .../LongPollingTransport.cs | 3 ++- .../WebSocketsTransport.cs | 4 ++-- .../HubConnectionTests.cs | 8 ++++---- 6 files changed, 21 insertions(+), 18 deletions(-) rename src/Microsoft.AspNetCore.Sockets.Client/{ => Internal}/NullLoggerFactory.cs (86%) diff --git a/src/Microsoft.AspNetCore.SignalR.Client/HubConnection.cs b/src/Microsoft.AspNetCore.SignalR.Client/HubConnection.cs index de3aaca1b9..4175867344 100644 --- a/src/Microsoft.AspNetCore.SignalR.Client/HubConnection.cs +++ b/src/Microsoft.AspNetCore.SignalR.Client/HubConnection.cs @@ -12,6 +12,7 @@ using System.Threading; using System.Threading.Tasks; using Microsoft.AspNetCore.Sockets; using Microsoft.AspNetCore.Sockets.Client; +using Microsoft.AspNetCore.Sockets.Client.Internal; using Microsoft.Extensions.Logging; namespace Microsoft.AspNetCore.SignalR.Client @@ -45,29 +46,29 @@ namespace Microsoft.AspNetCore.SignalR.Client remove { _connection.Closed -= value; } } + public HubConnection(Uri url, IInvocationAdapter adapter) + : this(new Connection(url), adapter, null) + { } + + public HubConnection(Uri url) + : this(new Connection(url), new JsonNetInvocationAdapter(), null) + { } + public HubConnection(Uri url, IInvocationAdapter adapter, ILoggerFactory loggerFactory) : this(new Connection(url, loggerFactory), adapter, loggerFactory) { } public HubConnection(IConnection connection, IInvocationAdapter adapter, ILoggerFactory loggerFactory) { - // TODO: loggerFactory shouldn't be required - if (loggerFactory == null) - { - throw new ArgumentNullException(nameof(loggerFactory)); - } - if (connection == null) { throw new ArgumentNullException(nameof(connection)); } _connection = connection; - _binder = new HubBinder(this); _adapter = adapter; - _logger = loggerFactory.CreateLogger(); - + _logger = (loggerFactory ?? NullLoggerFactory.Instance).CreateLogger(); _connection.Received += OnDataReceived; _connection.Closed += Shutdown; } diff --git a/src/Microsoft.AspNetCore.Sockets.Client/Connection.cs b/src/Microsoft.AspNetCore.Sockets.Client/Connection.cs index e03e6ca343..60400beed6 100644 --- a/src/Microsoft.AspNetCore.Sockets.Client/Connection.cs +++ b/src/Microsoft.AspNetCore.Sockets.Client/Connection.cs @@ -7,6 +7,7 @@ using System.Net.Http; using System.Threading; using System.Threading.Tasks; using System.Threading.Tasks.Channels; +using Microsoft.AspNetCore.Sockets.Client.Internal; using Microsoft.AspNetCore.Sockets.Internal; using Microsoft.Extensions.Logging; diff --git a/src/Microsoft.AspNetCore.Sockets.Client/NullLoggerFactory.cs b/src/Microsoft.AspNetCore.Sockets.Client/Internal/NullLoggerFactory.cs similarity index 86% rename from src/Microsoft.AspNetCore.Sockets.Client/NullLoggerFactory.cs rename to src/Microsoft.AspNetCore.Sockets.Client/Internal/NullLoggerFactory.cs index ddbb6d7686..261a2f5d7f 100644 --- a/src/Microsoft.AspNetCore.Sockets.Client/NullLoggerFactory.cs +++ b/src/Microsoft.AspNetCore.Sockets.Client/Internal/NullLoggerFactory.cs @@ -4,9 +4,9 @@ using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging.Abstractions; -namespace Microsoft.AspNetCore.Sockets.Client +namespace Microsoft.AspNetCore.Sockets.Client.Internal { - internal class NullLoggerFactory : ILoggerFactory + public class NullLoggerFactory : ILoggerFactory { public static readonly ILoggerFactory Instance = new NullLoggerFactory(); diff --git a/src/Microsoft.AspNetCore.Sockets.Client/LongPollingTransport.cs b/src/Microsoft.AspNetCore.Sockets.Client/LongPollingTransport.cs index c223dcbc5e..5957db308e 100644 --- a/src/Microsoft.AspNetCore.Sockets.Client/LongPollingTransport.cs +++ b/src/Microsoft.AspNetCore.Sockets.Client/LongPollingTransport.cs @@ -9,6 +9,7 @@ using System.Net.Http; using System.Net.Http.Headers; using System.Threading; using System.Threading.Tasks; +using Microsoft.AspNetCore.Sockets.Client.Internal; using Microsoft.Extensions.Internal; using Microsoft.Extensions.Logging; @@ -31,7 +32,7 @@ namespace Microsoft.AspNetCore.Sockets.Client public LongPollingTransport(HttpClient httpClient, ILoggerFactory loggerFactory) { _httpClient = httpClient; - _logger = loggerFactory.CreateLogger(); + _logger = (loggerFactory ?? NullLoggerFactory.Instance).CreateLogger(); } public Task StartAsync(Uri url, IChannelConnection application) diff --git a/src/Microsoft.AspNetCore.Sockets.Client/WebSocketsTransport.cs b/src/Microsoft.AspNetCore.Sockets.Client/WebSocketsTransport.cs index 2d490abca0..2641eb175e 100644 --- a/src/Microsoft.AspNetCore.Sockets.Client/WebSocketsTransport.cs +++ b/src/Microsoft.AspNetCore.Sockets.Client/WebSocketsTransport.cs @@ -2,13 +2,13 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; -using System.IO.Pipelines; +using System.Diagnostics; using System.Net.WebSockets; using System.Threading; using System.Threading.Tasks; using System.Collections.Generic; +using Microsoft.AspNetCore.Sockets.Client.Internal; using Microsoft.Extensions.Logging; -using System.Diagnostics; namespace Microsoft.AspNetCore.Sockets.Client { diff --git a/test/Microsoft.AspNetCore.SignalR.Client.FunctionalTests/HubConnectionTests.cs b/test/Microsoft.AspNetCore.SignalR.Client.FunctionalTests/HubConnectionTests.cs index cc55a559f9..3473eda2d5 100644 --- a/test/Microsoft.AspNetCore.SignalR.Client.FunctionalTests/HubConnectionTests.cs +++ b/test/Microsoft.AspNetCore.SignalR.Client.FunctionalTests/HubConnectionTests.cs @@ -51,7 +51,7 @@ namespace Microsoft.AspNetCore.SignalR.Client.FunctionalTests using (var httpClient = _testServer.CreateClient()) { var transport = new LongPollingTransport(httpClient, loggerFactory); - var connection = new HubConnection(new Uri("http://test/hubs"), new JsonNetInvocationAdapter(), loggerFactory); + var connection = new HubConnection(new Uri("http://test/hubs"), new JsonNetInvocationAdapter()); try { await connection.StartAsync(transport, httpClient); @@ -76,7 +76,7 @@ namespace Microsoft.AspNetCore.SignalR.Client.FunctionalTests using (var httpClient = _testServer.CreateClient()) { var transport = new LongPollingTransport(httpClient, loggerFactory); - var connection = new HubConnection(new Uri("http://test/hubs"), new JsonNetInvocationAdapter(), loggerFactory); + var connection = new HubConnection(new Uri("http://test/hubs"), new JsonNetInvocationAdapter()); try { await connection.StartAsync(transport, httpClient); @@ -126,7 +126,7 @@ namespace Microsoft.AspNetCore.SignalR.Client.FunctionalTests using (var httpClient = _testServer.CreateClient()) { var transport = new LongPollingTransport(httpClient, loggerFactory); - var connection = new HubConnection(new Uri("http://test/hubs"), new JsonNetInvocationAdapter(), loggerFactory); + var connection = new HubConnection(new Uri("http://test/hubs"), new JsonNetInvocationAdapter()); try { await connection.StartAsync(transport, httpClient); @@ -156,7 +156,7 @@ namespace Microsoft.AspNetCore.SignalR.Client.FunctionalTests using (var httpClient = _testServer.CreateClient()) { var transport = new LongPollingTransport(httpClient, loggerFactory); - var connection = new HubConnection(new Uri("http://test/hubs"), new JsonNetInvocationAdapter(), loggerFactory); + var connection = new HubConnection(new Uri("http://test/hubs"), new JsonNetInvocationAdapter()); try { await connection.StartAsync(transport, httpClient);