Azure

Getting Began With Azure Service Bus Queues And ASP.NET Core – Half 1

On this article, we’re going to focus on Microsoft Azure Service Bus Queues. First, we are going to see what’s Azure Service Bus after which focus on extra about queues. We’re additionally going to construct a easy software to ship and obtain messages in queues utilizing ASP.NET Core. So let’s seize a cup of espresso and begin studying.

 

What’s Azure Service Bus?

 

Azure Service Bus is a message dealer service which is hosted on the Azure platform and it supplies performance to publish messages to numerous functions and likewise decouple the functions.

 

 

Microsoft Azure Service Bus is a totally managed enterprise integration message dealer. Service Bus can decouple functions and providers. Service Bus affords a dependable and safe platform for asynchronous switch of information and state.

 

Information is transferred between completely different functions and providers utilizing messages. A message is in binary format and may comprise JSON, XML, or simply textual content. 

 

Microsoft service bus is available in completely different flavors,

  • Queues
  • Subject (we are going to cowl this in subsequent articles) 

Azure Service Bus: Queues

 

Queues observe a First-In-First-Out (FIFO) sample. Queues present the one-way transport just like the sender goes to ship message within the queue and the receiver would gather messages from 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 completes the messages.

 

 

The queue comprises a secondary sub-queue, known as a dead-letter queue (DLQ). Each time we create a queue DLQ is routinely added in our important queue. When the messages are usually not delivered to the receiver or can’t be processed by the receiver then such messages are pushed to DLQ.

 

Now we’ve mentioned sufficient concerning Queues so let’s create queues in Azure and construct a easy software to ship and obtain messages from the queue.

 

Making a easy software to ship and obtain messages from the queue.

 

Conditions

Overview of the appliance

 

We might be making a easy software which consists of three elements,

  • Create Azure Service Bus Queue utilizing Azure Portal (Coated on this article)
  • Create Internet API to push the message into Queue (Coated on this article)
  • Create a Background Service to obtain a message from Queue (Protecting in Half 2) 
Getting Started With Azure Service Bus Queues And ASP.NET Core

 

Creating Azure Service Bus Queue utilizing Azure Portal

  • Login to Azure and click on on Create a useful resource button. 
  • Within the search field sort service bus and choose it.
Getting Started With Azure Service Bus Queues And ASP.NET Core

  • Click on on Create button. You will notice the Create Namespace web page. 
Getting Started With Azure Service Bus Queues And ASP.NET Core

  • Azure has Useful resource Teams (RG) which acts as a container on your sources. So now we’re going to create a Service bus useful resource. First we have to create Useful resource Group. When you’ve got already created RG then you should use the identical right here. Below Useful resource group click on on Create New button and provides a singular RG identify. 
Getting Started With Azure Service Bus Queues And ASP.NET Core

  • Now we’ve to specify the Namespace identify. A namespace is a container for all messaging parts. A number of queues and subjects might be in a single namespace, and namespaces usually function software containers.
  • Choose the placement.
  • Choose pricing tier. Azure supplies Three pricing tiers,
Getting Started With Azure Service Bus Queues And ASP.NET Core

  • We have now stuffed in all particulars so click on on Assessment + create button.
Getting Started With Azure Service Bus Queues And ASP.NET Core

  • Assessment every little thing is added property and at last click on on Create button. 
Getting Started With Azure Service Bus Queues And ASP.NET Core

  • Creating sources will take time. 
Getting Started With Azure Service Bus Queues And ASP.NET Core

  • Now our order queue is created efficiently.
Getting Started With Azure Service Bus Queues And ASP.NET Core

  • Go to the Queues part within the left panel and click on on Queue and provides a singular identify for queue and click on on the Create button. 
Getting Started With Azure Service Bus Queues And ASP.NET Core

 

That is it. We have now created our first queue.

 

Create Internet API to push the message into Queue

 

Conditions

  • Visual Studio 19(in case you are utilizing .NET Core 3.1)
  • .NET Core 3.1 SDK put in 

Very first thing is to create a brand new ASP.NET Core Internet API challenge. For many who are new to ASP.NET Core, I’ve listed down the steps to create a brand new Internet API challenge.

  • Open Visual Studio and click on on File -> New -> Venture. Then choose ASP.NET Core Internet Utility and click on on the Subsequent button. 
  • Give the challenge identify and click on on Create button.
  • After that choose API and click on on Create button.
  • So now your ASP.NET Core Internet API challenge is setup.
  • First we have to set up Azure Service Bus NuGet bundle:
Getting Started With Azure Service Bus Queues And ASP.NET Core

 

Create a brand new class known as Order.cs and add beneath properties,

  1. namespace Order.Internet.API  
  2. {  
  3.     public class Order  
  4.     {  
  5.         public int Id { getset; }  
  6.         public int Amount { getset; }  
  7.         public decimal Worth { getset; }  
  8.     }  
  9. }  
To work together with Azure Service Bus Microsoft.Azure.ServiceBus bundle supplies one thing known as QueueClient which accepts queue connection string and queue identify as enter and returns the QueueClient object. First, we seize the connection string of Queue from Azure Portal. So open the queue and click on on Shared entry insurance policies then choose RootManageSharedAccessKey and replica it into appsettings.json file. 
Getting Started With Azure Service Bus Queues And ASP.NET Core

 

appsettings.json

  1. {  
  2.   “QueueConnectionString”“<substitute your RootManageSharedAccessKey right here>”,  
  3.   “QueueName”:  “order-queue”,  
  4.   “Logging”: {  
  5.     “LogLevel”: {  
  6.       “Default”“Data”,  
  7.       “Microsoft”“Warning”,  
  8.       “Microsoft.Internet hosting.Lifetime”“Data”  
  9.     }  
  10.   },  
  11.   “AllowedHosts”“*”  
  12. }  
 Create a brand new controller known as OrdersController and add beneath code to push message into queue,
  1. utilizing System;  
  2. utilizing System.Textual content;  
  3. utilizing System.Threading.Duties;  
  4. utilizing Microsoft.AspNetCore.Mvc;  
  5. utilizing Microsoft.Azure.ServiceBus;  
  6. utilizing Microsoft.Extensions.Configuration;  
  7. utilizing Newtonsoft.Json;  
  8.   
  9. namespace Order.Internet.Api.Controllers  
  10. {  
  11.     [Route(“api/[controller]”)]  
  12.     [ApiController]  
  13.     public class OrdersController : ControllerBase  
  14.     {  
  15.         personal readonly IConfiguration _configuration;  
  16.   
  17.         public OrdersController(IConfiguration configuration)  
  18.         {  
  19.             _configuration = configuration;  
  20.         }  
  21.   
  22.         [HttpPost]  
  23.         public async Job<IActionResult> CreateOrderAsync([FromBody] Order order)  
  24.         {  
  25.             IQueueClient queueClient = new QueueClient(_configuration[“QueueConnectionString”], _configuration[“QueueName”]);  
  26.             var orderJSON = JsonConvert.SerializeObject(order);  
  27.             var orderMessage = new Message(Encoding.UTF8.GetBytes(orderJSON))  
  28.             {  
  29.                 MessageId = Guid.NewGuid().ToString(),  
  30.                 ContentType = “software/json”  
  31.             };  
  32.             await queueClient.SendAsync(orderMessage).ConfigureAwait(false);  
  33.   
  34.             return Okay(“Create order message has been efficiently pushed to queue”);  
  35.         }  
  36.     }  
  37. }  
  • So we’ve created QueueClient object after which we created Message. We used SendAsync() methodology to push message to the queue.
  • We’re testing this utilizing postman. Run the app and hit submit API to push order into Queue. 
Getting Started With Azure Service Bus Queues And ASP.NET Core
  • After a profitable POST name let’s go to Azure Portal and see if the message is pushed to the queue. So we’ve 1 energetic message within the queue. 
Getting Started With Azure Service Bus Queues And ASP.NET Core

 

Conclusion

 

On this Half 1 of the Azure Service Bus Queue collection, we’ve discovered the best way to create a queue via Azure Portal and likewise we’ve created a Internet API that pushes a message to the Queue. In Half 2 we are going to create a Background Service which is able to learn messages from the Queue.

 

I actually hope that you just loved this text, share it with mates and please don’t hesitate to ship me your ideas or feedback.

 

Show More

Related Articles

Leave a Reply

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

Back to top button