Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/main' into Enable-WriteCore-fe…
Browse files Browse the repository at this point in the history
…ature-for-compute
  • Loading branch information
welovej committed Oct 10, 2024
2 parents d8e3cfd + 4a2b1bd commit f26a809
Show file tree
Hide file tree
Showing 13 changed files with 690 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<PackageReference Include="CommandLineParser" Version="2.7.82" />
<PackageReference Include="Azure.Storage.Blobs" Version="12.16.0" />

<PackageReference Include="System.Text.Json" Version="6.0.9" />
<PackageReference Include="System.Text.Json" Version="6.0.10" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
# Release History

## 1.15.0-beta.1 (Unreleased)

### Features Added

### Breaking Changes
## 1.15.0 (2024-10-14)

### Bugs Fixed

### Other Changes
* Fixed the issue that the function return value from isolated-worker process is not handled correctly.

## 1.14.0 (2024-05-24)

Expand Down

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -254,8 +254,8 @@ public partial class SignalRParameterAttribute : System.Attribute
{
public SignalRParameterAttribute() { }
}
[Microsoft.Azure.WebJobs.Description.BindingAttribute]
[System.AttributeUsageAttribute(System.AttributeTargets.Parameter | System.AttributeTargets.ReturnValue)]
[Microsoft.Azure.WebJobs.Description.BindingAttribute(TriggerHandlesReturnValue=true)]
[System.AttributeUsageAttribute(System.AttributeTargets.Parameter)]
public partial class SignalRTriggerAttribute : System.Attribute
{
public SignalRTriggerAttribute() { }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public void Initialize(ExtensionConfigContext context)
// Trigger binding rule
var triggerBindingRule = context.AddBindingRule<SignalRTriggerAttribute>();
triggerBindingRule.AddConverter<InvocationContext, JObject>(JObject.FromObject);
triggerBindingRule.BindToTrigger<InvocationContext>(new SignalRTriggerBindingProvider(_dispatcher, _nameResolver, _serviceManagerStore, webhookException));
triggerBindingRule.BindToTrigger(new SignalRTriggerBindingProvider(_dispatcher, _nameResolver, _serviceManagerStore, webhookException));

// Non-trigger binding rule
var signalRConnectionInfoAttributeRule = context.AddBindingRule<SignalRConnectionInfoAttribute>();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>$(RequiredTargetFrameworks)</TargetFrameworks>
<TargetFrameworks>$(RequiredTargetFrameworks);net6.0;net8.0</TargetFrameworks>
<PackageId>Microsoft.Azure.WebJobs.Extensions.SignalRService</PackageId>
<Version>1.15.0-beta.1</Version>
<Version>1.15.0</Version>
<!--The ApiCompatVersion is managed automatically and should not generally be modified manually.-->
<ApiCompatVersion>1.14.0</ApiCompatVersion>
<SignAssembly>true</SignAssembly>
<IsExtensionClientLibrary>true</IsExtensionClientLibrary>
<NoWarn>$(NoWarn);CS1591;AZC0107;</NoWarn>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Threading.Tasks;

using Microsoft.AspNetCore.SignalR.Protocol;
using Newtonsoft.Json.Linq;
using InvocationMessage = Microsoft.Azure.SignalR.Serverless.Protocols.InvocationMessage;

namespace Microsoft.Azure.WebJobs.Extensions.SignalRService
Expand Down Expand Up @@ -52,7 +53,7 @@ public override async Task<HttpResponseMessage> ExecuteAsync(HttpRequestMessage
else
{
var result = await tcs.Task.ConfigureAwait(false);
completionMessage = CompletionMessage.WithResult(message.InvocationId, result);
completionMessage = CompletionMessage.WithResult(message.InvocationId, ToSafeSerializationType(result));
response = new HttpResponseMessage(HttpStatusCode.OK);
}
}
Expand All @@ -68,6 +69,18 @@ public override async Task<HttpResponseMessage> ExecuteAsync(HttpRequestMessage
return response;
}

private static object ToSafeSerializationType(object result)
{
if (result is JToken jtoken)
{
return new JTokenWrapper(jtoken);
}
else
{
return result;
}
}

private static void AssertConsistency(InvocationContext context, InvocationMessage message)
{
if (!string.Equals(context.Event, message.Target, StringComparison.OrdinalIgnoreCase))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ namespace Microsoft.Azure.WebJobs.Extensions.SignalRService
/// <summary>
/// Attribute used to mark a function that should be triggered by messages sent from SignalR clients.
/// </summary>
[AttributeUsage(AttributeTargets.ReturnValue | AttributeTargets.Parameter)]
[Binding]
[AttributeUsage(AttributeTargets.Parameter)]
#pragma warning disable CS0618 // Type or member is obsolete
[Binding(TriggerHandlesReturnValue = true)]
#pragma warning restore CS0618 // Type or member is obsolete
public class SignalRTriggerAttribute : Attribute
{
/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
using Microsoft.Azure.WebJobs.Host.Protocols;
using Microsoft.Azure.WebJobs.Host.Triggers;
using Microsoft.Extensions.Options;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace Microsoft.Azure.WebJobs.Extensions.SignalRService
Expand Down Expand Up @@ -201,6 +202,11 @@ public Task<object> GetValueAsync()
{
return Task.FromResult<object>(JObject.FromObject(_value));
}
// Isolated worker model
else if (_parameter.ParameterType == typeof(string))
{
return Task.FromResult(JsonConvert.SerializeObject(_value) as object);
}

return Task.FromResult<object>(null);
}
Expand All @@ -210,7 +216,7 @@ public string ToInvokeString()
return _value.ToString();
}

public Type Type => _parameter.GetType();
public Type Type => _parameter.ParameterType;

// No use here
public Task SetValueAsync(object value, CancellationToken cancellationToken)
Expand Down
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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public void ValidateSecurityTokenFacts(string tokenString, SecurityTokenStatus e
{
var ctx = new DefaultHttpContext();
var req = ctx.Request;
req.Headers.Add("Authorization", new StringValues(tokenString));
req.Headers.Append("Authorization", new StringValues(tokenString));

Action<TokenValidationParameters> configureTokenValidationParameters = parameters =>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,13 @@ public static HttpRequestMessage CreateHttpRequestMessage(string hub, string cat
var context = new DefaultHttpContext();
context.Request.ContentType = contentType;
context.Request.Method = "Post";
context.Request.Headers.Add(Constants.AsrsHubNameHeader, hub);
context.Request.Headers.Add(Constants.AsrsCategory, category);
context.Request.Headers.Add(Constants.AsrsEvent, @event);
context.Request.Headers.Add(Constants.AsrsConnectionIdHeader, connectionId);
context.Request.Headers.Append(Constants.AsrsHubNameHeader, hub);
context.Request.Headers.Append(Constants.AsrsCategory, category);
context.Request.Headers.Append(Constants.AsrsEvent, @event);
context.Request.Headers.Append(Constants.AsrsConnectionIdHeader, connectionId);
if (signatures != null)
{
context.Request.Headers.Add(Constants.AsrsSignature, string.Join(",", signatures));
context.Request.Headers.Append(Constants.AsrsSignature, string.Join(",", signatures));
}
context.Request.Body = content == null ? Stream.Null : new MemoryStream(content);

Expand Down

0 comments on commit f26a809

Please sign in to comment.