This article covers implementing publishing and reading in the Azure Service Bus queue using both .Net Core and Python.
Azure is primarily a .NETCore shop but it does support other famous languages as well and one of them is Python. During a recent implementation of queues, I came across a thought of implementing service bus queues in python and honestly, it is simpler compared to .NET Core.
I will be using Visual Studio Code for implementation and the Python version is 3.12.3. Make sure that you have an Azure Service Bus resource ready with a queue in it
Let's start with .NET Core implementation first.
.NET CORE
Message Sender
For sending a message I have created a sample web API project. Just open the terminal, go to the folder where you want to add the solution and run the below command to create a web API project.
This creates a web api solution.
Now open the solution in VSCode and install the below NugetPackage using the terminal itself.
Once this is done add a new model class to your solution. This will be the structure of the message that we will be sending.
Now, add a new Controller with endpoints to send the message to the queue.
Now, I do not suggest putting connection string and queue name in code, it should rather come from app settings, but that is beyond the scope of this article.
Run the solution and call this endpoint with the body to create a message.
![Endpoint]()
![Active message]()
Message Receiver
We will be using a console application to receive these messages.
In order to create a console application using the terminal, run the below command.
This creates a console application named AzureServiceBusReceiver.
Now open the solution in VSCode and install the below NugetPackage using the terminal itself.
Once, this is done, add the below code to the Program. cs.
Once you run this console application, it will read any existing messages in the queue. If you run both the above solutions together, you can send messages from web API solution and it will be received by the console application
![Console Application]()
Python
Now, let's try to achieve the same using Python.
Create a folder for Python files and install the below package. This package is used to write and read messages to the Azure service bus queue.
Create a file for pushing messages to the service bus queue and use the code below.
This code is quite straightforward. It creates a client using a connection string and then creates an object od the queue and then pushes the message to the queue. Asyncio is used to run the method asynchronously.
Now, create another file to read the messages from the queue.
This method is similar to the previous one, the only difference is that we are reading the message in this one. This is also making sure to delete the message from the queue by marking it as deleted.
Now, let's run both files together using the below code.
We get the below result.
![Result]()
As you can see, the message is sent by the publisher and then read by the receiver.
Hope this article helps you in setting up basic Azure service queue integration with .netcore and Python.