Message buses and queues are a powerful tool for communicating between applications they are used extensively in microservices. But they can be useful in larger applications as well. In this article we’re going to look at how to implement azure service bus queues into our applications to both send and receive messages.
Before making our hands dirty let’s understand what is Azure service bus with real time example.
let’s pretend that our sender is a website that captures information about Employees so employee can come in here to log in and create account and it’s going to email you and say hey thanks for joining and some cool information about what we do well. Here I could put that <email code> in the same site that captures all of your information but then what if I have to take down the email service or change the email service ? I have to take the whole site down including the rest of the website that’s not ideal for micro services — the idea is we break out a piece of our application and make it an entire application that does one thing and so we can say well our microservice would be that email system where all it does is send an email.
It doesn’t know who or when you give us all the information in the input — it’d be a little service that sit out there and all it’s going to do is listen for new emails that need to be sent. When a person signs up then we’re going to send them an email so we’d have to some way to tell this micro service that go ahead and send an email but how do we do that without being directly connected to it ? I could add this as a reference and then trigger some kind of method but that is not the right way to do it and again if I want to update this micro service, then we have to hit the website down, because it’s tied together. Now how can we solve this problem ? and the answer is message buses or queues. And the idea is sender is going to send a message and it’s going to a queue and receiver – its job is to look at that message queue and wait for new messages to come. So with a message bus you can listen for changes and the receiver is going to listen to the queue and say when something comes in I’m going to do work .
So now we understood how message bus helps in real time scenario. Now will do small hands-on to understand azure service bus bit more. Let’s get back to the business.
I am going to create a new .Net 5 blazer server project. This kicks off the queue that creates the queue message — the basic structure of our application is we’ll have a blazer server app where we have a web form you fill in the web form with some information you hit submit that’s going to send a message to the queue. we’re going to have a different application we’ll use a console app for the other application that listens for queue messages and then it will do something with those queue messages which is just display on the screen that’s all we’re going to do.
We will create a new blazer project and name it MessageSender
We will add new class library to the solution and name it Common and will add reference to MessageSender project
Let’s create Console app project and name it MessageReceiver.
So those are our three pieces we’re gonna have our Sender ,Common and Receiver.
Now in the sender right click on dependencies manage new get packages add microsoft azure service bus.
Let’s add a class file in Common project and call it Employee make it public employeeID, first name last name. Please refer side image for solution structure and dependencies.
In MessageSender project, let’s create folder and call this services and add a class and let’s call this class the QueueService please refer side image.
Add constructor with IConfiguration — we’ll call this config control to create and assign the fields of appsettings.json. Let’s create and implement SendMessageAsync
We’re going to serialize our service bus message. Serialization what it does is it turns your object into a json. what a serializer does is it takes our object and turns it into a string now we can take the same that string and then deserialize it back to an object kind of like you know dehydrating rehydrating our objects.
Let’s go and create azure service bus. if you’re not familiar with azure subscription, you can get azure for free azure.microsoft.com/account/free
You can get first 30 days hundred dollars in credit or something like that to try pretty much anything you want out but then you also have 12 months of free services and then you also have 25 plus things that are free forever.
Let’s create a resource and we’re going to search for service bus hit create. provide all details hit create or review and create as shown in below picture.please refer below images for references.
We’re ready it’s validated hit create and that’s going to create our service bus.
Then go to your resource group, in my case it is AzureBlogs and go inside your namespace (ShreeMediumBlog). Then access Shared access policies and copy from primary connection string.
Let’s go back to visual studio and paste that connection string in appsettings.json.
Let’s go back to Azure and create queues and call it employeequeue. Please refer below images for queue creation in azure.
Let’s go back to visual studio and create UI with some basic code to set basic fields.
We will run this application and add some items to the Queue.
In azure if we open up the employee queue we’ll see that we have one active message and you can see message without reading by clicking on peek.
Let’s try to understand, how we can consume these messages from the Azure service bus. So our messagereceiver going to do that job.
Let’s make Void main async by adding async keyword prior to that. Now will implement below code to consume messages.
We’re going to pass in our connection string and our queue name so this establishes the connection to our queue and we’re going to pass in a exception received handler object and then autocomplete equals false what autocomplete equals false — autocomplete equals false will not mark the message as complete so when it triggers and we’re not going to just say and it’s complete we’re going to wait until we read the message and as long as they successfully read. The message we’re going to say yes complete this message and that’s where that 30 second lock timer comes in where we have 30 seconds to do it before it gets unlocked and allowed to get pulled down by somebody else so that’s our options. And now we’re going to say queueclient.register messagehandler and let’s say process messages async that’s a method name and we’re gonna pass in our message handler options.
So what this is doing is it’s going to create a new queue client which talks to the message queue in the azure service bus and it’s going to register a message handler — a message handler is the method that’s going to accept messages. when messages come in it listens and decodes byte array and deserializes into Employee object. We will print message response in console window.
Note : will set startup projects as multiple as shown below :