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

Using ServiceTransportType.Persistent in Management SDK breaks enum serialization #1219

Closed
arek-avanade opened this issue Feb 18, 2021 · 8 comments · Fixed by #1351
Closed

Comments

@arek-avanade
Copy link

I'm using Management SDK in Azure Function to send messages back to our SignalR clients. Recently we switched to ServiceTransportType.Persistent (websocket) in the SDK to improve performance, but then messages arriving on the client side had Enums serialized as integers rather than strings, despite our Newtonsoft.Json settings. When using ServiceTransportType.Transient transport everything was working fine and our JSON settings were properly respected.

Please note since there is no way to configure Newtonsoft.Json in ServiceManagerBuilder currently, we are configuring it globally in function Startup class in the following manner:

JsonConvert.DefaultSettings = () => new JsonSerializerSettings
{
   ContractResolver = new CamelCasePropertyNamesContractResolver(),
   Converters = { new StringEnumConverter() }
};

This has worked perfectly find with 'transient' transport, so for example the below enum:

public enum Error
{
   SignalLost
}

was serialized as "SignalLost", but after switching to 'persistent' transport it's serialized as 0.

@wanlwanl
Copy link
Member

Could you provide the version of Management SDK and the function app project?
The .csproj file is better.

@arek-avanade
Copy link
Author

I cannot share the entire project file but here's an excerpt. We're running on .NET Core 3.1.

    <PackageReference Include="Microsoft.Azure.Functions.Extensions" Version="1.1.0" />
    <PackageReference Include="Microsoft.Azure.SignalR.Management" Version="1.7.1" />
    <PackageReference Include="Microsoft.Azure.WebJobs.Extensions.EventHubs" Version="4.0.1" />
    <PackageReference Include="Microsoft.Extensions.Http" Version="3.1.12" />
    <PackageReference Include="Microsoft.Extensions.Logging.ApplicationInsights" Version="2.16.0" />
    <PackageReference Include="Microsoft.NET.Sdk.Functions" Version="3.0.11" />
    <PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
    <PackageReference Include="StyleCop.Analyzers" Version="1.1.118">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
    </PackageReference>

@wanlwanl
Copy link
Member

the json serializer is changed to System.Text.Json in function v3 and .Net Core 3.1, please use:
using System.Text.Json.Serialization;
and then add JsonStringEnumConverter like this:

    [JsonConverter(typeof(JsonStringEnumConverter))]
    public enum Error
    {
        SignalLost
    }

@arek-avanade
Copy link
Author

I'm not using output binding but Microsoft.Azure.SignalR.Management nuget. The reason is actually that I don't want to use System.Text.Json.Serialization, but rather Newtonsoft.Json. I can see in the code below that in the Transient transport mode the nuget is actually using Newtonsoft.Json through RestHubLifetimeManager and RestClientclasses:

request.Content = new StringContent(JsonConvert.SerializeObject(payload, JsonSerializerSettings), Encoding.UTF8, "application/json");

But for the Persistent transport there is some more low-level SignalR stuff happening in the SDK so it's not clear how to control serialization for this case, because I don't see any JSON serialization there.

@wanlwanl
Copy link
Member

Persistent transport reuse Microsoft.Azure.SignalR package, it uses System.Text.Json by default.
Plan to expose the configuration of JSON serialization in the future, then you can configure the System.Text.Json or Newtonsoft.Json.
For now, Management SDK doesn't support it. How about keep using Transient mode for now? I will update here when the JSON configuration is exposed.

@arek-avanade
Copy link
Author

Yeah, for now we switched back to Transient mode. It'd be great to get JSON configuration exposed moving forward. Looking forward to it!

@SebastianBienert
Copy link

Hi @wanlwanl any updates on progress of message serialization settings?

@Y-Sindo
Copy link
Member

Y-Sindo commented Jul 9, 2021

@SebastianBienert We have added a method to customize the message serialization for both two modes. But the method is not public now. Will make it public for the next release.
https://github.com/Azure/azure-signalr/blob/dev/src/Microsoft.Azure.SignalR.Management/ServiceManagerBuilder.cs#L55

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants