Skip to content

Commit 0fa8d45

Browse files
authored
[SignalR functions bindings] Self-implemented MessagePack hub protocol (Azure#47649)
As `Microsoft.AspNetCore.SignalR.Protocols.MessagePack` 1.x relies on an unsecure `MessagePack` version 1.x, and we cannot update `Microsoft.AspNetCore.SignalR.Protocols.MessagePack` to 2.x as we have to support .NET Standard 2.0 framework, we implement the required functionalities of `Microsoft.AspNetCore.SignalR.Protocols.MessagePack` with `MessagePack` 2.0 * Copied `MessagePackHubProtocol` files from https://github.com/dotnet/aspnetcore/blob/0825def633c99d9fdd74e47e69bcde3935a5fe74/ * Self-implemented MessagePack Hub Protocol * Fix security code alert * Cleanup dependencies This pull request includes several updates and improvements to the `Microsoft.Azure.WebJobs.Extensions.SignalRService` package, including dependency updates, breaking changes, and new internal implementations for MessagePack protocol support. ### Dependency updates: * Updated `MessagePack` to version 2.5.192 and `Microsoft.Azure.SignalR` packages to version 1.29.0 in `eng/Packages.Data.props` and `CHANGELOG.md` files. [[1]](diffhunk://#diff-93a28d9569550c68624a8ad2209a6fae1d4f88237e4b5414eed2ecac4ef8c98fL212-R215) [[2]](diffhunk://#diff-4d5b0364bdfdd9ef92d9390778dc219802aaa0928538cb2cd57b68dbe0c29221L3-R15) ### Breaking changes: * Removed .NET 6.0 support from `Microsoft.Azure.WebJobs.Extensions.SignalRService.csproj`. [[1]](diffhunk://#diff-4d5b0364bdfdd9ef92d9390778dc219802aaa0928538cb2cd57b68dbe0c29221L3-R15) [[2]](diffhunk://#diff-d7d1fc92bb60f4162589c9d75a13093a5c1388addb9ed263f3b8269eccd623deL1-R6) ### New internal implementations for MessagePack protocol: * Added `BinaryMessageFormatter`, `DefaultMessagePackHubProtocolWorker`, `MessagePackHubProtocol`, and `MessagePackHubProtocolWorker` classes to support MessagePack serialization and deserialization. [[1]](diffhunk://#diff-d078a537b71b42d35b0edfe9d0b29af9a4015b69a4f34c0a249eca42ee30c93fR1-R56) [[2]](diffhunk://#diff-a76e5671b348949cd69c7c0d48230ba174198702ca45b5b7e5368a8cc8cbd9e2R1-R41) [[3]](diffhunk://#diff-72c85cc1f4ff9895e501b28007e8369acfd4881c70893ee96f4a97f0dbd20debR1-R97) [[4]](diffhunk://#diff-666c7b6452ef625804593260147713eba1eb6ec088285711dfc90b6bc202b481R1-R207) ### Code improvements: * Added `JTokenWrapperMessagePackFormatter` to `JTokenWrapper` class for MessagePack serialization support. [[1]](diffhunk://#diff-3b3afd72fe7b3ec3ff0c24b0f3d8641ba5620774ad29921f462595747e91e058L18-R26) [[2]](diffhunk://#diff-3b3afd72fe7b3ec3ff0c24b0f3d8641ba5620774ad29921f462595747e91e058R49-R62) ### Test updates: * Updated test cases to use `MicrosoftEntraAccessKey` instead of `AadAccessKey` and added validation for issuer and audience in `DefaultSecurityTokenValidatorTests`. [[1]](diffhunk://#diff-703e2ebb42f2b10519f5f7f40dc765fba826f126042a9d5833ca6a68b4e1d32eL44-R47) [[2]](diffhunk://#diff-703e2ebb42f2b10519f5f7f40dc765fba826f126042a9d5833ca6a68b4e1d32eL68-R71) [[3]](diffhunk://#diff-87b72117be06a7d56bfa5afc7195979729da5ee25e66a63d4b86f7e3c6e27eb6L50-R55) <details> <summary>Details</summary> </details>
1 parent 6d25121 commit 0fa8d45

File tree

12 files changed

+853
-27
lines changed

12 files changed

+853
-27
lines changed

eng/Packages.Data.props

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -209,11 +209,10 @@
209209
<PackageReference Update="CloudNative.CloudEvents.SystemTextJson" Version="2.0.0" />
210210
<PackageReference Update="Google.Protobuf" Version="3.24.3" />
211211
<PackageReference Update="Grpc.Tools" Version="2.51.0" PrivateAssets="all" />
212-
<PackageReference Update="MessagePack" Version="1.9.11" />
213-
<PackageReference Update="Microsoft.AspNetCore.SignalR.Protocols.MessagePack" Version="1.1.5" />
214-
<PackageReference Update="Microsoft.Azure.SignalR" Version="1.25.2" />
215-
<PackageReference Update="Microsoft.Azure.SignalR.Management" Version="1.25.2" />
216-
<PackageReference Update="Microsoft.Azure.SignalR.Protocols" Version="1.25.2" />
212+
<PackageReference Update="MessagePack" Version="2.5.192" />
213+
<PackageReference Update="Microsoft.Azure.SignalR" Version="1.29.0" />
214+
<PackageReference Update="Microsoft.Azure.SignalR.Management" Version="1.29.0" />
215+
<PackageReference Update="Microsoft.Azure.SignalR.Protocols" Version="1.29.0" />
217216
<PackageReference Update="Microsoft.Azure.SignalR.Serverless.Protocols" Version="1.10.0" />
218217
<PackageReference Update="Microsoft.Azure.WebJobs" Version="3.0.37" />
219218
<PackageReference Update="Microsoft.Azure.WebJobs.Sources" Version="3.0.37" PrivateAssets="All"/>

sdk/signalr/Microsoft.Azure.WebJobs.Extensions.SignalRService/CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
# Release History
22

3-
## 1.16.0-beta.1 (Unreleased)
3+
## 2.0.0-beta.1 (Unreleased)
44

55
### Features Added
66

77
### Breaking Changes
8+
* Remove .NET 6.0 support.
89

910
### Bugs Fixed
11+
* Correctly support returning result for SignalR invocation in MessagePack protocol from isolated-worker process.
1012

1113
### Other Changes
14+
* Update `MessagePack` to 2.5.192
15+
* Update `Microsoft.Azure.SignalR`, `Microsoft.Azure.SignalR.Management`, `Microsoft.Azure.SignalR.Protocols` to 1.29.0
1216

1317
## 1.15.0 (2024-10-14)
1418

Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFrameworks>$(RequiredTargetFrameworks);net6.0;net8.0</TargetFrameworks>
4+
<TargetFrameworks>$(RequiredTargetFrameworks)</TargetFrameworks>
55
<PackageId>Microsoft.Azure.WebJobs.Extensions.SignalRService</PackageId>
6-
<Version>1.16.0-beta.1</Version>
7-
<ApiCompatVersion>1.15.0</ApiCompatVersion>
6+
<Version>2.0.0-beta.1</Version>
87
<!--The ApiCompatVersion is managed automatically and should not generally be modified manually.-->
98
<SignAssembly>true</SignAssembly>
109
<IsExtensionClientLibrary>true</IsExtensionClientLibrary>
@@ -13,15 +12,16 @@
1312

1413
<ItemGroup>
1514
<PackageReference Include="MessagePack" />
16-
<PackageReference Include="Microsoft.Bcl.AsyncInterfaces" />
1715
<PackageReference Include="Microsoft.Extensions.Azure" />
18-
<PackageReference Include="Microsoft.AspNetCore.Http.Connections" />
19-
<PackageReference Include="Microsoft.AspNetCore.SignalR.Protocols.MessagePack" />
2016
<PackageReference Include="Microsoft.Azure.Functions.Extensions" />
2117
<PackageReference Include="Microsoft.Azure.SignalR.Management" />
2218
<PackageReference Include="Microsoft.Azure.SignalR" />
2319
<PackageReference Include="Microsoft.Azure.SignalR.Protocols" />
2420
<PackageReference Include="Microsoft.Azure.SignalR.Serverless.Protocols" />
2521
<PackageReference Include="System.IdentityModel.Tokens.Jwt" />
2622
</ItemGroup>
23+
24+
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
25+
<PackageReference Include="Microsoft.AspNetCore.Http.Connections" />
26+
</ItemGroup>
2727
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
using System;
5+
using System.Buffers;
6+
7+
namespace Microsoft.AspNetCore.Internal;
8+
9+
/// <summary>
10+
/// Copied from https://github.com/dotnet/aspnetcore/blob/0825def633c99d9fdd74e47e69bcde3935a5fe74/
11+
/// </summary>
12+
internal static class BinaryMessageFormatter
13+
{
14+
public static void WriteLengthPrefix(long length, IBufferWriter<byte> output)
15+
{
16+
Span<byte> lenBuffer = stackalloc byte[5];
17+
18+
var lenNumBytes = WriteLengthPrefix(length, lenBuffer);
19+
20+
output.Write(lenBuffer.Slice(0, lenNumBytes));
21+
}
22+
23+
public static int WriteLengthPrefix(long length, Span<byte> output)
24+
{
25+
// This code writes length prefix of the message as a VarInt. Read the comment in
26+
// the BinaryMessageParser.TryParseMessage for details.
27+
var lenNumBytes = 0;
28+
do
29+
{
30+
ref var current = ref output[lenNumBytes];
31+
current = (byte)(length & 0x7f);
32+
length >>= 7;
33+
if (length > 0)
34+
{
35+
current |= 0x80;
36+
}
37+
lenNumBytes++;
38+
}
39+
while (length > 0);
40+
41+
return lenNumBytes;
42+
}
43+
44+
public static int LengthPrefixLength(long length)
45+
{
46+
var lenNumBytes = 0;
47+
do
48+
{
49+
length >>= 7;
50+
lenNumBytes++;
51+
}
52+
while (length > 0);
53+
54+
return lenNumBytes;
55+
}
56+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
using System;
5+
using System.IO;
6+
7+
using MessagePack;
8+
9+
namespace Microsoft.AspNetCore.SignalR.Protocol;
10+
11+
#nullable enable
12+
13+
/// <summary>
14+
/// Copied from https://github.com/dotnet/aspnetcore/blob/0825def633c99d9fdd74e47e69bcde3935a5fe74/
15+
/// </summary>
16+
internal sealed class DefaultMessagePackHubProtocolWorker : MessagePackHubProtocolWorker
17+
{
18+
private readonly MessagePackSerializerOptions _messagePackSerializerOptions;
19+
20+
public DefaultMessagePackHubProtocolWorker(MessagePackSerializerOptions messagePackSerializerOptions)
21+
{
22+
_messagePackSerializerOptions = messagePackSerializerOptions;
23+
}
24+
25+
protected override object? DeserializeObject(ref MessagePackReader reader, Type type, string field)
26+
{
27+
try
28+
{
29+
return MessagePackSerializer.Deserialize(type, ref reader, _messagePackSerializerOptions);
30+
}
31+
catch (Exception ex)
32+
{
33+
throw new InvalidDataException($"Deserializing object of the `{type.Name}` type for '{field}' failed.", ex);
34+
}
35+
}
36+
37+
protected override void Serialize(ref MessagePackWriter writer, Type type, object value)
38+
{
39+
MessagePackSerializer.Serialize(type, ref writer, value, _messagePackSerializerOptions);
40+
}
41+
}

0 commit comments

Comments
 (0)