Skip to content

Commit

Permalink
Merge pull request #255 from Azure/dev
Browse files Browse the repository at this point in the history
Release 1.5.0
  • Loading branch information
Y-Sindo authored Jul 5, 2021
2 parents 2f39c96 + d2feb7c commit 264cab8
Show file tree
Hide file tree
Showing 14 changed files with 162 additions and 91 deletions.
5 changes: 3 additions & 2 deletions build/dependencies.props
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
</PropertyGroup>
<PropertyGroup Label="Package Versions">
<MicroBuildCorePackageVersion>0.3.0</MicroBuildCorePackageVersion>
<MicrosoftAzureSignalRManagement>1.8.2</MicrosoftAzureSignalRManagement>
<MicrosoftAzureSignalRManagement>1.9.1</MicrosoftAzureSignalRManagement>
<MicrosoftAzureFunctionsExtensionsVersion>1.0.0</MicrosoftAzureFunctionsExtensionsVersion>
<MicrosoftNETTestSdkPackageVersion>15.8.0</MicrosoftNETTestSdkPackageVersion>
<MoqPackageVersion>4.9.0</MoqPackageVersion>
Expand All @@ -17,7 +17,7 @@
<MicrosoftAspNetCoreSignalRProtocolsMessagePackPackageVersion>1.1.5</MicrosoftAspNetCoreSignalRProtocolsMessagePackPackageVersion>
<SystemIdentityModelTokensJwtVersion>5.5.0</SystemIdentityModelTokensJwtVersion>
<MicrosoftAspNetCoreSignalRPackageVersion>1.0.0</MicrosoftAspNetCoreSignalRPackageVersion>
<MicrosoftAspNetCoreSignalRProtocolsNewtonsoftJson>3.0.0</MicrosoftAspNetCoreSignalRProtocolsNewtonsoftJson>
<MicrosoftAspNetCoreSignalRProtocolsNewtonsoftJson3_1>3.0.0</MicrosoftAspNetCoreSignalRProtocolsNewtonsoftJson3_1>
<MicrosoftAspNetCoreSignalRClient>5.0.3</MicrosoftAspNetCoreSignalRClient>
<MicrosoftExtensionsConfiguration>5.0.0</MicrosoftExtensionsConfiguration>

Expand All @@ -27,6 +27,7 @@
<MicrosoftAzureFunctionsExtensionsV2>1.0.0</MicrosoftAzureFunctionsExtensionsV2>
<MicrosoftAzureFunctionsExtensionsV3>1.1.0</MicrosoftAzureFunctionsExtensionsV3>
<MicrosoftAzureWebJobsHostStorage>3.0.14</MicrosoftAzureWebJobsHostStorage>
<MicrosoftAspNetCoreSignalRProtocolsNewtonsoftJson5_0>5.0.1</MicrosoftAspNetCoreSignalRProtocolsNewtonsoftJson5_0>

</PropertyGroup>
<Import Project="$(DotNetPackageVersionPropsPath)" Condition=" '$(DotNetPackageVersionPropsPath)' != '' " />
Expand Down
4 changes: 2 additions & 2 deletions run.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,10 @@ function Get-KoreBuild {
[System.IO.Compression.ZipFile]::ExtractToDirectory($tmpfile, $korebuildPath)
}
# Hack to use Core version
Write-Host "!!! Hack to use .NET Core SDK 5.0.103"
Write-Host "!!! Hack to use .NET Core SDK 5.0.301"
$sdkversion = Get-ChildItem -Path $korebuildPath -Include sdk.version -Recurse
$sdkpath = Join-Path $sdkversion.DirectoryName $sdkversion.Name
Set-Content -Path $sdkpath -Value "5.0.103" -Force
Set-Content -Path $sdkpath -Value "5.0.301" -Force
}
catch {
Remove-Item -Recurse -Force $korebuildPath -ErrorAction Ignore
Expand Down
4 changes: 2 additions & 2 deletions run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,9 @@ get_korebuild() {
rm "$tmpfile" || true
fi

echo "!!! Hack to use .NET Core SDK 5.0.103"
echo "!!! Hack to use .NET Core SDK 5.0.301"
local filePath=`find $korebuild_path -name sdk.version`
echo "5.0.103" > $filePath
echo "5.0.301" > $filePath

source "$korebuild_path/KoreBuild.sh"
} || {
Expand Down
32 changes: 18 additions & 14 deletions src/SignalRServiceExtension/Config/DependencyInjectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using Microsoft.AspNetCore.SignalR.Protocol;
using Microsoft.Azure.SignalR.Management;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Newtonsoft.Json;

namespace Microsoft.Azure.WebJobs.Extensions.SignalRService
{
Expand All @@ -15,18 +15,27 @@ internal static class DependencyInjectionExtensions

public static IServiceCollection SetHubProtocol(this IServiceCollection services, IConfiguration configuration)
{
if (Environment.Version.Major == 4 && configuration[Constants.AzureSignalRHubProtocol] != null)
var hubProtocolConfig = configuration[Constants.AzureSignalRHubProtocol];
if (hubProtocolConfig is not null && Environment.Version.Major == 4)
{
// Actually is .Net Core 2.x
// .Net Core 2.x is always Newtonsoft.Json.
throw new InvalidOperationException(HubProtocolError);
}
#if NETCOREAPP3_1 || NETCOREAPP3_0 || NETSTANDARD2_0
else if (!DotnetRuntime(configuration) || UserSpecifyNewtonsoft(configuration))

//indicates Newtonsoft, camcelCase
if (configuration.GetValue(Constants.AzureSignalRNewtonsoftCamelCase, false))
{
// .Net Core 3.1, overwrite the System.Text.Json Protocol.
services.TryAddEnumerable(ServiceDescriptor.Singleton<IHubProtocol, NewtonsoftJsonHubProtocol>());
// The default options is camelCase.
return services.AddNewtonsoftHubProtocol(o => { });
}
#endif

if (!DotnetRuntime(configuration) || Enum.TryParse<HubProtocol>(hubProtocolConfig, out var result) && result == HubProtocol.NewtonsoftJson)
{
// Reset the options to keep backward compatibility.
return services.AddNewtonsoftHubProtocol(o => o.PayloadSerializerSettings = new JsonSerializerSettings());
}

//If hubProtocolConfig is SystemTextJson for .Net Core 3.1, do nothing, as transient mode doesn't accept it and persisent mode is already System.Text.Json by default.
return services;
}

Expand All @@ -36,10 +45,5 @@ private static bool DotnetRuntime(IConfiguration configuration)
//unit test environment
return workerRuntime == null || workerRuntime == Constants.DotnetWorker;
}

private static bool UserSpecifyNewtonsoft(IConfiguration configuration)
{
return configuration.GetValue(Constants.AzureSignalRHubProtocol, HubProtocol.SystemTextJson) == HubProtocol.NewtonsoftJson;
}
}
}
1 change: 1 addition & 0 deletions src/SignalRServiceExtension/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,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 static string AzureSignalRNewtonsoftCamelCase = $"{AzureSignalRHubProtocol}:{HubProtocol.NewtonsoftJson}:CamelCase";
public const string ServiceTransportTypeName = "AzureSignalRServiceTransportType";
public const string AsrsHeaderPrefix = "X-ASRS-";
public const string AsrsConnectionIdHeader = AsrsHeaderPrefix + "Connection-Id";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'netcoreapp3.1'">
<PackageReference Include="Microsoft.AspNetCore.SignalR.Protocols.NewtonsoftJson" Version="$(MicrosoftAspNetCoreSignalRProtocolsNewtonsoftJson)" />
<PackageReference Include="Microsoft.AspNetCore.SignalR.Protocols.NewtonsoftJson" Version="$(MicrosoftAspNetCoreSignalRProtocolsNewtonsoftJson3_1)" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,6 @@ public class InvocationContext
/// </summary>
public IDictionary<string, string> Claims { get; set; }

internal IServiceHubContext HubContext { get; set; }
internal ServiceHubContext HubContext { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,23 @@ public static Task<IHubClients> GetClientsAsync(this InvocationContext invocatio
/// </summary>
public static Task<IGroupManager> GetGroupsAsync(this InvocationContext invocationContext)
{
return Task.FromResult(invocationContext.HubContext.Groups);
return Task.FromResult(invocationContext.HubContext.Groups as IGroupManager);
}

/// <summary>
/// Get the user group manager of this hub.
/// </summary>
public static Task<IUserGroupManager> GetUserGroupManagerAsync(this InvocationContext invocationContext)
{
return Task.FromResult(invocationContext.HubContext.UserGroups);
return Task.FromResult(invocationContext.HubContext.UserGroups as IUserGroupManager);
}

/// <summary>
/// Get the client manager of this hub.
/// </summary>
public static ClientManager GetClientManager(this InvocationContext invocationContext)
{
return invocationContext.HubContext.ClientManager;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ internal class SignalRTriggerBinding : ITriggerBinding
private readonly SignalRTriggerAttribute _attribute;
private readonly ISignalRTriggerDispatcher _dispatcher;
private readonly AccessKey[] _accessKeys;
private readonly IServiceHubContext _hubContext;
private readonly ServiceHubContext _hubContext;

public SignalRTriggerBinding(ParameterInfo parameterInfo, SignalRTriggerAttribute attribute, ISignalRTriggerDispatcher dispatcher, AccessKey[] accessKeys, IServiceHubContext hubContext)
public SignalRTriggerBinding(ParameterInfo parameterInfo, SignalRTriggerAttribute attribute, ISignalRTriggerDispatcher dispatcher, AccessKey[] accessKeys, ServiceHubContext hubContext)
{
_parameterInfo = parameterInfo ?? throw new ArgumentNullException(nameof(parameterInfo));
_attribute = attribute ?? throw new ArgumentNullException(nameof(attribute));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Azure.SignalR.Management;
using Microsoft.Azure.WebJobs.Description;
using Microsoft.Azure.WebJobs.Host;
using Microsoft.Azure.WebJobs.Host.Triggers;
Expand Down Expand Up @@ -55,7 +56,7 @@ public async Task<ITriggerBinding> TryCreateAsync(TriggerBindingProviderContext

var hubContext = await _managerStore.GetOrAddByConnectionStringKey(attribute.ConnectionStringSetting).GetAsync(resolvedAttribute.HubName);

return new SignalRTriggerBinding(parameterInfo, resolvedAttribute, _dispatcher, accessKeys, hubContext);
return new SignalRTriggerBinding(parameterInfo, resolvedAttribute, _dispatcher, accessKeys, hubContext as ServiceHubContext);
}

internal SignalRTriggerAttribute GetParameterResolvedAttribute(SignalRTriggerAttribute attribute, ParameterInfo parameterInfo)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<PackageReference Include="xunit" Version="$(XunitPackageVersion)" />
<PackageReference Include="xunit.runner.visualstudio" Version="$(XunitRunnerVisualstudioPackageVersion)" />
<PackageReference Include="Microsoft.Azure.SignalR.Management" Version="$(MicrosoftAzureSignalRManagement)" />
<PackageReference Include="Microsoft.AspNetCore.SignalR.Protocols.NewtonsoftJson" Version="$(MicrosoftAspNetCoreSignalRProtocolsNewtonsoftJson)" />
<PackageReference Include="Microsoft.AspNetCore.SignalR.Protocols.NewtonsoftJson" Version="$(MicrosoftAspNetCoreSignalRProtocolsNewtonsoftJson5_0)" />
</ItemGroup>

<ItemGroup>
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.SignalR;
using Microsoft.AspNetCore.SignalR.Protocol;
using Microsoft.Azure.SignalR.Management;
using Microsoft.Azure.SignalR.Tests.Common;
using Microsoft.Azure.WebJobs.Extensions.SignalRService;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.Extensions.Options;
using Newtonsoft.Json.Serialization;
using Xunit;

namespace SignalRServiceExtension.Tests
{
public class SetNewtonsoftHubProtocolFacts
{
[Fact]
public void EmptyHubProtocolSetting_DoNothing()
{
var configuration = new ConfigurationBuilder().AddInMemoryCollection().Build();
var services = new ServiceCollection()
.SetHubProtocol(configuration);
Assert.Empty(services);
}

#if NETCOREAPP2_1

[Fact]
public void SetHubProtocol_Throw()
{
var configuration = new ConfigurationBuilder().AddInMemoryCollection().Build();
configuration[Constants.AzureSignalRHubProtocol] = HubProtocol.SystemTextJson.ToString();
var services = new ServiceCollection();
Assert.Throws<InvalidOperationException>(() => services.SetHubProtocol(configuration));
}

#endif

#if NETCOREAPP3_1

[Fact]
public void SetSystemTextJson_DoNothing()
{
var configuration = new ConfigurationBuilder().AddInMemoryCollection().Build();
configuration[Constants.AzureSignalRHubProtocol] = HubProtocol.SystemTextJson.ToString();
var services = new ServiceCollection()
.SetHubProtocol(configuration);
Assert.Empty(services);
}

[Fact]
public async Task SetNewtonsoftPersistent()
{
var serviceManagerStore = CreateServiceManagerStore(out var configuration);
configuration[Constants.ServiceTransportTypeName] = "Persistent";
var hubContext = await serviceManagerStore.GetOrAddByConnectionStringKey(Constants.AzureSignalRConnectionStringName).GetAsync("hub") as ServiceHubContextImpl;
var hubProtocol = hubContext.ServiceProvider.GetRequiredService<IHubProtocol>();
Assert.IsType<NewtonsoftJsonHubProtocol>(hubProtocol);
var newtonsoftOptions = hubContext.ServiceProvider.GetRequiredService<IOptions<NewtonsoftJsonHubProtocolOptions>>();
Assert.Null(newtonsoftOptions.Value.PayloadSerializerSettings.ContractResolver);
}

[Fact]
public async Task SetNewtonsoftInTransientModeAsync()
{
var serviceManagerStore = CreateServiceManagerStore(out var configuration);
var hubContext = await serviceManagerStore.GetOrAddByConnectionStringKey(Constants.AzureSignalRConnectionStringName).GetAsync("hub") as ServiceHubContextImpl;

var restHubProtocol = hubContext.ServiceProvider.GetRequiredService<IRestHubProtocol>();
Assert.IsType<NewtonsoftRestHubProtocol>(restHubProtocol);
var newtonsoftOptions = hubContext.ServiceProvider.GetRequiredService<IOptions<NewtonsoftServiceHubProtocolOptions>>();
Assert.Null(newtonsoftOptions.Value.PayloadSerializerSettings.ContractResolver);
}

[Fact]
public async Task SetNewtonsoftCamelCaseInTransientModeAsync()
{
var serviceManagerStore = CreateServiceManagerStore(out var configuration);
configuration["Azure:SignalR:HubProtocol:NewtonsoftJson:CamelCase"] = "true";

var hubContext = await serviceManagerStore.GetOrAddByConnectionStringKey(Constants.AzureSignalRConnectionStringName).GetAsync("hub") as ServiceHubContextImpl;
var restHubProtocol = hubContext.ServiceProvider.GetRequiredService<IRestHubProtocol>();
Assert.IsType<NewtonsoftRestHubProtocol>(restHubProtocol);
var newtonsoftOptions = hubContext.ServiceProvider.GetRequiredService<IOptions<NewtonsoftServiceHubProtocolOptions>>();
Assert.IsType<CamelCasePropertyNamesContractResolver>(newtonsoftOptions.Value.PayloadSerializerSettings.ContractResolver);
}

[Fact]
public async Task SetNewtonsoftCamelCaseInPersistentModeAsync()
{
var serviceManagerStore = CreateServiceManagerStore(out var configuration);
configuration["Azure:SignalR:HubProtocol:NewtonsoftJson:CamelCase"] = "true";
configuration[Constants.ServiceTransportTypeName] = "Persistent";

var hubContext = await serviceManagerStore.GetOrAddByConnectionStringKey(Constants.AzureSignalRConnectionStringName).GetAsync("hub") as ServiceHubContextImpl;
var hubProtocol = hubContext.ServiceProvider.GetRequiredService<IHubProtocol>();
Assert.IsType<NewtonsoftJsonHubProtocol>(hubProtocol);
var newtonsoftOptions = hubContext.ServiceProvider.GetRequiredService<IOptions<NewtonsoftJsonHubProtocolOptions>>();
Assert.IsType<CamelCasePropertyNamesContractResolver>(newtonsoftOptions.Value.PayloadSerializerSettings.ContractResolver);
}

private ServiceManagerStore CreateServiceManagerStore(out IConfiguration configuration)
{
configuration = new ConfigurationBuilder().AddInMemoryCollection().Build();
configuration[Constants.AzureSignalRHubProtocol] = HubProtocol.NewtonsoftJson.ToString();
configuration[Constants.AzureSignalRConnectionStringName] = FakeEndpointUtils.GetFakeConnectionString(1).Single();

return new ServiceManagerStore(configuration, NullLoggerFactory.Instance, null);
}
#endif
}
}
2 changes: 1 addition & 1 deletion version.props
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project>
<PropertyGroup>
<VersionPrefix>1.4.2</VersionPrefix>
<VersionPrefix>1.5.0</VersionPrefix>
<VersionSuffix>preview1</VersionSuffix>
<PackageVersion Condition="'$(IsFinalBuild)' == 'true' AND '$(VersionSuffix)' == 'rtm' ">$(VersionPrefix)</PackageVersion>
<PackageVersion Condition="'$(IsFinalBuild)' == 'true' AND '$(VersionSuffix)' != 'rtm' ">$(VersionPrefix)-$(VersionSuffix)-final</PackageVersion>
Expand Down

0 comments on commit 264cab8

Please sign in to comment.