-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support handling function return value from worker process (#46158)
Fix Azure/azure-functions-dotnet-worker#1496 The reason to add .NET 6 and .NET 8 as TFM: The worker function return value is a Newtonsoft JObject if it's an object in the worker. Since .NET Core Frameworks, the default SignalR JSON protocol uses System.Text.Json library and can't serialize Newtonsoft JObject. Therefore, we need to wrap the JObject to a System.Text.Json serializable object with a customized JSON converter. The customized JSON converter first converts the Newtonsoft JObject to JSON with Newtonsoft library, then writes the raw JSON object with System.Text.Json writer. For .NET Standard 2.0, it's not possible to write a raw JSON with System.Text.Json writer, therefore, we need to add .NET 6 and .NET 8 as TFM to write the raw JSON.
- Loading branch information
Showing
12 changed files
with
689 additions
and
23 deletions.
There are no files selected for viewing
9 changes: 2 additions & 7 deletions
9
sdk/signalr/Microsoft.Azure.WebJobs.Extensions.SignalRService/CHANGELOG.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
302 changes: 302 additions & 0 deletions
302
...Extensions.SignalRService/api/Microsoft.Azure.WebJobs.Extensions.SignalRService.net6.0.cs
Large diffs are not rendered by default.
Oops, something went wrong.
302 changes: 302 additions & 0 deletions
302
...Extensions.SignalRService/api/Microsoft.Azure.WebJobs.Extensions.SignalRService.net8.0.cs
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 2 additions & 3 deletions
5
...bs.Extensions.SignalRService/src/Microsoft.Azure.WebJobs.Extensions.SignalRService.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
...rosoft.Azure.WebJobs.Extensions.SignalRService/src/TriggerBindings/Utils/JTokenWrapper.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using System; | ||
using System.Text.Json; | ||
using Microsoft.AspNetCore.SignalR.Protocol; | ||
using Newtonsoft.Json; | ||
using Newtonsoft.Json.Linq; | ||
|
||
namespace Microsoft.Azure.WebJobs.Extensions.SignalRService; | ||
|
||
/// <summary> | ||
/// Helps to make the <see cref="JToken"/> object correctly seralized in <see cref="JsonHubProtocol"/> that using System.Text.Json internally. | ||
/// <para>Since .Net Core 3.0, the <see cref="JsonHubProtocol"/> uses System.Text.Json library for JSON (de)serialization, which cannot handle <see cref="JToken"/> correctly. However, in isolated worker model, if a SignalR trigger function returns an object, then the SignalR extensions in host process gets a <see cref="JToken"/> object. We need to make sure the <see cref="JToken"/> object serialized correctly in the <see cref="CompletionMessage"/>.</para> | ||
/// </summary> | ||
|
||
[System.Text.Json.Serialization.JsonConverter(typeof(JTokenWrapperJsonConverter))] | ||
internal class JTokenWrapper | ||
{ | ||
public JTokenWrapper(JToken value) | ||
{ | ||
Value = value; | ||
} | ||
|
||
public JToken Value { get; } | ||
} | ||
|
||
internal class JTokenWrapperJsonConverter : System.Text.Json.Serialization.JsonConverter<JTokenWrapper> | ||
{ | ||
public override JTokenWrapper Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
|
||
public override void Write(Utf8JsonWriter writer, JTokenWrapper value, JsonSerializerOptions options) | ||
{ | ||
#if NET6_0_OR_GREATER | ||
var jsonString = JsonConvert.SerializeObject(value.Value); | ||
writer.WriteRawValue(jsonString); | ||
#elif NETSTANDARD2_0 | ||
// No need to implement. | ||
// First of all, the SignalR extensions for host process always run on .NET 6 or greater runtime when this class is first written. | ||
// Even if somehow the extensions run on .NET Framework, the JsonHubProtocol would use Newtonsoft.Json for serialization and this class would not be used. | ||
throw new NotImplementedException("Serializing Newtonsoft.Json.JsonToken with System.Text.Json is not implemented. "); | ||
#endif | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1b4c390
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The fixes have been applied to .NET6 package: Microsoft.Azure.WebJobs.Extensions.SignalRService whereas the same issue exists in .NET8 version of this package: Microsoft.Azure.Functions.Worker.Extensions.SignalRService. Please kindly confirm when will the fix be propagated to this library for .NET8 applications