.NET Core Azure Service Bus Send and Receive Message in Session Enabled Subscription

Samuel Hutama
Oct 20, 2018 · 3 min read

In this post, I will show you the steps to send and receive message in session enabled subscription.


The first step to be able to use Microsoft Azure Service Bus is to have an Azure account. You can sign up in their official website for free (12 months of free services).

The next step is to create a Service Bus namespace in your Azure portal. Microsoft already covers this in their documentation. It also covers the code to send and receive message to/from Azure Service Bus.

Image for post
Image for post
Azure Service Bus Icon.

Session Enabled Subscription — Receiver Code

But if you set the “Enable sessions” to be true when creating your Service Bus subscription:

Subscription with session enabled.

You will encounter this error when receiving the message you have sent to Service Bus:

Image for post
Image for post
“It is not possible for an entity that requires sessions to create a non-sessionful message receiver”.

There are 2 parts of the code in our receiver code that cause this error.

  1. The first one is because we use the RegisterMessageHandler() method. This method can only be used to listen to a non-sessionful subscription. We need to use the RegisterSessionHandler() method instead.
  2. The second one is because we use CompleteAsync() method. This also can only be used for a non-sessionful subscription.

The fix for these two parts looks like this:

We remove the CompleteAsync() method call, therefore we set AutoComplete property to be true.

Session Enabled Subscription — Sender Code

After applying the previous fix, the error will now gone. But, you won’t get any message even when you have sent a new message to your Service Bus subscription:

Image for post
Image for post

For a session enabled subscription, the message need to have a SessionId. Our sender code has not set this SessionId in the messages it sends. Adding this SessionId is quite simple:

//// Create a new message to send to the topic.
string messageBody = $"Message {i}";
var message = new Message(Encoding.UTF8.GetBytes(messageBody));//// Set the SessionId with a random Guid.
message.SessionId = Guid.NewGuid().ToString();
//// Write the body of the message to the console.
Console.WriteLine($"Sending message: {messageBody}");
//// Send the message to the topic.
await topicClient.SendAsync(message);

Now that our messages have SessionId, our listener should be able to receive the messages we have sent:

Image for post
Image for post

Voila! Now we are able to send and receive messages to/from our Azure Service Bus in session enabled subscription.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch

Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore

Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

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