ASP.Net Core 3.0 SignalR HubConnection.InvokeAsync 抛出:'whatever' 的 JSON 属性 名称与另一个 属性 冲突
ASP.Net Core 3.0 SignalR HubConnection.InvokeAsync throws: The JSON property name for 'whatever' collides with another property
我的应用程序中使用了以下通过 SignalR 连接进行通信的 DTO / POCO Class:
class Base {
public string Value { get; set; }
}
class Child : Base {
public new string Value { get; set; }
}
这在 ASP.Net Core 2.2 中运行良好。但是,迁移到 3.0 后遇到以下异常:
Error Message:
System.InvalidOperationException : The JSON property name for 'Biz4x.Frontend.Web.DTO.BoardRates.BoardTemplateWithRatesDTO.Template' collides with another property.
Stack Trace:
at System.Text.Json.ThrowHelper.ThrowInvalidOperationException_SerializerPropertyNameConflict(JsonClassInfo jsonClassInfo, JsonPropertyInfo jsonPropertyInfo)
at System.Text.Json.JsonClassInfo..ctor(Type type, JsonSerializerOptions options)
at System.Text.Json.JsonSerializerOptions.GetOrAddClass(Type classType)
at System.Text.Json.WriteStackFrame.Initialize(Type type, JsonSerializerOptions options)
at System.Text.Json.JsonSerializer.WriteCore(Utf8JsonWriter writer, PooledByteBufferWriter output, Object value, Type type, JsonSerializerOptions options)
at System.Text.Json.JsonSerializer.WriteValueCore(Utf8JsonWriter writer, Object value, Type type, JsonSerializerOptions options)
at System.Text.Json.JsonSerializer.Serialize(Utf8JsonWriter writer, Object value, Type inputType, JsonSerializerOptions options)
at Microsoft.AspNetCore.SignalR.Protocol.JsonHubProtocol.WriteArguments(Object[] arguments, Utf8JsonWriter writer)
at Microsoft.AspNetCore.SignalR.Protocol.JsonHubProtocol.WriteInvocationMessage(InvocationMessage message, Utf8JsonWriter writer)
at Microsoft.AspNetCore.SignalR.Protocol.JsonHubProtocol.WriteMessageCore(HubMessage message, IBufferWriter`1 stream)
at Microsoft.AspNetCore.SignalR.Protocol.JsonHubProtocol.WriteMessage(HubMessage message, IBufferWriter`1 output)
at Microsoft.AspNetCore.SignalR.Client.HubConnection.SendHubMessage(ConnectionState connectionState, HubMessage hubMessage, CancellationToken cancellationToken)
at Microsoft.AspNetCore.SignalR.Client.HubConnection.InvokeCore(ConnectionState connectionState, String methodName, InvocationRequest irq, Object[] args, String[] streams, CancellationToken cancellationToken)
at Microsoft.AspNetCore.SignalR.Client.HubConnection.InvokeCoreAsyncCore(String methodName, Type returnType, Object[] args, CancellationToken cancellationToken)
at System.Threading.Tasks.ForceAsyncAwaiter`1.GetResult()
at Microsoft.AspNetCore.SignalR.Client.HubConnection.InvokeCoreAsync(String methodName, Type returnType, Object[] args, CancellationToken cancellationToken)
感谢任何建议和见解。
ASP.NET Core 2.2 使用 Newtonsoft.Json
而 ASP.NET Core 3.0 使用 System.Text.Json
.
您可以安装 Microsoft.AspNetCore.SignalR.Protocols.NewtonsoftJson nuget 包并将其添加到 IServiceCollection
以切换到 Newtonsoft.Json
services.AddSignalR().AddNewtonsoftJsonProtocol();
如果您想切换回以前默认使用 Newtonsoft.Json,请执行以下操作:
- 安装 Microsoft.AspNetCore.Mvc.NewtonsoftJson NuGet 包。
在ConfigureServices()
中添加对AddNewtonsoftJson()
的调用
public void ConfigureServices(IServiceCollection services)
{
...
services.AddControllers().AddNewtonsoftJson()
...
}
我的应用程序中使用了以下通过 SignalR 连接进行通信的 DTO / POCO Class:
class Base {
public string Value { get; set; }
}
class Child : Base {
public new string Value { get; set; }
}
这在 ASP.Net Core 2.2 中运行良好。但是,迁移到 3.0 后遇到以下异常:
Error Message:
System.InvalidOperationException : The JSON property name for 'Biz4x.Frontend.Web.DTO.BoardRates.BoardTemplateWithRatesDTO.Template' collides with another property.
Stack Trace:
at System.Text.Json.ThrowHelper.ThrowInvalidOperationException_SerializerPropertyNameConflict(JsonClassInfo jsonClassInfo, JsonPropertyInfo jsonPropertyInfo)
at System.Text.Json.JsonClassInfo..ctor(Type type, JsonSerializerOptions options)
at System.Text.Json.JsonSerializerOptions.GetOrAddClass(Type classType)
at System.Text.Json.WriteStackFrame.Initialize(Type type, JsonSerializerOptions options)
at System.Text.Json.JsonSerializer.WriteCore(Utf8JsonWriter writer, PooledByteBufferWriter output, Object value, Type type, JsonSerializerOptions options)
at System.Text.Json.JsonSerializer.WriteValueCore(Utf8JsonWriter writer, Object value, Type type, JsonSerializerOptions options)
at System.Text.Json.JsonSerializer.Serialize(Utf8JsonWriter writer, Object value, Type inputType, JsonSerializerOptions options)
at Microsoft.AspNetCore.SignalR.Protocol.JsonHubProtocol.WriteArguments(Object[] arguments, Utf8JsonWriter writer)
at Microsoft.AspNetCore.SignalR.Protocol.JsonHubProtocol.WriteInvocationMessage(InvocationMessage message, Utf8JsonWriter writer)
at Microsoft.AspNetCore.SignalR.Protocol.JsonHubProtocol.WriteMessageCore(HubMessage message, IBufferWriter`1 stream)
at Microsoft.AspNetCore.SignalR.Protocol.JsonHubProtocol.WriteMessage(HubMessage message, IBufferWriter`1 output)
at Microsoft.AspNetCore.SignalR.Client.HubConnection.SendHubMessage(ConnectionState connectionState, HubMessage hubMessage, CancellationToken cancellationToken)
at Microsoft.AspNetCore.SignalR.Client.HubConnection.InvokeCore(ConnectionState connectionState, String methodName, InvocationRequest irq, Object[] args, String[] streams, CancellationToken cancellationToken)
at Microsoft.AspNetCore.SignalR.Client.HubConnection.InvokeCoreAsyncCore(String methodName, Type returnType, Object[] args, CancellationToken cancellationToken)
at System.Threading.Tasks.ForceAsyncAwaiter`1.GetResult()
at Microsoft.AspNetCore.SignalR.Client.HubConnection.InvokeCoreAsync(String methodName, Type returnType, Object[] args, CancellationToken cancellationToken)
感谢任何建议和见解。
ASP.NET Core 2.2 使用 Newtonsoft.Json
而 ASP.NET Core 3.0 使用 System.Text.Json
.
您可以安装 Microsoft.AspNetCore.SignalR.Protocols.NewtonsoftJson nuget 包并将其添加到 IServiceCollection
以切换到 Newtonsoft.Json
services.AddSignalR().AddNewtonsoftJsonProtocol();
如果您想切换回以前默认使用 Newtonsoft.Json,请执行以下操作:
- 安装 Microsoft.AspNetCore.Mvc.NewtonsoftJson NuGet 包。
在
的调用ConfigureServices()
中添加对AddNewtonsoftJson()
public void ConfigureServices(IServiceCollection services) { ... services.AddControllers().AddNewtonsoftJson() ... }