SignalR Based Types

Hello, I am 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 the hub to the client, I expect that on the client side I will see the _member type as B, but I see it as A.

I tried changing the server side serializer as follows, but without effect

 var serializer = new JsonSerializer() { TypeNameHandling = TypeNameHandling.All, }; GlobalHost.DependencyResolver.Register(typeof(JsonSerializer), () => serializer); 
+6
source share
1 answer

Here's how I managed to solve it by indicating the serializer to include full types in it (by default, they should not be included).

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; 
+4
source

All Articles