Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.Net Core 3.0 SignalR HubConnection.InvokeAsync throws: The JSON property name for 'whatever' collides with another property

I have the following DTO / POCO Class used in my application which is communicated over SignalR connection:

class Base {
     public string Value { get; set; }
}
class Child : Base {
     public new string Value { get; set; }
}

This works well in ASP.Net Core 2.2. However, I bump into the following exception after migrating to 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)

Any advice and insight is Appreciated.

like image 871
Kok How Teh Avatar asked Dec 07 '25 08:12

Kok How Teh


2 Answers

ASP.NET Core 2.2 is using Newtonsoft.Json and ASP.NET Core 3.0 is using System.Text.Json.

You can install Microsoft.AspNetCore.SignalR.Protocols.NewtonsoftJson nuget package and add it to IServiceCollection to switch to Newtonsoft.Json

services.AddSignalR().AddNewtonsoftJsonProtocol();
like image 121
Kahbazi Avatar answered Dec 08 '25 21:12

Kahbazi


If you’d like to switch back to the previous default of using Newtonsoft.Json, do the following:

  • Install the Microsoft.AspNetCore.Mvc.NewtonsoftJson NuGet package.
  • In ConfigureServices() add a call to AddNewtonsoftJson()

    public void ConfigureServices(IServiceCollection services)
    {
        ...
        services.AddControllers().AddNewtonsoftJson()
        ...
    }
    
like image 41
Mazen Keeikati Avatar answered Dec 08 '25 20:12

Mazen Keeikati



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!