Skip to content

Commit

Permalink
Merge pull request #188 from Azure/dev
Browse files Browse the repository at this point in the history
Add hub protocol configuration on netcore3.1 (#187)
  • Loading branch information
Y-Sindo committed Mar 19, 2021
2 parents 731c000 + e6c2d5c commit 68cac2c
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 4 deletions.
23 changes: 23 additions & 0 deletions src/SignalRServiceExtension/Config/HubProtocol.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
#if NETCOREAPP3_1
using Microsoft.AspNetCore.SignalR.Protocol;

namespace Microsoft.Azure.WebJobs.Extensions.SignalRService
{
internal enum HubProtocol
{
/// <summary>
/// Implements the SignalR Hub Protocol using System.Text.Json.
/// <see cref="JsonHubProtocol"/>
/// </summary>
SystemTextJson,

/// <summary>
/// Implements the SignalR Hub Protocol using Newtonsoft.Json.
/// <see cref="NewtonsoftJsonHubProtocol "/>
/// </summary>
NewtonsoftJson
}
}
#endif
21 changes: 17 additions & 4 deletions src/SignalRServiceExtension/Config/ServiceManagerStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,15 @@ internal class ServiceManagerStore : IServiceManagerStore
private readonly IConfiguration configuration;
private readonly IEndpointRouter router;
private readonly ConcurrentDictionary<string, IInternalServiceHubContextStore> store = new ConcurrentDictionary<string, IInternalServiceHubContextStore>();
private readonly ILogger<ServiceManagerStore> logger;
private const string hubProtocolWarning = "It's invalid to configure hub protocol on Azure Functions runtime V2. Newtonsoft.Json protocol will be used.";

public ServiceManagerStore(IConfiguration configuration, ILoggerFactory loggerFactory, IEndpointRouter router = null)
{
this.loggerFactory = loggerFactory;
this.configuration = configuration;
this.router = router;
logger = this.loggerFactory.CreateLogger<ServiceManagerStore>();
}

public IInternalServiceHubContextStore GetOrAddByConnectionStringKey(string connectionStringKey)
Expand Down Expand Up @@ -66,13 +69,23 @@ private IInternalServiceHubContextStore CreateHubContextStore(string connectionS
{
services.AddSingleton(router);
}
#if NETSTANDARD2_0
if (configuration[Constants.AzureSignalRHubProtocol] != null)
{
logger.LogWarning(hubProtocolWarning);
}
#endif
#if NETCOREAPP3_1
var jsonProtocols = services.Where(s => s.ServiceType == typeof(IHubProtocol) && s.ImplementationType == typeof(JsonHubProtocol)).ToArray();
foreach (var protocol in jsonProtocols)
var protocolStr = configuration.GetValue(Constants.AzureSignalRHubProtocol, HubProtocol.SystemTextJson);
if (protocolStr == HubProtocol.NewtonsoftJson)
{
services.Remove(protocol);
var jsonProtocols = services.Where(s => s.ServiceType == typeof(IHubProtocol) && s.ImplementationType == typeof(JsonHubProtocol)).ToArray();
foreach (var protocol in jsonProtocols)
{
services.Remove(protocol);
}
services.AddSingleton<IHubProtocol, NewtonsoftJsonHubProtocol>();
}
services.AddSingleton<IHubProtocol, NewtonsoftJsonHubProtocol>();
#endif
services.AddSingleton(services.ToList() as IReadOnlyCollection<ServiceDescriptor>);
return services.BuildServiceProvider()
Expand Down
1 change: 1 addition & 0 deletions src/SignalRServiceExtension/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ internal static class Constants
{
public const string AzureSignalRConnectionStringName = "AzureSignalRConnectionString";
public const string AzureSignalREndpoints = "Azure:SignalR:Endpoints";
public const string AzureSignalRHubProtocol = "Azure:SignalR:HubProtocol";
public const string ServiceTransportTypeName = "AzureSignalRServiceTransportType";
public const string AsrsHeaderPrefix = "X-ASRS-";
public const string AsrsConnectionIdHeader = AsrsHeaderPrefix + "Connection-Id";
Expand Down

0 comments on commit 68cac2c

Please sign in to comment.