2

I'm trying to figure out how to configure the JSON serialization settings that the Azure SignalR Management library uses. How can I specify that the JSON should be serialized with camelCase property names instead of UpperCase names?

Here's a bit of code that sends a message ...

private static IServiceManager CreateServiceManager(string connectionString)
{
    var builder = new ServiceManagerBuilder()
        .WithOptions(options => { options.ConnectionString = connectionString; });

    return builder.Build();
}

public static async Task SendLogMessageAsync(string connectionString, string userId, LogMessage logMessage)
{
    using (var manager = CreateServiceManager(connectionString))
    {
        var hubContext = await manager.CreateHubContextAsync("SystemEventHub");

        await hubContext.Clients.User(userId).SendCoreAsync("ReceiveLogMessage", new[] { logMessage });

        await hubContext.DisposeAsync();
    }
}

You can see that I'm providing a LogMessage instance as the message parameter. The SendCoreAsync method is serializing it using UpperCase property names. I'd like that to be configured to send using camelCase properties.

This is using the Microsoft.Azure.SignalR.Management nuget package.

Ryan
  • 7,733
  • 10
  • 61
  • 106

1 Answers1

2

It seems you can :-) It took a lot of looking around to find this and I came across a lot of suggested solutions (that didn't work) before finally getting to this solution.

If you're using the System.Text.Json serializer use this in your configure method in the startup class.

builder.Services.Configure<SignalROptions>(o => o.JsonObjectSerializer = new JsonObjectSerializer(
new JsonSerializerOptions()
{
    PropertyNamingPolicy = JsonNamingPolicy.CamelCase
}));

If you're using Newtonsoft use this instead

builder.Services.Configure<SignalROptions>(o => o.JsonObjectSerializer = new NewtonsoftJsonObjectSerializer(
new JsonSerializerSettings()
{
    ContractResolver = new CamelCasePropertyNamesContractResolver()
}));

This is the article i got the solution from GitHub

Morph
  • 196
  • 2
  • 7