Merge branch 'rel/1.0.0-alpha1' into dev

# Conflicts:
#	src/Microsoft.AspNetCore.SignalR.Client.Core/Microsoft.AspNetCore.SignalR.Client.Core.csproj
This commit is contained in:
David Fowler 2017-09-13 09:20:53 -07:00
commit 11cc57ee0e
12 changed files with 57 additions and 36 deletions

View File

@ -31,16 +31,11 @@ namespace ClientSample
baseUrl = string.IsNullOrEmpty(baseUrl) ? "http://localhost:5000/default" : baseUrl; baseUrl = string.IsNullOrEmpty(baseUrl) ? "http://localhost:5000/default" : baseUrl;
Console.WriteLine("Connecting to {0}", baseUrl); Console.WriteLine("Connecting to {0}", baseUrl);
var httpConnection = new HttpConnection(new Uri(baseUrl)); HubConnection connection = await ConnectAsync(baseUrl);
var connection = new HubConnectionBuilder() Console.WriteLine("Connected to {0}", baseUrl);
.WithUrl(baseUrl)
.WithConsoleLogger()
.Build();
try try
{ {
await connection.StartAsync();
Console.WriteLine("Connected to {0}", baseUrl);
var cts = new CancellationTokenSource(); var cts = new CancellationTokenSource();
Console.CancelKeyPress += (sender, a) => Console.CancelKeyPress += (sender, a) =>
@ -92,5 +87,26 @@ namespace ClientSample
} }
return 0; return 0;
} }
private static async Task<HubConnection> ConnectAsync(string baseUrl)
{
// Keep trying to until we can start
while (true)
{
var connection = new HubConnectionBuilder()
.WithUrl(baseUrl)
.WithConsoleLogger(LogLevel.Trace)
.Build();
try
{
await connection.StartAsync();
return connection;
}
catch (Exception)
{
await Task.Delay(1000);
}
}
}
} }
} }

View File

@ -1,9 +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.Sockets.Client;
namespace Microsoft.AspNetCore.SignalR.Client
{
public delegate IConnection ConnectionFactoryDelegate();
}

View File

@ -5,6 +5,7 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel; using System.ComponentModel;
using Microsoft.AspNetCore.SignalR.Internal.Protocol; using Microsoft.AspNetCore.SignalR.Internal.Protocol;
using Microsoft.AspNetCore.Sockets.Client;
using Newtonsoft.Json; using Newtonsoft.Json;
namespace Microsoft.AspNetCore.SignalR.Client namespace Microsoft.AspNetCore.SignalR.Client
@ -12,9 +13,9 @@ namespace Microsoft.AspNetCore.SignalR.Client
public class HubConnectionBuilder : IHubConnectionBuilder public class HubConnectionBuilder : IHubConnectionBuilder
{ {
private readonly Dictionary<KeyValuePair<string, Type>, object> _settings = new Dictionary<KeyValuePair<string, Type>, object>(); private readonly Dictionary<KeyValuePair<string, Type>, object> _settings = new Dictionary<KeyValuePair<string, Type>, object>();
private ConnectionFactoryDelegate _connectionFactoryDelegate; private Func<IConnection> _connectionFactoryDelegate;
public void ConfigureConnectionFactory(ConnectionFactoryDelegate connectionFactoryDelegate) => public void ConfigureConnectionFactory(Func<IConnection> connectionFactoryDelegate) =>
_connectionFactoryDelegate = connectionFactoryDelegate; _connectionFactoryDelegate = connectionFactoryDelegate;
public void AddSetting<T>(string name, T value) public void AddSetting<T>(string name, T value)

View File

@ -1,6 +1,7 @@
// Copyright (c) .NET Foundation. All rights reserved. // 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. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using Microsoft.AspNetCore.SignalR.Internal.Protocol; using Microsoft.AspNetCore.SignalR.Internal.Protocol;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Newtonsoft.Json; using Newtonsoft.Json;
@ -25,12 +26,20 @@ namespace Microsoft.AspNetCore.SignalR.Client
return hubConnectionBuilder.WithHubProtocol(new MessagePackHubProtocol()); return hubConnectionBuilder.WithHubProtocol(new MessagePackHubProtocol());
} }
public static IHubConnectionBuilder WithLogger(this IHubConnectionBuilder hubConnectionBuilder, ILoggerFactory loggerFactory) public static IHubConnectionBuilder WithLoggerFactory(this IHubConnectionBuilder hubConnectionBuilder, ILoggerFactory loggerFactory)
{ {
hubConnectionBuilder.AddSetting(HubConnectionBuilderDefaults.LoggerFactoryKey, loggerFactory); hubConnectionBuilder.AddSetting(HubConnectionBuilderDefaults.LoggerFactoryKey,
loggerFactory ?? throw new ArgumentNullException(nameof(loggerFactory)));
return hubConnectionBuilder; return hubConnectionBuilder;
} }
public static IHubConnectionBuilder WithLogger(this IHubConnectionBuilder hubConnectionBuilder, Action<ILoggerFactory> configureLogging)
{
var loggerFactory = hubConnectionBuilder.GetLoggerFactory() ?? new LoggerFactory();
configureLogging(loggerFactory);
return hubConnectionBuilder.WithLoggerFactory(loggerFactory);
}
public static ILoggerFactory GetLoggerFactory(this IHubConnectionBuilder hubConnectionBuilder) public static ILoggerFactory GetLoggerFactory(this IHubConnectionBuilder hubConnectionBuilder)
{ {
hubConnectionBuilder.TryGetSetting<ILoggerFactory>(HubConnectionBuilderDefaults.LoggerFactoryKey, out var loggerFactory); hubConnectionBuilder.TryGetSetting<ILoggerFactory>(HubConnectionBuilderDefaults.LoggerFactoryKey, out var loggerFactory);

View File

@ -1,13 +1,16 @@
// Copyright (c) .NET Foundation. All rights reserved. // 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. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using Microsoft.AspNetCore.Sockets.Client;
namespace Microsoft.AspNetCore.SignalR.Client namespace Microsoft.AspNetCore.SignalR.Client
{ {
public interface IHubConnectionBuilder public interface IHubConnectionBuilder
{ {
void AddSetting<T>(string name, T value); void AddSetting<T>(string name, T value);
bool TryGetSetting<T>(string name, out T value); bool TryGetSetting<T>(string name, out T value);
void ConfigureConnectionFactory(ConnectionFactoryDelegate connectionFactory); void ConfigureConnectionFactory(Func<IConnection> connectionFactoryDelegate);
HubConnection Build(); HubConnection Build();
} }
} }

View File

@ -15,7 +15,7 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" /> <PackageReference Include="Microsoft.Extensions.Logging" />
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@ -10,17 +10,18 @@ namespace Microsoft.AspNetCore.SignalR.Client
{ {
public static IHubConnectionBuilder WithConsoleLogger(this IHubConnectionBuilder hubConnectionBuilder) public static IHubConnectionBuilder WithConsoleLogger(this IHubConnectionBuilder hubConnectionBuilder)
{ {
return hubConnectionBuilder.WithConsoleLogger(new LoggerFactory()); return hubConnectionBuilder.WithLogger(loggerFactory =>
{
loggerFactory.AddConsole();
});
} }
public static IHubConnectionBuilder WithConsoleLogger(this IHubConnectionBuilder hubConnectionBuilder, ILoggerFactory loggerFactory) public static IHubConnectionBuilder WithConsoleLogger(this IHubConnectionBuilder hubConnectionBuilder, LogLevel logLevel)
{ {
if (loggerFactory == null) return hubConnectionBuilder.WithLogger(loggerFactory =>
{ {
throw new ArgumentNullException(nameof(loggerFactory)); loggerFactory.AddConsole(logLevel);
} });
return hubConnectionBuilder.WithLogger(loggerFactory.AddConsole());
} }
} }
} }

View File

@ -11,7 +11,6 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging" Version="$(AspNetCoreVersion)" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="$(AspNetCoreVersion)" /> <PackageReference Include="Microsoft.Extensions.Logging.Console" Version="$(AspNetCoreVersion)" />
</ItemGroup> </ItemGroup>

View File

@ -47,7 +47,7 @@ namespace Microsoft.AspNetCore.SignalR.Client.FunctionalTests
var connection = new HubConnectionBuilder() var connection = new HubConnectionBuilder()
.WithUrl(_serverFixture.BaseUrl + path) .WithUrl(_serverFixture.BaseUrl + path)
.WithTransportType(transportType) .WithTransportType(transportType)
.WithLogger(loggerFactory) .WithLoggerFactory(loggerFactory)
.WithHubProtocol(protocol) .WithHubProtocol(protocol)
.Build(); .Build();

View File

@ -43,7 +43,7 @@ namespace Microsoft.AspNetCore.SignalR.Client.Tests
{ {
var connectionBuilder = new HubConnectionBuilder(); var connectionBuilder = new HubConnectionBuilder();
var loggerFactory = Mock.Of<ILoggerFactory>(); var loggerFactory = Mock.Of<ILoggerFactory>();
connectionBuilder.WithLogger(loggerFactory); connectionBuilder.WithLoggerFactory(loggerFactory);
Assert.Same(loggerFactory, connectionBuilder.GetLoggerFactory()); Assert.Same(loggerFactory, connectionBuilder.GetLoggerFactory());
} }
@ -52,7 +52,8 @@ namespace Microsoft.AspNetCore.SignalR.Client.Tests
{ {
var connectionBuilder = new HubConnectionBuilder(); var connectionBuilder = new HubConnectionBuilder();
var mockLoggerFactory = new Mock<ILoggerFactory>(); var mockLoggerFactory = new Mock<ILoggerFactory>();
connectionBuilder.WithConsoleLogger(mockLoggerFactory.Object); connectionBuilder.WithLoggerFactory(mockLoggerFactory.Object);
connectionBuilder.WithConsoleLogger();
mockLoggerFactory.Verify(f => f.AddProvider(It.IsAny<ConsoleLoggerProvider>()), Times.Once); mockLoggerFactory.Verify(f => f.AddProvider(It.IsAny<ConsoleLoggerProvider>()), Times.Once);
} }

View File

@ -25,10 +25,10 @@ namespace Microsoft.AspNetCore.SignalR.Client.Tests
} }
[Fact] [Fact]
public void WithConsoleLoggerThrowsForNullLoggerFactory() public void WithLoggerFactoryThrowsForNullLoggerFactory()
{ {
Assert.Equal("loggerFactory", Assert.Equal("loggerFactory",
Assert.Throws<ArgumentNullException>(() => new HubConnectionBuilder().WithConsoleLogger(null)).ParamName); Assert.Throws<ArgumentNullException>(() => new HubConnectionBuilder().WithLoggerFactory(null)).ParamName);
} }
} }
} }

View File

@ -287,7 +287,7 @@ namespace Microsoft.AspNetCore.SignalR.Tests
var connection = new HubConnectionBuilder() var connection = new HubConnectionBuilder()
.WithUrl(new Uri(url)) .WithUrl(new Uri(url))
.WithTransportType(transportType) .WithTransportType(transportType)
.WithLogger(loggerFactory) .WithLoggerFactory(loggerFactory)
.Build(); .Build();
try try
{ {