SignalR Typenamehandling

后端 未结 3 1144
栀梦
栀梦 2020-12-03 22:43

I am trying to get SignalR to work with custom JsonSerializerSettings for its payload, specifically I\'m trying to set TypeNameHandling = TypeNameHandling.Auto.

3条回答
  •  心在旅途
    2020-12-03 22:55

    I know that this is a rather old thread and that there is an accepted answer.

    However, I had the problem that I could not make the Server read the received json correctly, that is it did only read the base classes

    However, the solution to the problem was quite simple:

    I added this line before the parameter classes:

    [JsonConverter(typeof(PolymorphicAssemblyRootConverter), typeof(ABase))]
    public class ABase
    {
    }
    
    public class ADerived : ABase
    {
        public AInner[] DifferentObjects { get; set;}
    }
    public class AInner
    {
    }
    public class AInnerDerived : AInner
    {
    }
    ...
    public class PolymorphicAssemblyRootConverter: JsonConverter
    {
        public PolymorphicAssemblyRootConverter(Type classType) :
           this(new Assembly[]{classType.Assembly})
        {
        }
        // Here comes the rest of PolymorphicAssemblyRootConverter
    }
    

    No need to set JsonSerializer on the proxy connection of the client and add it to the GlobalHost.DependencyResolver.

    It took me a long time to figure it out, I am using SignalR 2.2.1 on both client and server.

提交回复
热议问题