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

Add hub protocol configuration on netcore3.1 (#187) #188

Merged
merged 1 commit into from
Mar 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
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