anon
anon

Reputation:

Can a NServiceBus message have an anonymous type property?

I have a Payload property of type object.
I fill Payload with an anonymous type, and send it off, I then get this ugly error:

2014-03-12 15:50:25,649 [7] 
ERROR NServiceBus.Unicast.Transport.TransportReceiver [(null)] <(null)> - 
Failed to serialize message with ID: fc8d44c1-3750-4658-ba91-a2ec010507aa`

System.Runtime.Serialization.SerializationException: 
An error occurred while attempting to extract logical messages from 
transport message NServiceBus.TransportMessage ---> 
Newtonsoft.Json.JsonSerializationException: 

Error resolving type specified in 
JSON '<>f__AnonymousType2`2[[System.String, mscorlib],[System.Int32,mscorlib]], MyProject'. 
Path '[0].Payload.$type', line 1, position 366. ---> Newtonsoft.Json.JsonSerializationException: Could not load assembly 'MyProject'.

Upvotes: 0

Views: 822

Answers (1)

Charles
Charles

Reputation: 3774

I ran into this problem - the issue is with json.net not really NServiceBus.
Json.Net is requiring being able to deserialize the type into a strict object, which it just can't.

There are a couple ways you can do this, you can change your payload object into an interface which will force NServiceBus to use TypeNameHandling.None which in turn will cause Json.Net to deserialize even if doesn't know the type.

Or you can write your own serializer, probably based heavily off their Json.Net one but with TypeNameHandling.None always on.

I have not found a reason to keep TypeNameHandling on, I think maybe the particular team made a decision to do it that way.

Upvotes: 2

Related Questions