Newtonsoft.Json.JsonSerializationException: Could not create an instance of type


Our microservices use NServiceBus SQL Server persistence and AWS SQS & SNS Topics for the message subscriptions. All event messages published by a microservice inherit from an abstract base class: BaseEvent

I have recently upgraded from NServiceBus.Newtonsoft.Json 2.3.0 to 2.4.0.
I also use the following code to explicitly specify the TypeNameHandling.None in the JsonSerializerSettings for the NewtonsoftJsonSerializer (although I understand that in 2.4.0 the serializer by default uses TypeNameHandling.None)

            .Settings(new Newtonsoft.Json.JsonSerializerSettings
                TypeNameHandling = Newtonsoft.Json.TypeNameHandling.None

Most of our services work fine, however, one service is throwing this exception when it receives: UserProfileUpdatedEvent

Newtonsoft.Json.JsonSerializationException: Could not create an instance of type Common.Framework.Contracts.BaseEvent. Type is an interface or abstract class and cannot be instantiated.

In the SQS, I see the message in the error queue and the NServiceBus.EnclosedMessageTypes is:

 ServiceA.Contract, Version=, Culture=neutral, 
public class UserProfileUpdatedEvent : BaseUserEvent {  }

public class BaseUserEvent : BaseEvent
    public Key UserKey { get; set; }

public abstract class BaseEvent : IEvent
    public Key EventKey { get; set; }

    public string Error { get; set; }

Please note, both the publisher and consumer services share the assembly that contains ServiceA.Contract.Events.UserProfileUpdatedEvent

We’re currently using the following versions:

NServiceBus 7.8.2
NServiceBus.AmazonSQS 5.6.3
NServiceBus.Newtonsoft.Json 2.4.0
NServiceBus.Persistence.Sql 6.6.2
NServiceBus.UniformSession 2.2.0

Should I instead use TypeNameHandling.Auto?

Hi Omair,

Quick follow-up question from my side. Does the endpoint that throws the exception have a handler that tries to subscribe to the base event, or does it only subscribe to ServiceA.Contract.Events.UserProfileUpdatedEvent?

Have you verified that the assembly versions of the contract assembly are matching, since the fully qualified assembly name is being used?


Yes, the endpoint throwing the exception subscribes to the concrete/derived event class ServiceA.Contract.Events.UserProfileUpdatedEvent.

And they’re both referencing the same .NET project/assembly. The services are in the same git (mono)repository.


Hi Daniel

I found the issue at my end. Upon further investigation, turns out the endpoint receiving the event had two instances running and one of them had the event assembly reference missing.

Thanks a lot.
Best regards,

1 Like

Hi Omair,

Thanks for following up, and I’m glad you found it!