{"id":1966,"date":"2023-07-17T10:58:27","date_gmt":"2023-07-17T10:58:27","guid":{"rendered":"http:\/\/waqar-arshad.com\/?p=1966"},"modified":"2023-07-17T10:58:27","modified_gmt":"2023-07-17T10:58:27","slug":"rabbitmq-message-queue-using-net-core-6-web-api","status":"publish","type":"post","link":"http:\/\/waqar-arshad.com\/index.php\/2023\/07\/17\/rabbitmq-message-queue-using-net-core-6-web-api\/","title":{"rendered":"RabbitMQ Message Queue Using .NET Core 6 Web API"},"content":{"rendered":"<div class=\"pld-like-dislike-wrap pld-template-1\">\r\n    <div class=\"pld-like-wrap  pld-common-wrap\">\r\n    <a href=\"javascript:void(0)\" class=\"pld-like-trigger pld-like-dislike-trigger  \" title=\"\" data-post-id=\"1966\" data-trigger-type=\"like\" data-restriction=\"cookie\" data-already-liked=\"0\">\r\n                        <i class=\"fas fa-thumbs-up\"><\/i>\r\n                <\/a>\r\n    <span class=\"pld-like-count-wrap pld-count-wrap\">    <\/span>\r\n<\/div><div class=\"pld-dislike-wrap  pld-common-wrap\">\r\n    <a href=\"javascript:void(0)\" class=\"pld-dislike-trigger pld-like-dislike-trigger  \" title=\"\" data-post-id=\"1966\" data-trigger-type=\"dislike\" data-restriction=\"cookie\" data-already-liked=\"0\">\r\n                        <i class=\"fas fa-thumbs-down\"><\/i>\r\n                <\/a>\r\n    <span class=\"pld-dislike-count-wrap pld-count-wrap\"><\/span>\r\n<\/div><\/div>\n<h1 class=\"wp-block-heading\"><strong>RabbitMQ Message Queue Using .NET Core 6 Web API<\/strong><\/h1>\n\n\n\n<p>In this article, we will&nbsp;discuss the RabbitMQ Message Queue and its implementation using .NET Core 6 API as Message Producer and Console Application as a Message Consumer.<\/p>\n\n\n\n<p><strong>Agenda:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Introduction of RabbitMQ<\/li>\n\n\n\n<li>Benefits of using RabbitMQ<\/li>\n\n\n\n<li>Implementation of RabbitMQ in .NET Core 6<\/li>\n<\/ul>\n\n\n\n<p><strong>Prerequisites:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Visual Studio 2022<\/li>\n\n\n\n<li>Docker Desktop<\/li>\n\n\n\n<li>.NET Core 6 SDK<\/li>\n<\/ul>\n\n\n\n<p>Introduction of RabbitMQ<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Rabbit MQ is the message broker that acts as a middleware while using multiple microservices.<\/li>\n\n\n\n<li>RabbitMQ is an open-source message broker software. It is sometimes also called message-oriented middleware.<\/li>\n\n\n\n<li>RabbitMQ is written in the Erlang programming language.<\/li>\n\n\n\n<li>RabbitMQ is used to reduce the load and delivery time of a web application when some of the resources have taken a lot of time to process the data.<\/li>\n<\/ul>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"940\" height=\"437\" src=\"http:\/\/waqar-arshad.com\/wp-content\/uploads\/2023\/07\/image-13.png\" alt=\"\" class=\"wp-image-1967\" srcset=\"http:\/\/waqar-arshad.com\/wp-content\/uploads\/2023\/07\/image-13.png 940w, http:\/\/waqar-arshad.com\/wp-content\/uploads\/2023\/07\/image-13-300x139.png 300w, http:\/\/waqar-arshad.com\/wp-content\/uploads\/2023\/07\/image-13-768x357.png 768w\" sizes=\"auto, (max-width: 940px) 100vw, 940px\" \/><\/figure>\n\n\n\n<ul class=\"wp-block-list\">\n<li>As you can see in the diagram above, there is one producer who sends a message to the RabbitMQ server. The server will store that message inside the queue in a FIFO manner.<\/li>\n\n\n\n<li>Once the producer has sent the message to the queue, there may be multiple consumers that want the message produced by the producer. In that case, consumers subscribe to the message and get that message from the Message Queue as you see in the above diagram.<\/li>\n\n\n\n<li>In this section, we will use one eCommerce Site as an example to understand more fully.<\/li>\n\n\n\n<li>There are multiple microservices running in the background while we are using the eCommerce website. There is one service that takes care of order details, and another service that takes care of payment details and receipts.<\/li>\n\n\n\n<li>Suppose we placed one order. At that time, the order service will start and process our order. After taking the order details, it will send data to the payment service, which takes the payment and sends the payment receipt to the end-users.<\/li>\n\n\n\n<li>In this case, there may be a chance of some technical issue occurring&nbsp;in the payment service. If the user did not receive the payment receipt due to this, the user will be impacted and connected with the support team ti try&nbsp;to learn the status of the order.<\/li>\n\n\n\n<li>There may be another scenario on the user(consumer) side. Perhaps due to some technical issue, the user is exited from the application when the payment is in process. But, he will not get any receipt details after payment is successfully processed from backend services.<\/li>\n\n\n\n<li>In these scenarios, the RabbitMQ plays an essential role to process messages in the message queue. So, when the consumer gets online, he will receive&nbsp;that order receipt message from the message queue, produced by the producer without impacting the web application.<\/li>\n\n\n\n<li>All these examples are just for understanding purpose.&nbsp;There are a lot of scenarios in which RabbitMQ may play&nbsp;an important role while&nbsp;using multiple microservices. Sometimes RabbitMQ is used fully to load balancing between multiple services, or for many other purposes.<\/li>\n<\/ul>\n\n\n\n<p>Benefits of using RabbitMQ<\/p>\n\n\n\n<p>There are many benefits to using a Message Broker to send data to the consumer. Below, we will discuss a few of these benefits.<\/p>\n\n\n\n<p><strong>High Availability<\/strong><\/p>\n\n\n\n<p>When multiple microservices are used by the application, if one of the microservices is stopped due to technical reasons at that time, the message will&nbsp;never be lost. Instead, it persists in the RabbitMQ server. After some time, when our service starts working, it will connect with RabbitMQ and take the pending message easily.<\/p>\n\n\n\n<p><strong>Scalability<\/strong><\/p>\n\n\n\n<p>When we use RabbitMQ,&nbsp;at that time our application does not depend on only one server and virtual machine to process a request.&nbsp;If&nbsp;our server is stopped at that&nbsp;time, RabbitMQ will transfer our application load to another server that has the same services running in the background.<\/p>\n\n\n\n<p>RabbitMQ Implementation with .NET Core 6<\/p>\n\n\n\n<p>Let\u2019s start with the practical implementation of RabbitMQ using .NET Core 6 Web API as producer and Console Application as a consumer.<\/p>\n\n\n\n<p><strong>Step 1<\/strong><\/p>\n\n\n\n<p>Create a .NET Core API project.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"940\" height=\"481\" src=\"http:\/\/waqar-arshad.com\/wp-content\/uploads\/2023\/07\/image-14.png\" alt=\"\" class=\"wp-image-1968\" srcset=\"http:\/\/waqar-arshad.com\/wp-content\/uploads\/2023\/07\/image-14.png 940w, http:\/\/waqar-arshad.com\/wp-content\/uploads\/2023\/07\/image-14-300x154.png 300w, http:\/\/waqar-arshad.com\/wp-content\/uploads\/2023\/07\/image-14-768x393.png 768w\" sizes=\"auto, (max-width: 940px) 100vw, 940px\" \/><\/figure>\n\n\n\n<p><strong>Step 2<\/strong><\/p>\n\n\n\n<p>Configure your project.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"940\" height=\"476\" src=\"http:\/\/waqar-arshad.com\/wp-content\/uploads\/2023\/07\/image-15.png\" alt=\"\" class=\"wp-image-1969\" srcset=\"http:\/\/waqar-arshad.com\/wp-content\/uploads\/2023\/07\/image-15.png 940w, http:\/\/waqar-arshad.com\/wp-content\/uploads\/2023\/07\/image-15-300x152.png 300w, http:\/\/waqar-arshad.com\/wp-content\/uploads\/2023\/07\/image-15-768x389.png 768w\" sizes=\"auto, (max-width: 940px) 100vw, 940px\" \/><\/figure>\n\n\n\n<p><strong>Step 3<\/strong><\/p>\n\n\n\n<p>Provide additional information about your project.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"940\" height=\"483\" src=\"http:\/\/waqar-arshad.com\/wp-content\/uploads\/2023\/07\/image-16.png\" alt=\"\" class=\"wp-image-1970\" srcset=\"http:\/\/waqar-arshad.com\/wp-content\/uploads\/2023\/07\/image-16.png 940w, http:\/\/waqar-arshad.com\/wp-content\/uploads\/2023\/07\/image-16-300x154.png 300w, http:\/\/waqar-arshad.com\/wp-content\/uploads\/2023\/07\/image-16-768x395.png 768w\" sizes=\"auto, (max-width: 940px) 100vw, 940px\" \/><\/figure>\n\n\n\n<p><strong>Step 4<\/strong><\/p>\n\n\n\n<p>Project Structure of Producer Application.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"456\" height=\"588\" src=\"http:\/\/waqar-arshad.com\/wp-content\/uploads\/2023\/07\/image-17.png\" alt=\"\" class=\"wp-image-1971\" srcset=\"http:\/\/waqar-arshad.com\/wp-content\/uploads\/2023\/07\/image-17.png 456w, http:\/\/waqar-arshad.com\/wp-content\/uploads\/2023\/07\/image-17-233x300.png 233w\" sizes=\"auto, (max-width: 456px) 100vw, 456px\" \/><\/figure>\n\n\n\n<p><strong>Step 5<\/strong><\/p>\n\n\n\n<p>Install a few NuGet Package.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"940\" height=\"647\" src=\"http:\/\/waqar-arshad.com\/wp-content\/uploads\/2023\/07\/image-18.png\" alt=\"\" class=\"wp-image-1972\" srcset=\"http:\/\/waqar-arshad.com\/wp-content\/uploads\/2023\/07\/image-18.png 940w, http:\/\/waqar-arshad.com\/wp-content\/uploads\/2023\/07\/image-18-300x206.png 300w, http:\/\/waqar-arshad.com\/wp-content\/uploads\/2023\/07\/image-18-768x529.png 768w\" sizes=\"auto, (max-width: 940px) 100vw, 940px\" \/><\/figure>\n\n\n\n<p><strong>Step 6<\/strong><\/p>\n\n\n\n<p>Create Product Class inside the Models folder.<\/p>\n\n\n\n<p>namespace RabitMqProductAPI.Models {<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp; public class Product {<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public int ProductId {<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; get;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; set;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public string ProductName {<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; get;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; set;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public string ProductDescription {<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; get;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; set;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public int ProductPrice {<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; get;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; set;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public int ProductStock {<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; get;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; set;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp; }<\/p>\n\n\n\n<p>}<\/p>\n\n\n\n<p>C#<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>Step 7<\/strong><\/p>\n\n\n\n<p>Next, create a DbContextClass.cs class inside the Data folder.<\/p>\n\n\n\n<p>using Microsoft.EntityFrameworkCore;<\/p>\n\n\n\n<p>using RabitMqProductAPI.Models;<\/p>\n\n\n\n<p>namespace RabitMqProductAPI.Data {<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp; public class DbContextClass: DbContext {<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; protected readonly IConfiguration Configuration;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public DbContextClass(IConfiguration configuration) {<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Configuration = configuration;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; protected override void OnConfiguring(DbContextOptionsBuilder options) {<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; options.UseSqlServer(Configuration.GetConnectionString(&#8220;DefaultConnection&#8221;));<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public DbSet &lt; Product &gt; Products {<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; get;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; set;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp; }<\/p>\n\n\n\n<p>}<\/p>\n\n\n\n<p>C#<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>Step 8<\/strong><\/p>\n\n\n\n<p>Later on, create an IProductService.cs and ProductService.cs class inside the Services folder.<\/p>\n\n\n\n<p>using RabitMqProductAPI.Models;<\/p>\n\n\n\n<p>namespace RabitMqProductAPI.Services {<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp; public interface IProductService {<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public IEnumerable &lt; Product &gt; GetProductList();<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public Product GetProductById(int id);<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public Product AddProduct(Product product);<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public Product UpdateProduct(Product product);<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public bool DeleteProduct(int Id);<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp; }<\/p>\n\n\n\n<p>}<\/p>\n\n\n\n<p>C#<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>Create a ProductService.cs.<\/p>\n\n\n\n<p>using RabitMqProductAPI.Data;<\/p>\n\n\n\n<p>using RabitMqProductAPI.Models;<\/p>\n\n\n\n<p>namespace RabitMqProductAPI.Services {<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp; public class ProductService: IProductService {<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; private readonly DbContextClass _dbContext;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public ProductService(DbContextClass dbContext) {<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; _dbContext = dbContext;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public IEnumerable &lt; Product &gt; GetProductList() {<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return _dbContext.Products.ToList();<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public Product GetProductById(int id) {<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return _dbContext.Products.Where(x =&gt; x.ProductId == id).FirstOrDefault();<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public Product AddProduct(Product product) {<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var result = _dbContext.Products.Add(product);<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; _dbContext.SaveChanges();<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return result.Entity;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public Product UpdateProduct(Product product) {<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var result = _dbContext.Products.Update(product);<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; _dbContext.SaveChanges();<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return result.Entity;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public bool DeleteProduct(int Id) {<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var filteredData = _dbContext.Products.Where(x =&gt; x.ProductId == Id).FirstOrDefault();<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var result = _dbContext.Remove(filteredData);<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; _dbContext.SaveChanges();<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return result != null ? true : false;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp; }<\/p>\n\n\n\n<p>}<\/p>\n\n\n\n<p>C#<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>Step 9<\/strong><\/p>\n\n\n\n<p>Create IRabitMQProducer.cs and RabitMQProducer.cs classes for the message queue inside the RabbitMQ folder.<\/p>\n\n\n\n<p>namespace RabitMqProductAPI.RabitMQ {<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp; public interface IRabitMQProducer {<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public void SendProductMessage &lt; T &gt; (T message);<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp; }<\/p>\n\n\n\n<p>}<\/p>\n\n\n\n<p>C#<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>Next, create a RabitMQProducer.cs class.<\/p>\n\n\n\n<p>using Newtonsoft.Json;<\/p>\n\n\n\n<p>using RabbitMQ.Client;<\/p>\n\n\n\n<p>using System.Text;<\/p>\n\n\n\n<p>namespace RabitMqProductAPI.RabitMQ {<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp; public class RabitMQProducer: IRabitMQProducer {<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public void SendProductMessage &lt; T &gt; (T message) {<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \/\/Here we specify the Rabbit MQ Server. we use rabbitmq docker image and use it<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var factory = new ConnectionFactory {<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; HostName = &#8220;localhost&#8221;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; };<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \/\/Create the RabbitMQ connection using connection factory details as i mentioned above<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var connection = factory.CreateConnection();<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \/\/Here we create channel with session and model<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; using<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var channel = connection.CreateModel();<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \/\/declare the queue after mentioning name and a few property related to that<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; channel.QueueDeclare(&#8220;product&#8221;, exclusive: false);<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \/\/Serialize the message<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var json = JsonConvert.SerializeObject(message);<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var body = Encoding.UTF8.GetBytes(json);<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \/\/put the data on to the product queue<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; channel.BasicPublish(exchange: &#8220;&#8221;, routingKey: &#8220;product&#8221;, body: body);<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp; }<\/p>\n\n\n\n<p>}<\/p>\n\n\n\n<p>C#<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>Step 10<\/strong><\/p>\n\n\n\n<p>After that, create a new ProductController.cs.<\/p>\n\n\n\n<p>using Microsoft.AspNetCore.Mvc;<\/p>\n\n\n\n<p>using RabitMqProductAPI.Models;<\/p>\n\n\n\n<p>using RabitMqProductAPI.RabitMQ;<\/p>\n\n\n\n<p>using RabitMqProductAPI.Services;<\/p>\n\n\n\n<p>namespace RabitMqProductAPI.Controllers {<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp; [Route(&#8220;api\/[controller]&#8221;)]<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp; [ApiController]<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp; public class ProductController: ControllerBase {<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; private readonly IProductService productService;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; private readonly IRabitMQProducer _rabitMQProducer;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public ProductController(IProductService _productService, IRabitMQProducer rabitMQProducer) {<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; productService = _productService;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; _rabitMQProducer = rabitMQProducer;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [HttpGet(&#8220;productlist&#8221;)]<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public IEnumerable &lt; Product &gt; ProductList() {<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var productList = productService.GetProductList();<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return productList;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [HttpGet(&#8220;getproductbyid&#8221;)]<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public Product GetProductById(int Id) {<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return productService.GetProductById(Id);<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [HttpPost(&#8220;addproduct&#8221;)]<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public Product AddProduct(Product product) {<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var productData = productService.AddProduct(product);<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \/\/send the inserted product data to the queue and consumer will listening this data from queue<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; _rabitMQProducer.SendProductMessage(productData);<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return productData;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [HttpPut(&#8220;updateproduct&#8221;)]<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public Product UpdateProduct(Product product) {<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return productService.UpdateProduct(product);<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [HttpDelete(&#8220;deleteproduct&#8221;)]<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public bool DeleteProduct(int Id) {<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return productService.DeleteProduct(Id);<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp; }<\/p>\n\n\n\n<p>}<\/p>\n\n\n\n<p>C#<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>Here, you can see that we inject the IRabitMQProducer service inside the constructor and use it in the add product API endpoint to send data into the message queue. This&nbsp;inserts product details inside the RabbitMQ queue, and later the consumer will get that data. This is the continuously listening queue.<\/p>\n\n\n\n<p><strong>Step 11<\/strong><\/p>\n\n\n\n<p>Add the connection string inside the appsetting.json file.<\/p>\n\n\n\n<p>{<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp; &#8220;Logging&#8221;: {<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8220;LogLevel&#8221;: {<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8220;Default&#8221;: &#8220;Information&#8221;,<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8220;Microsoft.AspNetCore&#8221;: &#8220;Warning&#8221;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp; },<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp; &#8220;AllowedHosts&#8221;: &#8220;*&#8221;,<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp; &#8220;ConnectionStrings&#8221;: {<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8220;DefaultConnection&#8221;: &#8220;Data Source=DESKTOP-***;Initial Catalog=RabitMQDemo;User Id=**;Password=***@1;&#8221;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp; }<\/p>\n\n\n\n<p>}<\/p>\n\n\n\n<p>BASIC<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>Step 12<\/strong><\/p>\n\n\n\n<p>Next, register a few services inside the Program.cs class.<\/p>\n\n\n\n<p>using RabitMqProductAPI.Data;<\/p>\n\n\n\n<p>using RabitMqProductAPI.RabitMQ;<\/p>\n\n\n\n<p>using RabitMqProductAPI.Services;<\/p>\n\n\n\n<p>var builder = WebApplication.CreateBuilder(args);<\/p>\n\n\n\n<p>\/\/ Add services to the container.<\/p>\n\n\n\n<p>builder.Services.AddScoped &lt; IProductService, ProductService &gt; ();<\/p>\n\n\n\n<p>builder.Services.AddDbContext &lt; DbContextClass &gt; ();<\/p>\n\n\n\n<p>builder.Services.AddScoped &lt; IRabitMQProducer, RabitMQProducer &gt; ();<\/p>\n\n\n\n<p>builder.Services.AddControllers();<\/p>\n\n\n\n<p>\/\/ Learn more about configuring Swagger\/OpenAPI at <a href=\"https:\/\/aka.ms\/aspnetcore\/swashbuckle\">https:\/\/aka.ms\/aspnetcore\/swashbuckle<\/a><\/p>\n\n\n\n<p>builder.Services.AddEndpointsApiExplorer();<\/p>\n\n\n\n<p>builder.Services.AddSwaggerGen();<\/p>\n\n\n\n<p>var app = builder.Build();<\/p>\n\n\n\n<p>\/\/ Configure the HTTP request pipeline.<\/p>\n\n\n\n<p>if (app.Environment.IsDevelopment()) {<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp; app.UseSwagger();<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp; app.UseSwaggerUI();<\/p>\n\n\n\n<p>}<\/p>\n\n\n\n<p>app.UseHttpsRedirection();<\/p>\n\n\n\n<p>app.UseAuthorization();<\/p>\n\n\n\n<p>app.MapControllers();<\/p>\n\n\n\n<p>app.Run();<\/p>\n\n\n\n<p>C#<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>Step 13<\/strong><\/p>\n\n\n\n<p>Add migration and update using the following entity framework command after executing that into the package manager console under the main project.<\/p>\n\n\n\n<p>add-migration \u201cfirst\u201d<\/p>\n\n\n\n<p>update-database<\/p>\n\n\n\n<p><strong>Step 14<\/strong><\/p>\n\n\n\n<p>Install Rabbitmq docker file using the following command (Note- docker desktop is in running mode):<\/p>\n\n\n\n<p>docker pull rabbitmq:3-management<\/p>\n\n\n\n<p>Next, create a container and start using the Rabbitmq Dockerfile that we downloaded:<\/p>\n\n\n\n<p>docker run &#8211;rm -it -p 15672:15672 -p 5672:5672 rabbitmq:3-management<\/p>\n\n\n\n<p><strong>Step 15<\/strong><\/p>\n\n\n\n<p>Finally, run your application and you will see the swagger UI and API endpoints.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"940\" height=\"468\" src=\"http:\/\/waqar-arshad.com\/wp-content\/uploads\/2023\/07\/image-19.png\" alt=\"\" class=\"wp-image-1973\" srcset=\"http:\/\/waqar-arshad.com\/wp-content\/uploads\/2023\/07\/image-19.png 940w, http:\/\/waqar-arshad.com\/wp-content\/uploads\/2023\/07\/image-19-300x149.png 300w, http:\/\/waqar-arshad.com\/wp-content\/uploads\/2023\/07\/image-19-768x382.png 768w\" sizes=\"auto, (max-width: 940px) 100vw, 940px\" \/><\/figure>\n\n\n\n<p>This is all about Product Web API as producer. Now, let&#8217;s create a new console application as the consumer consumes the messag&nbsp;sent by the producer.<\/p>\n\n\n\n<p><strong>Step 1<\/strong><\/p>\n\n\n\n<p>Add a new console application inside the same solution.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"940\" height=\"479\" src=\"http:\/\/waqar-arshad.com\/wp-content\/uploads\/2023\/07\/image-20.png\" alt=\"\" class=\"wp-image-1974\" srcset=\"http:\/\/waqar-arshad.com\/wp-content\/uploads\/2023\/07\/image-20.png 940w, http:\/\/waqar-arshad.com\/wp-content\/uploads\/2023\/07\/image-20-300x153.png 300w, http:\/\/waqar-arshad.com\/wp-content\/uploads\/2023\/07\/image-20-768x391.png 768w\" sizes=\"auto, (max-width: 940px) 100vw, 940px\" \/><\/figure>\n\n\n\n<p><strong>Step 2<\/strong><\/p>\n\n\n\n<p>Configure your new project.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"940\" height=\"481\" src=\"http:\/\/waqar-arshad.com\/wp-content\/uploads\/2023\/07\/image-21.png\" alt=\"\" class=\"wp-image-1975\" srcset=\"http:\/\/waqar-arshad.com\/wp-content\/uploads\/2023\/07\/image-21.png 940w, http:\/\/waqar-arshad.com\/wp-content\/uploads\/2023\/07\/image-21-300x154.png 300w, http:\/\/waqar-arshad.com\/wp-content\/uploads\/2023\/07\/image-21-768x393.png 768w\" sizes=\"auto, (max-width: 940px) 100vw, 940px\" \/><\/figure>\n\n\n\n<p><strong>Step 3<\/strong><\/p>\n\n\n\n<p>Provide additional information.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"940\" height=\"476\" src=\"http:\/\/waqar-arshad.com\/wp-content\/uploads\/2023\/07\/image-22.png\" alt=\"\" class=\"wp-image-1976\" srcset=\"http:\/\/waqar-arshad.com\/wp-content\/uploads\/2023\/07\/image-22.png 940w, http:\/\/waqar-arshad.com\/wp-content\/uploads\/2023\/07\/image-22-300x152.png 300w, http:\/\/waqar-arshad.com\/wp-content\/uploads\/2023\/07\/image-22-768x389.png 768w\" sizes=\"auto, (max-width: 940px) 100vw, 940px\" \/><\/figure>\n\n\n\n<p><strong>Step 4<\/strong><\/p>\n\n\n\n<p>Install a few NuGet packages.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"940\" height=\"239\" src=\"http:\/\/waqar-arshad.com\/wp-content\/uploads\/2023\/07\/image-23.png\" alt=\"\" class=\"wp-image-1977\" srcset=\"http:\/\/waqar-arshad.com\/wp-content\/uploads\/2023\/07\/image-23.png 940w, http:\/\/waqar-arshad.com\/wp-content\/uploads\/2023\/07\/image-23-300x76.png 300w, http:\/\/waqar-arshad.com\/wp-content\/uploads\/2023\/07\/image-23-768x195.png 768w\" sizes=\"auto, (max-width: 940px) 100vw, 940px\" \/><\/figure>\n\n\n\n<p><strong>Step 5<\/strong><\/p>\n\n\n\n<p>Add the following code inside the Program.cs class:<\/p>\n\n\n\n<p>using RabbitMQ.Client;<\/p>\n\n\n\n<p>using RabbitMQ.Client.Events;<\/p>\n\n\n\n<p>using System.Text;<\/p>\n\n\n\n<p>\/\/Here we specify the Rabbit MQ Server. we use rabbitmq docker image and use it<\/p>\n\n\n\n<p>var factory = new ConnectionFactory {<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp; HostName = &#8220;localhost&#8221;<\/p>\n\n\n\n<p>};<\/p>\n\n\n\n<p>\/\/Create the RabbitMQ connection using connection factory details as i mentioned above<\/p>\n\n\n\n<p>var connection = factory.CreateConnection();<\/p>\n\n\n\n<p>\/\/Here we create channel with session and model<\/p>\n\n\n\n<p>using<\/p>\n\n\n\n<p>var channel = connection.CreateModel();<\/p>\n\n\n\n<p>\/\/declare the queue after mentioning name and a few property related to that<\/p>\n\n\n\n<p>channel.QueueDeclare(&#8220;product&#8221;, exclusive: false);<\/p>\n\n\n\n<p>\/\/Set Event object which listen message from chanel which is sent by producer<\/p>\n\n\n\n<p>var consumer = new EventingBasicConsumer(channel);<\/p>\n\n\n\n<p>consumer.Received += (model, eventArgs) =&gt; {<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp; var body = eventArgs.Body.ToArray();<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp; var message = Encoding.UTF8.GetString(body);<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp; Console.WriteLine($ &#8220;Product message received: {message}&#8221;);<\/p>\n\n\n\n<p>};<\/p>\n\n\n\n<p>\/\/read the message<\/p>\n\n\n\n<p>channel.BasicConsume(queue: &#8220;product&#8221;, autoAck: true, consumer: consumer);<\/p>\n\n\n\n<p>Console.ReadKey();<\/p>\n\n\n\n<p>C#<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>Step 6<\/strong><\/p>\n\n\n\n<p>Final Project Structure:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"541\" height=\"724\" src=\"http:\/\/waqar-arshad.com\/wp-content\/uploads\/2023\/07\/image-24.png\" alt=\"\" class=\"wp-image-1978\" srcset=\"http:\/\/waqar-arshad.com\/wp-content\/uploads\/2023\/07\/image-24.png 541w, http:\/\/waqar-arshad.com\/wp-content\/uploads\/2023\/07\/image-24-224x300.png 224w\" sizes=\"auto, (max-width: 541px) 100vw, 541px\" \/><\/figure>\n\n\n\n<p><strong>Step 7<\/strong><\/p>\n\n\n\n<p>Go to the solution property and configure both the producer and the consumer project as a starting project as shown below:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"940\" height=\"646\" src=\"http:\/\/waqar-arshad.com\/wp-content\/uploads\/2023\/07\/image-25.png\" alt=\"\" class=\"wp-image-1979\" srcset=\"http:\/\/waqar-arshad.com\/wp-content\/uploads\/2023\/07\/image-25.png 940w, http:\/\/waqar-arshad.com\/wp-content\/uploads\/2023\/07\/image-25-300x206.png 300w, http:\/\/waqar-arshad.com\/wp-content\/uploads\/2023\/07\/image-25-768x528.png 768w\" sizes=\"auto, (max-width: 940px) 100vw, 940px\" \/><\/figure>\n\n\n\n<p><strong>Step 8<\/strong><\/p>\n\n\n\n<p>Open the following URL to open the RabbitMQ dashboard on the port we set while running docker:<\/p>\n\n\n\n<p><a href=\"http:\/\/localhost:15672\/\" target=\"_blank\" rel=\"noreferrer noopener\">http:\/\/localhost:15672\/<\/a><\/p>\n\n\n\n<p>When you click&nbsp;the URL, the login page will open.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"940\" height=\"260\" src=\"http:\/\/waqar-arshad.com\/wp-content\/uploads\/2023\/07\/image-26.png\" alt=\"\" class=\"wp-image-1980\" srcset=\"http:\/\/waqar-arshad.com\/wp-content\/uploads\/2023\/07\/image-26.png 940w, http:\/\/waqar-arshad.com\/wp-content\/uploads\/2023\/07\/image-26-300x83.png 300w, http:\/\/waqar-arshad.com\/wp-content\/uploads\/2023\/07\/image-26-768x212.png 768w\" sizes=\"auto, (max-width: 940px) 100vw, 940px\" \/><\/figure>\n\n\n\n<p>Enter default username (&#8220;guest&#8221;) and password (also &#8220;guest&#8221;), and next you will see the dashboard.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"940\" height=\"553\" src=\"http:\/\/waqar-arshad.com\/wp-content\/uploads\/2023\/07\/image-27.png\" alt=\"\" class=\"wp-image-1981\" srcset=\"http:\/\/waqar-arshad.com\/wp-content\/uploads\/2023\/07\/image-27.png 940w, http:\/\/waqar-arshad.com\/wp-content\/uploads\/2023\/07\/image-27-300x176.png 300w, http:\/\/waqar-arshad.com\/wp-content\/uploads\/2023\/07\/image-27-768x452.png 768w\" sizes=\"auto, (max-width: 940px) 100vw, 940px\" \/><\/figure>\n\n\n\n<p>Open the queue tab, in which you will see our product queue.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"940\" height=\"496\" src=\"http:\/\/waqar-arshad.com\/wp-content\/uploads\/2023\/07\/image-28.png\" alt=\"\" class=\"wp-image-1982\" srcset=\"http:\/\/waqar-arshad.com\/wp-content\/uploads\/2023\/07\/image-28.png 940w, http:\/\/waqar-arshad.com\/wp-content\/uploads\/2023\/07\/image-28-300x158.png 300w, http:\/\/waqar-arshad.com\/wp-content\/uploads\/2023\/07\/image-28-768x405.png 768w\" sizes=\"auto, (max-width: 940px) 100vw, 940px\" \/><\/figure>\n\n\n\n<p><strong>Step 9<\/strong><\/p>\n\n\n\n<p>Enter the one product details and execute the API.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"940\" height=\"429\" src=\"http:\/\/waqar-arshad.com\/wp-content\/uploads\/2023\/07\/image-29.png\" alt=\"\" class=\"wp-image-1983\" srcset=\"http:\/\/waqar-arshad.com\/wp-content\/uploads\/2023\/07\/image-29.png 940w, http:\/\/waqar-arshad.com\/wp-content\/uploads\/2023\/07\/image-29-300x137.png 300w, http:\/\/waqar-arshad.com\/wp-content\/uploads\/2023\/07\/image-29-768x351.png 768w\" sizes=\"auto, (max-width: 940px) 100vw, 940px\" \/><\/figure>\n\n\n\n<p><strong>Step 10<\/strong><\/p>\n\n\n\n<p>When you execute the above API using swagger, then the message is sent in the queue. You can see inside the console window of the consumer the product details that he listened to from the queue immediately.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"940\" height=\"132\" src=\"http:\/\/waqar-arshad.com\/wp-content\/uploads\/2023\/07\/image-30.png\" alt=\"\" class=\"wp-image-1984\" srcset=\"http:\/\/waqar-arshad.com\/wp-content\/uploads\/2023\/07\/image-30.png 940w, http:\/\/waqar-arshad.com\/wp-content\/uploads\/2023\/07\/image-30-300x42.png 300w, http:\/\/waqar-arshad.com\/wp-content\/uploads\/2023\/07\/image-30-768x108.png 768w\" sizes=\"auto, (max-width: 940px) 100vw, 940px\" \/><\/figure>\n\n\n\n<p>This is all about RabbitMQ, you may use this as per your requirement and purpose.<\/p>\n\n\n\n<p>Conclusion<\/p>\n\n\n\n<p>We discussed all the things related to RabbitMQ beginning from the introduction, working,&nbsp;and then set up a docker image and a few functionalities related to the producer and the consumer using the product application.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>RabbitMQ Message Queue Using .NET Core 6 Web API In this article, we will&nbsp;discuss the RabbitMQ Message Queue and its implementation using .NET Core 6 API as Message Producer and Console Application as a Message Consumer. Agenda: Prerequisites: Introduction of RabbitMQ Benefits of using RabbitMQ There are many benefits to using a Message Broker to [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_uag_custom_page_level_css":"","site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[37],"tags":[],"class_list":["post-1966","post","type-post","status-publish","format-standard","hentry","category-message-broker"],"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false},"uagb_author_info":{"display_name":"admin","author_link":"http:\/\/waqar-arshad.com\/index.php\/author\/waqar_29_1\/"},"uagb_comment_info":28,"uagb_excerpt":"RabbitMQ Message Queue Using .NET Core 6 Web API In this article, we will&nbsp;discuss the RabbitMQ Message Queue and its implementation using .NET Core 6 API as Message Producer and Console Application as a Message Consumer. Agenda: Prerequisites: Introduction of RabbitMQ Benefits of using RabbitMQ There are many benefits to using a Message Broker to&hellip;","_links":{"self":[{"href":"http:\/\/waqar-arshad.com\/index.php\/wp-json\/wp\/v2\/posts\/1966","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/waqar-arshad.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/waqar-arshad.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/waqar-arshad.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/waqar-arshad.com\/index.php\/wp-json\/wp\/v2\/comments?post=1966"}],"version-history":[{"count":1,"href":"http:\/\/waqar-arshad.com\/index.php\/wp-json\/wp\/v2\/posts\/1966\/revisions"}],"predecessor-version":[{"id":1985,"href":"http:\/\/waqar-arshad.com\/index.php\/wp-json\/wp\/v2\/posts\/1966\/revisions\/1985"}],"wp:attachment":[{"href":"http:\/\/waqar-arshad.com\/index.php\/wp-json\/wp\/v2\/media?parent=1966"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/waqar-arshad.com\/index.php\/wp-json\/wp\/v2\/categories?post=1966"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/waqar-arshad.com\/index.php\/wp-json\/wp\/v2\/tags?post=1966"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}