Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

separate ASP.NET and the core one productInfo #499

Merged
merged 3 commits into from
Apr 26, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/Microsoft.Azure.SignalR.AspNet/ProductInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.Reflection;

namespace Microsoft.Azure.SignalR.AspNet
{
internal static class ProductInfo
{
/// <summary>
/// For .NET framework below netframework462, there are assembly binding issues when referencing netstandard assemblies, https://github.com/Azure/azure-signalr/issues/452
/// For now, disable usage of System.Runtime.InteropServices.RuntimeInformation
/// </summary>
/// <returns></returns>
public static string GetProductInfo()
{
var assembly = Assembly.GetCallingAssembly();
var packageId = assembly.GetName().Name;
var version = assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>().InformationalVersion;

return $"{packageId}/{version}(Asp.Net SignalR)";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.Buffers;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
Expand All @@ -18,6 +19,7 @@ namespace Microsoft.Azure.SignalR.AspNet
{
internal partial class ServiceConnection : ServiceConnectionBase
{
private static readonly Dictionary<string, string> CustomHeader = new Dictionary<string, string> { { "Asrs-User-Agent", ProductInfo.GetProductInfo() } };
private const string ReconnectMessage = "asrs:reconnect";
private readonly ConcurrentDictionary<string, ClientContext> _clientConnections = new ConcurrentDictionary<string, ClientContext>(StringComparer.Ordinal);

Expand All @@ -42,7 +44,7 @@ public ServiceConnection(

protected override Task<ConnectionContext> CreateConnection(string target = null)
{
return _connectionFactory.ConnectAsync(TransferFormat.Binary, ConnectionId, target);
return _connectionFactory.ConnectAsync(TransferFormat.Binary, ConnectionId, target, headers: CustomHeader);
}

protected override Task DisposeConnection()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Connections;
Expand All @@ -9,7 +10,7 @@ namespace Microsoft.Azure.SignalR
{
internal interface IConnectionFactory
{
Task<ConnectionContext> ConnectAsync(TransferFormat transferFormat, string connectionId, string target, CancellationToken cancellationToken = default);
Task<ConnectionContext> ConnectAsync(TransferFormat transferFormat, string connectionId, string target, CancellationToken cancellationToken = default, IDictionary<string, string> headers = null);

// Current plan for IAsyncDisposable is that DisposeAsync will NOT take a CancellationToken
// https://github.com/dotnet/csharplang/blob/195efa07806284d7b57550e7447dc8bd39c156bf/proposals/async-streams.md#iasyncdisposable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@ namespace Microsoft.Azure.SignalR
{
internal class ConnectionFactory : IConnectionFactory
{
// Fix issue: https://github.com/Azure/azure-signalr/issues/198
// .NET Framework has restriction about reserved string as the header name like "User-Agent"
private static readonly Dictionary<string, string> CustomHeader = new Dictionary<string, string> { { "Asrs-User-Agent", ProductInfo.GetProductInfo() } };

private readonly IServiceEndpointProvider _provider;
private readonly ILoggerFactory _loggerFactory;
private readonly string _userId;
Expand All @@ -33,15 +29,15 @@ public ConnectionFactory(string hubName, IServiceEndpointProvider provider, ISer
_hubName = hubName;
}

public async Task<ConnectionContext> ConnectAsync(TransferFormat transferFormat, string connectionId, string target, CancellationToken cancellationToken = default)
public async Task<ConnectionContext> ConnectAsync(TransferFormat transferFormat, string connectionId, string target, CancellationToken cancellationToken = default, IDictionary<string, string> headers = null)
{
var httpConnectionOptions = new HttpConnectionOptions
{
Url = GetServiceUrl(connectionId, target),
AccessTokenProvider = () => Task.FromResult(_provider.GenerateServerAccessToken(_hubName, _userId)),
Transports = HttpTransportType.WebSockets,
SkipNegotiation = true,
Headers = CustomHeader
Headers = headers
};
var httpConnection = new HttpConnection(httpConnectionOptions, _loggerFactory);
try
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Connections;
Expand All @@ -15,6 +16,10 @@ namespace Microsoft.Azure.SignalR
{
internal partial class ServiceConnection : ServiceConnectionBase
{
// Fix issue: https://github.com/Azure/azure-signalr/issues/198
// .NET Framework has restriction about reserved string as the header name like "User-Agent"
private static readonly Dictionary<string, string> CustomHeader = new Dictionary<string, string> { { "Asrs-User-Agent", ProductInfo.GetProductInfo() } };

private const string ClientConnectionCountInHub = "#clientInHub";
private const string ClientConnectionCountInServiceConnection = "#client";

Expand Down Expand Up @@ -51,7 +56,7 @@ public ServiceConnection(IServiceProtocol serviceProtocol,

protected override Task<ConnectionContext> CreateConnection(string target = null)
{
return _connectionFactory.ConnectAsync(TransferFormat.Binary, ConnectionId, target);
return _connectionFactory.ConnectAsync(TransferFormat.Binary, ConnectionId, target, headers: CustomHeader);
}

protected override Task DisposeConnection()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Connections;
Expand All @@ -22,7 +23,7 @@ public Task<TestConnectionContext> GetConnectedServerAsync()
return _waitForServerConnection.Task;
}

public Task<ConnectionContext> ConnectAsync(TransferFormat transferFormat, string connectionId, string target, CancellationToken cancellationToken = default)
public Task<ConnectionContext> ConnectAsync(TransferFormat transferFormat, string connectionId, string target, CancellationToken cancellationToken = default, IDictionary<string, string> headers = null)
{
var connection = new TestConnectionContext();
_connectCallback?.Invoke(connection);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public TestConnectionFactory(Func<TestConnection, Task> connectCallback)
}

public async Task<ConnectionContext> ConnectAsync(TransferFormat transferFormat, string connectionId, string target,
CancellationToken cancellationToken = default)
CancellationToken cancellationToken = default, IDictionary<string, string> headers = null)
{
Times.Add(DateTime.Now);

Expand Down