Hello I'm using signalR 2.02 , I can not get the correct derived class in my client .
I have the following situation
Class A {}
Class B : A{}
Class Other
{
public A _member {get;set}
}
Other instance = new Other() { _member = new B()}
I sent my instance from hub to the client , i expect on the client side i will see _member type as B , but i see it as A .
I've tried changing the serializer on the server side as follows , but to no effect
var serializer = new JsonSerializer()
{
TypeNameHandling = TypeNameHandling.All,
};
GlobalHost.DependencyResolver.Register(typeof(JsonSerializer), () => serializer);
SignalR is an asynchronous signaling library for ASP.NET that our team is working on to help build real-time multi-user web application.
SignalR can be used to add any sort of "real-time" web functionality to your ASP.NET application. While chat is often used as an example, you can do a whole lot more. Any time a user refreshes a web page to see new data, or the page implements long polling to retrieve new data, it is a candidate for using SignalR.
What is a SignalR hub. The SignalR Hubs API enables you to call methods on connected clients from the server. In the server code, you define methods that are called by client. In the client code, you define methods that are called from the server.
SignalR doesn't guarantee message delivery. Since SignalR doesn't block when you call client methods, you can invoke client methods very quickly as you've discovered. Unfortunately, the client might not always be ready to receive messages immediately once you send them, so SignalR has to buffer messages.
This is how I managed to solve by defining the serializer to include full types when needed (The default is not to include them).
On the server side :
var serializer = GlobalHost.DependencyResolver.GetService(typeof(JsonSerializer)) as JsonSerializer;
serializer.TypeNameHandling = TypeNameHandling.Auto;
GlobalHost.DependencyResolver.Register(typeof(JsonSerializer), () => serializer);
On the client side :
_connection = new HubConnection(http://localhost:8080);
_hubProxy = _connection.CreateHubProxy("MyHub");
_hubProxy.JsonSerializer.TypeNameHandling = Newtonsoft.Json.TypeNameHandling.Auto;
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