ASP.Net Core 3.0 SignalR HubConnection.InvokeAsync бросает: имя свойства JSON для «что угодно» конфликтует с другим свойством - PullRequest
0 голосов
/ 10 октября 2019

В моем приложении используется следующий класс DTO / POCO, который передается через соединение SignalR:

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)

Любые советы и идеи приветствуются.

Ответы [ 2 ]

1 голос
/ 10 октября 2019

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();
0 голосов
/ 08 ноября 2019

Если вы хотите вернуться к предыдущему стандартному использованию Newtonsoft.Json, выполните следующие действия:

  • Установите Microsoft.AspNetCore.Mvc.NewtonsoftJson NuGetпакет.
  • В ConfigureServices() добавить вызов AddNewtonsoftJson()

    public void ConfigureServices(IServiceCollection services)
    {
        ...
        services.AddControllers().AddNewtonsoftJson()
        ...
    }
    
...