The Structure of an Azure Service Bus message

There are obviously many different ways you could design the messages intended to be sent to / from your Azure Service Bus queue. So after some consideration I came up with the following design for a service bus message.

Hide Copy Code

    /// <summary>
/// POCO class used for message exchange with Azure Service Bus
/// </summary>
[DataContract]
[KnownType(typeof(MessageObjectEntity))]
public class MessageObjectEntity
{
[DataMember]
public string MessageType { get; set; }
[DataMember]
public object MessageContent { get; set; }
}

The MessageType property defines the type of the object that is contained within the message. MessageContent defines the actual object itself. This will be a serialised instance of the class which can then be deserialized when it is received by the receiving application. To allow different types of classes to be added to the message you need to add it as a KnownType().

For example to add instances of MyNewClass to your message you will need to add the following to your class declaration.

Hide Copy Code

[KnownType(typeof(MyNewClass))]

Here’s a function that uses the MessageObjectEntity class to send messages to the service bus.

Hide Copy Code

    /// <summary>
/// Creates a message for the service bus
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="message"></param>
/// <returns></returns>
private static MessageObjectEntity CreateMessageForServiceBus<T>(T message)
{
return new MessageObjectEntity
{
MessageType = message.GetType().AssemblyQualifiedName,
MessageContent = message
};
}

The method uses Generics enabling the function to work with any object type, therefore allowing our code to send messages of any type to the service bus. We have basically wrapped our message inside another class, and it is this class that we send / receive from the service bus. So as far as our service bus code is concerned, the messages are always of the same type i.e. MessageObjectEntity. It’s down to the receiving application to know what class is wrapped inside MessageObjectEntity so that it can deserialise it. And it knows what the type is as this is defined by the property MessageType from earlier.

And finally here’s the calling code that invokes our method and adds our message to the service bus.

Hide Copy Code

using Microsoft.ServiceBus.Messaging;   MessageObjectEntity messageToSend = CreateMessageForServiceBus(message);BrokeredMessage brokeredMessage = new BrokeredMessage(messageToSend);await this._client.SendAsync(brokeredMessage);

N.B. you will need to ensure you have downloaded the Nuget package for Azure Service Bus messaging before you can instantiate the BrokeredMessage class.

The instance property _client is an instance of the QueueClient class, which is the agent that communicates with your service bus queue.

Hide Copy Code

private QueueClient GetServiceBusClient(string connection, string queuename)
{
return this._client ??
(this._client = QueueClient.CreateFromConnectionString(connection, queuename, ReceiveMode.PeekLock));
}

Sending messages to your service bus is straight-forward. Of course, you can implement something completely different to what I am proposing here. This is the design I have come up with that meets our requirements and fits into our existing architecture.

I’ll discuss how I process messages on the Azure Service Bus in a future article.

A father, cyclist, vegetarian, atheist, geek and multiple award winning technical author. Loves real ale, fine wine and good music. All round decent chap.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store