Skip to main content

Service Bus Queues from Windows Azure - How to separate message or specify a consumer what types of message to consume

I continue the series of posts about Service Bus Queues from Windows Azure with a new post about splitter and aggregator on this type of queue.
We should see in what cases we need something like this. One possible case is when we want to send large messages using Service Bus Queue. When we need to do something like this we should use a table or blob and only put a unique id on the queue. But let assume that this is not possible. The legal department doesn’t permit us to save the content of the message in another location. We can use only the Service Bus Queue from Windows Azure. In this case we need to split the content in more than one messages. If we do this, we will need a method to recombine the content. We don’t each consumer to receive a different part of the content. All the content should go only to one consumer.
Another case is when we want to be able to configure what kind of messages a consumer can handle. This can be done very easily with Service Bus Topics, but we have a solution using Service Bus Queues. The only limitation in this case is the number of types of messages a consumer can handle – only one. But we can filter the messages in a way that only one type of messages can be consumed by the Service Bus Queues.
The solution for both cases is the SessionId property of the BrokeredMessage. This is a string property that can be set to each BrokeredMessage. Using this property we can group messages based on a session id. I don’t imagine this as a session id, but like a grouped id. To this string we can set any kind of value, from a GUID to a date time or a group name.
Basically when we define the producer – the splitter – the only thing that we need to do different is to set the SessionId to a value.
QueueClient qc =
    myFooConnectionString, "FooQueue");

BrokeredMessage message = new BrokeredMessage();
message.SessionId = Guid.NewGuid().ToString();
In this example we set the session id property and after that we send the message to the queue. If we want the value of the SessionId can be a constant that can define the type of message. In the following example I split a stream in such way that we create n message that contain all the content of the stream even if the content would not fit in only one message.
Stream messageStream = message.GetBody<Stream>();
for (int offset = 0;
    offset < length;
    offset += 255)
            long messageLength = (length - offset) > 255
                ? 255
        : length - offset;
            byte[] currentMessageContent = new byte[messageLength];
            int result = messageStream.Read(currentMessageContent, 0, (int)messageLength);
            BrokeredMessage currentMessage = new BrokeredMessage(
        new MemoryStream(currentMessageContent),
            subMessage.SessionId = currentContentId;
The BrokeredMessage constructor accepts a stream as parameter. The second parameter specified in constructor is used to tell the framework to not close the stream after the message is send.
The big difference appear on the consumer side – aggregator. When we start to consume messages from a session we can receive only messages from that session. The QueueClient class contain a method named AcceptMessageSession(). This will return a MessageSession that will be used to receive message for the given session. AcceptMessageSession() permit us to specify the session id of to get the first session that is available.
In the following example we will see how we can consume only messages of a given type.
MessageSession messageSession = qc.AcceptMessageSession(mySessionId);
    BrokeredMessage message = messageSession.Receive();
To be able to obtain the original stream we need to copy the content of each message to only one stream and in the end we will have our original stream – Service Bus Queue guarantees the order of the message will be the same as we send it (exception when a consumer don’t process a message as expected and the Complete() method is not called).
MemoryStream finalStream = new MemoryStream();
MessageSession messageSession = qc.AcceptMessageSession(mySessionId);
    BrokeredMessage message = messageSession.Receive(TimeSpan.FromSeconds(20));
    if(message != null)

A consumer can consume more than one session id, but each on a different thread. On the same thread and in the same time this is not possible.
We saw how we can split the content in more than one BrokeredMessage and recreate the whole message on the other side using Service Bus Queues. In the next post about Service Bus Queues will talk about the limitation of this type of queue in comparison with Windows Azure Queues.


Popular posts from this blog

ADO.NET provider with invariant name 'System.Data.SqlClient' could not be loaded

Today blog post will be started with the following error when running DB tests on the CI machine:
threw exception: System.InvalidOperationException: The Entity Framework provider type 'System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer' registered in the application config file for the ADO.NET provider with invariant name 'System.Data.SqlClient' could not be loaded. Make sure that the assembly-qualified name is used and that the assembly is available to the running application. See for more information. at System.Data.Entity.Infrastructure.DependencyResolution.ProviderServicesFactory.GetInstance(String providerTypeName, String providerInvariantName) This error happened only on the Continuous Integration machine. On the devs machines, everything has fine. The classic problem – on my machine it’s working. The CI has the following configuration:

TeamCity.NET 4.51EF 6.0.2VS2013
It seems that there …

How to check in AngularJS if a service was register or not

There are cases when you need to check in a service or a controller was register in AngularJS.
For example a valid use case is when you have the same implementation running on multiple application. In this case, you may want to intercept the HTTP provider and add a custom step there. This step don’t needs to run on all the application, only in the one where the service exist and register.
A solution for this case would be to have a flag in the configuration that specify this. In the core you would have an IF that would check the value of this flag.
Another solution is to check if a specific service was register in AngularJS or not. If the service was register that you would execute your own logic.
To check if a service was register or not in AngularJS container you need to call the ‘has’ method of ‘inhector’. It will return TRUE if the service was register.
if ($injector.has('httpInterceptorService')) { $httpProvider.interceptors.push('httpInterceptorService&#…

Fundamental Books of a Software Engineer (version 2018)

More then six years ago I wrote a blog post about fundamental books that any software engineer (developer) should read. Now it is an excellent time to update this list with new entries.

There are 5 different categories of books, that represent the recommended path. For example, you start with Coding books, after that, you read books about Programming, Design and so on.
There are some books about C++ that I recommend not because you shall know C++, only because the concepts that you can learn from it.


Writing solid codeCode completeProgramming Pearls, more programming pearls(recommended)[NEW] Introduction to Algorithms


Refactoring (M. Fowler)Pragmatic ProgrammerClean code[NEW] Software Engineering: A Practitioner's Approach[NEW] The Mythical Man-Month[NEW] The Art of Computer Programming


Applying UML and Patterns (GRASP patterns)C++ coding standards (Sutter, Alexandrescu)The C++ programming language (Stroustrup, Part IV)Object-oriented programming (Peter Coad)P…