Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SignalR not serializing enum values on custom type

I have a self-hosted SignalR server passing data to a .NET SignalR client. Essentially, it is doing a continuous or on-demand update of a custom type. When I want to trigger an update, I create a data transfer object, load it with the data to send, and send it via SignalR. The client side captures the update and displays the new values (I have a WPF app and a Console app and both do essentially the same thing - the WPF app just does it a little "prettier"). Everything works fine for most of the properties on my DTO. But there are two properties that are enum values and those always come across to the client as the enum's 0-value (in my case "Unknown") no matter what I set it to on the server side. On the server side, I trace it all the way to the

Clients.All.updateData(data);

call and the DTO (data) has the correct enum values all the way to that point. But putting a breakpoint on the client side right at the point of the function that receives calls from the server, the DTO has incorrect (0) values for both enums. I have searched online for any possible issues with SignalR serializing enum values and I have not come across anything. Is there something I'm missing? Thanks in advance.

Dennis

like image 423
Dennis Avatar asked Jul 20 '16 15:07

Dennis


People also ask

What is hubName in SignalR?

SignalR Hubs are a way to logically group connections as documented. Take an example of a chat application. A group of users could be part of the same hub which allows for sending messages between users in the group. The hubName here can be any string which is used to scope messages being sent between clients.

Is SignalR asynchronous?

SignalR is an asynchronous signaling library for ASP.NET that our team is working on to help build real-time multi-user web application.

Does SignalR need SSL?

If your SignalR application transmits sensitive information between the client and server, use SSL for the transport.

How do I send a message to a specific signal SignalR?

public async Task BroadcastToUser(string data, string userId) => await Clients. User(userId). SendAsync("broadcasttouser", data); Remember that when we are sending messages to a user, they will be sent to all connections associated with that user and not just any particular connection.


2 Answers

ASP.NET Core 3.x or higher

using System.Text.Json.Serialization;

services.AddSignalR()
    .AddJsonProtocol(options =>
    {
        options.PayloadSerializerOptions.Converters
           .Add(new JsonStringEnumConverter());
    });

ASP.NET Core 2.2 or lower

using Newtonsoft.Json.Converters;

services.AddSignalR()
    .AddJsonProtocol(options =>
    {
        options.PayloadSerializerSettings.Converters
            .Add(new StringEnumConverter());
    });

Additional Reading: ASP.NET Core SignalR configuration

like image 198
spottedmahn Avatar answered Sep 29 '22 11:09

spottedmahn


You said you checked everything online, but I still hope this link could be useful.

Just adjust your Startup class:

public class Startup
{
public void Configuration(IAppBuilder app)
{
    // Create JsonSerializer with StringEnumConverter.
    var serializer = new JsonSerializer();
    serializer.Converters.Add(new StringEnumConverter());
    // Register the serializer.
    GlobalHost.DependencyResolver.Register(typeof(JsonSerializer), () => serializer);

    app.MapSignalR();
    }
}
like image 31
danny Avatar answered Sep 29 '22 12:09

danny