deserializationazure-service-fabricazure-servicebus-queuesbrokeredmessage

Azure Service Bus Brokered Message Deserialization


I am sending messages to ASB using WCF NetMessaging. The message can contain any number of custom data contracts.

I have a Service Fabric stateless service with a custom listener for ASB delivering messages pushed onto the queue. All examples I’ve seen are able to only handle a single type of message (seems most guidance is to serializable to JSON but that’s not what I need to do here). I want the subscriber to the queue be able to handle a number of messages (any message sent to any action of the service).

I am able to add the Action to the BrokeredMessage.Properties so I know where to send it. The problem is I haven’t figured out how deserialize the message body in any way that works.

I can read it from a stream and get it to a string, but can’t do this: var myDTO = message.GetBody(); That throws serialization exceptions. I’ve also tried a variant of that passing in a DataContractSerializer - even though I think that is the default.

Furthermore, what I really need is a way to do this without knowing the type of data in the body - I could, conceivably, add more message.Properties for the types serialized in the body but I figure there has to be a direct way to do it using only the data in the body - after all WCF and similar techs do this with ease. But how?

Thanks for any help, Will


Solution

  • To have a stand-alone message body:

    Create an envelope type that describes the content (Type name, sender, timestamp, etc.), and holds a payload (string) property to contain the serialized object. To send out messages, you serialize (compress, encrypt) the object, assign the result to the payload property of an Envelope instance. Serialize the Envelope and send that out. To receive messages, deserialize the message body into an Envelope, examine the type information, and deserialzie the payload.

    This is more or less how SOAP based WCF services do/did it.

    Make sure your DTO is datacontract-serializable, by creating some unit tests. Keep in mind that the message body size is limited in ASB, XML may not be your best choice of serialization.

    You may also be hitting this issue.