Quantcast
Channel: CodeProject Latest postings for Dominic Burford
Viewing all articles
Browse latest Browse all 280

The Structure of an Azure Service Bus message

$
0
0
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.
///<summary>/// POCO class used for message exchange with the Azure Service Bus///</summary>    [DataContract]
    [KnownType(typeof(MessageObjectEntity))]publicclass MessageObjectEntity
    {
        [DataMember]publicstring MessageType { get; set; }
        [DataMember]publicobject 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.
[KnownType(typeof(MyNewClass))]
Here's a function that uses the MessageObjectEntity class to send messages to the service bus.
///<summary>/// Creates a message for the service bus///</summary>///<typeparamname="T"></typeparam>///<paramname="message"></param>///<returns></returns>privatestatic MessageObjectEntity CreateMessageForServiceBus<T>(T message)
    {returnnew 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.
using Microsoft.ServiceBus.Messaging;

    MessageObjectEntity messageToSend = CreateMessageForServiceBus(message);
    BrokeredMessage brokeredMessage = new BrokeredMessage(messageToSend);awaitthis._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.
private QueueClient GetServiceBusClient(string connection, string queuename)
    {returnthis._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.
"There are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies, and the other way is to make it so complicated that there are no obvious deficiencies. The first method is far more difficult." - C.A.R. Hoare

Home | LinkedIn | Google+ | Twitter

Viewing all articles
Browse latest Browse all 280

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>