Azure

How To Ship And Learn Messages From Azure Service Bus Queues Utilizing Azure Capabilities

Introduction

 

On this article, we’re going to talk about about Service Bus Queue Set off in Azure Capabilities. We’re additionally going to create a easy Azure operate to ship and skim messages from Service bus queues. So we’re going to create a HTTP set off operate to ship messages to the queue and a Service bus queue set off operate to learn messages from the queue.

 

If you’re new to Azure Capabilities then try the under articles,

What’s Azure Service Bus?

 

Azure Service Bus is a message dealer service that’s hosted on the Azure platform and it offers performance to publish messages to numerous purposes and in addition decouple the purposes. Azure service bus has two totally different flavors; i.e. Queues and Matters. Azure Service Bus Queues follows a First-In-First-Out (FIFO) sample. Queues present one-way transport just like the sender goes to ship messages within the queue and the receiver would acquire messages from the queue. In queues, there’s a 1:1 relationship between the sender and receiver. Messages are current within the queue till the receiver processes and full the messages.

 

Creating easy Azure Capabilities to ship and skim messages from the queue

Stipulations 

Overview of utility

 

We’re making a easy Azure operate utility that consists of three elements,

  1. Create Azure Service Bus Queue utilizing Azure Portal
  2. Create HTTP set off Azure Operate to ship the message into Queue 
  3. Create a Service Bus Queue set off Azure operate to learn a message from Queue

 

Create Azure Service Bus Queue utilizing Azure Portal

 

Observe the under steps to creating a brand new Service Bus and Queue into Azure.

  • Login to Azure and click on on Create a useful resource button.
  • Within the search field sort service bus and choose it. 

    How To Send And Read Messages From Azure Service Bus Queues Using Azure Functions

  • Click on on Create button. You will note the Create Namespace web page.

    How To Send And Read Messages From Azure Service Bus Queues Using Azure Functions

    Useful resource Group are nothing however a container to carry your sources in Azure. Service Bus is likely one of the useful resource so in case you have already created Useful resource Group then choose that or if have not created it but the clicking on Create button and provides it a reputation. 

    Namespace holds the a number of queues or matters resides underneath Service Bus. Specify a novel identify for namespace.

  • Fill out all of the required particulars and click on on Overview + Create button

    How To Send And Read Messages From Azure Service Bus Queues Using Azure Functions

  • Overview that the whole lot is added correctly and at last click on on the Create button.

    How To Send And Read Messages From Azure Service Bus Queues Using Azure Functions

  • Creating sources will take time so look forward to a while to complete the deployment.

    How To Send And Read Messages From Azure Service Bus Queues Using Azure Functions

  • As soon as the deployment is accomplished you will notice the Go to useful resource button.

    How To Send And Read Messages From Azure Service Bus Queues Using Azure Functions

  • Click on on Go to useful resource button to see our Service Bus useful resource.

    How To Send And Read Messages From Azure Service Bus Queues Using Azure Functions

  • So we now have efficiently created a brand new Service Bus. Now subsequent step is to create a Queue. Go to the Queues part within the left panel and click on on Queue and provides a novel identify for queue and click on on the Create button.

    How To Send And Read Messages From Azure Service Bus Queues Using Azure Functions

  • That is it. We’ve got created our first queue.

    How To Send And Read Messages From Azure Service Bus Queues Using Azure Functions

Create HTTP set off Azure Operate to ship the message into Queue

 

Stipulations

  • Visual Studio 19 with Azure Growth workload put in 

So open Visual Studio and Go to File -> New -> Challenge. Search “Azure Capabilities” within the search field and choose the Azure operate template and click on on Subsequent.

 

How To Send And Read Messages From Azure Service Bus Queues Using Azure Functions

 

Give a reputation to the operate undertaking and click on on Create.

 

How To Send And Read Messages From Azure Service Bus Queues Using Azure Functions

 

Choose the HTTP set off template and set the Authorization degree as Nameless and click on on Create.

 

How To Send And Read Messages From Azure Service Bus Queues Using Azure Functions

 

That is it. We’ve got created our first Azure operate. By default the identify of the operate is Function1.cs so now change it to “SendMessage”.

How To Send And Read Messages From Azure Service Bus Queues Using Azure Functions

 

Azure Capabilities has Triggers and Bindings. Triggers are those on account of which operate code begin executing foreign exchange. Within the case of HTTP set off everytime you make HTTP request by hitting the operate URL the particular operate will begin its execution. The Azure operate should have just one set off. Bindings are the path of knowledge coming in or going out from the operate. We’ve got In, out, and each kinds of bindings.

 

So for our utility, we’re creating HTTP Set off operate which takes information and sends it into the Queue. So to ship information to the queue we’re utilizing an Output binding. To attach with the queue we’d like a connection string. So let’s open the Service bus useful resource in portal -> Shared Entry Coverage -> Copy the Main Connection String underneath RootManagedSharedAccessKey

 

How To Send And Read Messages From Azure Service Bus Queues Using Azure Functions

 

First, set up the under NuGet bundle,

 

How To Send And Read Messages From Azure Service Bus Queues Using Azure Functions

 

Open the native.settings.json file in our operate app and add a key referred to as “AzureWebJobsServiceBus” and paste the connection string of our Service bus useful resource.

  1. {  
  2.     “IsEncrypted”false,  
  3.   “Values”: {  
  4.     “AzureWebJobsStorage”“UseDevelopmentStorage=true”,  
  5.     “FUNCTIONS_WORKER_RUNTIME”“dotnet”,  
  6.     “AzureWebJobsServiceBus”<exchange your RootManageSharedAccessKey right here>”
  7.   }  
  8. }  

Now add the under code to ship a message to the queue utilizing output bindings.

  1. utilizing System.IO;  
  2. utilizing System.Textual content;  
  3. utilizing System.Threading.Duties;  
  4. utilizing Microsoft.AspNetCore.Http;  
  5. utilizing Microsoft.Azure.WebJobs;  
  6. utilizing Microsoft.Azure.WebJobs.Extensions.Http;  
  7. utilizing Microsoft.Azure.WebJobs.ServiceBus;  
  8. utilizing Microsoft.Extensions.Logging;  
  9.   
  10. namespace AzServiceBusDemo  
  11. {  
  12.     public static class SendMessage  
  13.     {  
  14.         [FunctionName(“SendMessage”)]  
  15.         [return: ServiceBus(“az-queue”, EntityType.Queue)]  
  16.         public static async Process<string> Run(  
  17.             [HttpTrigger(AuthorizationLevel.Anonymous, “post”, Route = null)] HttpRequest req,  
  18.             ILogger log)  
  19.         {  
  20.             log.LogInformation(“SendMessage operate requested”);  
  21.             string physique = string.Empty;  
  22.             utilizing (var reader = new StreamReader(req.Physique, Encoding.UTF8))  
  23.             {  
  24.                 physique = await reader.ReadToEndAsync();  
  25.                 log.LogInformation($“Message physique : {physique}”);  
  26.             }  
  27.             log.LogInformation($“SendMessage processed.”);  
  28.             return physique;  
  29.         }  
  30.     }  
  31. }  
  • From traces 21-28, we’re simply getting information from the request physique & assigned it to a neighborhood variable. After that, we’re simply returning it from operate. 
  • In line 15, since we’re utilizing C# for creating operate, we are able to use ServiceBusAttribute to specify the output bindings. For the attribute constructor i.e. ServiceBus() we simply have to cross queue identify and kind of entity to bind. 

Now run the SendMessage operate.

 

How To Send And Read Messages From Azure Service Bus Queues Using Azure Functions

 

For testing the operate open postman and hit the about operate URL. 

 

How To Send And Read Messages From Azure Service Bus Queues Using Azure Functions

 

Since we logged our request physique, we are able to see the content material.

 

How To Send And Read Messages From Azure Service Bus Queues Using Azure Functions

 

Open queue “az-queue” and we are able to see the Lively message rely is now exhibiting as 1. 

 

How To Send And Read Messages From Azure Service Bus Queues Using Azure Functions

 

To verify the message content material within the queue you possibly can click on on Service Bus Explorer(preview) within the left sidebar after which click on on Peek and at last click on on the message to see the content material.

 

How To Send And Read Messages From Azure Service Bus Queues Using Azure Functions

 

Create a Service Bus Queue set off Azure operate to learn a message from Queue

 

Now the ultimate step is to learn a message from the queue i.e. “az-queue”. Proper-click on the undertaking and click on on undertaking -> Click on on Add -> Choose New Azure Operate. Choose Azure operate and provides it a reputation and click on on Add button.

 

How To Send And Read Messages From Azure Service Bus Queues Using Azure Functions

 

Now choose Service Bus Queue Set off and specify queue identify and click on on Add button.

 

How To Send And Read Messages From Azure Service Bus Queues Using Azure Functions

 

That is it — our Service Bus set off operate is created.

  1. utilizing Microsoft.Azure.WebJobs;  
  2. utilizing Microsoft.Extensions.Logging;  
  3.   
  4. namespace AzServiceBusDemo  
  5. {  
  6.     public static class ReadMessageFromQueue  
  7.     {  
  8.         [FunctionName(“ReadMessageFromQueue”)]  
  9.         public static void Run([ServiceBusTrigger(“az-queue”)]string myQueueItem, ILogger log)  
  10.         {  
  11.             log.LogInformation($“C# ServiceBus queue set off operate processed message: {myQueueItem}”);  
  12.         }  
  13.     }  
  14. }  

Now run our operate app. Since we have already got one energetic message in our queue, as quickly because the operate begins it can learn a message from the queue as proven under,

 

How To Send And Read Messages From Azure Service Bus Queues Using Azure Functions

 

So on this means, we are able to use Azure features to ship and skim messages from Azure Service Bus Queues.

 

Conclusion

 

On this article, we now have created a brand new Azure Service Bus occasion and added a brand new queue from the portal. Additionally, we now have created the HTTP set off Azure operate to ship messages into the Queue and Service Bus Queue set off operate to learn messages from a queue. I actually hope that you just loved this text, share it with buddies and please don’t hesitate to ship me your ideas or feedback. 

 

Keep tuned for extra articles on Azure Capabilities.

 

 

Completely happy Coding!! 

Tags
Show More

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Close