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
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.
SignalR is an asynchronous signaling library for ASP.NET that our team is working on to help build real-time multi-user web application.
If your SignalR application transmits sensitive information between the client and server, use SSL for the transport.
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.
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
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();
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With