SignalR Core 似乎不尊重 Newtonsoft 的 TypeNameHandling 设置

SignalR Core doesn't seem to respect Newtonsoft's TypeNameHandling setting

我传递的是 class 派生自普通 class 的元素。我发现,尽管传递了反序列化有效数据,但集线器不遵守 TypeNameHandling 并完全忽略 JSON 的 $type。无论我尝试什么,它都会反序列化到基础 class。

I event to take the JSON that I was send to the hub, past it into the hub code as a string, 然后使用 JsonConvert.Deserialize 查看会发生什么,它正确反序列化到我的派生对象。

在我的初创公司,我有

JsonConvert.DefaultSettings = () => new JsonSerializerSettings
            {
                TypeNameHandling = TypeNameHandling.Auto
            };

我不确定这是否真的被使用了,所以我创建了一个测试 JSON 转换器并在 CanRead 属性(我有 return静态假)。那被击中了。这也允许我的字符串反序列化工作。

那么集线器有什么不同之处,以至于一切似乎都正常工作除了集线器?

despite passing deserialization valid data, the hub does not respect the TypeNameHandling and completely ignores the $type of the JSON. It deserializes to the base class regardless of what I try.

我用下面的代码片段做了测试,对我有用,你可以参考。

启动中:

services.AddSignalR().AddJsonProtocol(options => {
    options.PayloadSerializerSettings = new Newtonsoft.Json.JsonSerializerSettings
    {
        TypeNameHandling = Newtonsoft.Json.TypeNameHandling.Auto
    };
});

集线器方法:

public async Task SendMessage1(Business mes)
{
    //code logic here
    //...
}

类:

public abstract class Business
{
    public string Name { get; set; }
}

public class Hotel : Business
{
    public int Stars { get; set; }
}

在客户端上,将以下 JSON 数据发送到上述集线器方法:

var ht = { "$type": "MyNamespaceHere.Hotel, NotesRTMSignalR", "Stars": 4, "Name": "Hudson Hotel" };

测试结果: