mattcole
mattcole

Reputation: 1229

Sending interfaces as message in NServiceBus with the Binary Serializer

I recently moved to using the binary serializer to send messages with NServiceBus. My messages are all defined as interfaces and are instantiated using

bus.Send<MessageType>(msg => msg.Property = someValue)

This leads to an exception being thrown from NServiceBus stating that

Cannot create an instance of an interface

I can see from the stack trace that the SimpleMessageMapper is being used, and after looking in the source can see it's making a call to Activator.CreateInstance.

I can't find anything in the documentation stating that it's not possible to do what I'm trying to do, is there a way to fix this?

Thanks, Matt

Upvotes: 1

Views: 1014

Answers (1)

Mike Strobel
Mike Strobel

Reputation: 25623

Are you defining the implementation classes for your message interfaces, or is nServiceBus generating classes on its own? If the former, make sure you still have a default constructor and that the class and all fields/events are marked as [Serializable] or [NonSerialized]. If the latter, it's possible that nServiceBus doesn't know how to generate members which may be needed for (de)serialization. You may have to write and map the implementation class yourself.

Upvotes: 0

Related Questions