Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SignalR derived types

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); 
like image 654
Shachaf.Gortler Avatar asked Feb 19 '14 06:02

Shachaf.Gortler


People also ask

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.

What is SignalR example?

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 are hubs in 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.

Does SignalR guarantee delivery?

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.


1 Answers

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;
like image 189
Shachaf.Gortler Avatar answered Oct 07 '22 10:10

Shachaf.Gortler