{"id":33005,"date":"2021-05-25T19:50:00","date_gmt":"2021-05-25T17:50:00","guid":{"rendered":"https:\/\/blexin.com\/?p=33005"},"modified":"2021-05-28T10:03:55","modified_gmt":"2021-05-28T08:03:55","slug":"masstransit-a-real-use-case","status":"publish","type":"post","link":"https:\/\/blexin.com\/en\/blog-en\/masstransit-a-real-use-case\/","title":{"rendered":"Mass Transit: a real use case"},"content":{"rendered":"\n<figure class=\"wp-block-image size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"608\" src=\"https:\/\/i0.wp.com\/blexin.com\/wp-content\/uploads\/2021\/05\/10_ITA_1105x656_blog-masstransit.png?resize=1024%2C608&#038;ssl=1\" alt=\"mass transit\" class=\"wp-image-32936\" srcset=\"https:\/\/blexin.com\/wp-content\/uploads\/2021\/05\/10_ITA_1105x656_blog-masstransit-1024x608.png 1024w, https:\/\/blexin.com\/wp-content\/uploads\/2021\/05\/10_ITA_1105x656_blog-masstransit-980x582.png 980w, https:\/\/blexin.com\/wp-content\/uploads\/2021\/05\/10_ITA_1105x656_blog-masstransit-480x285.png 480w\" sizes=\"auto, (min-width: 0px) and (max-width: 480px) 480px, (min-width: 481px) and (max-width: 980px) 980px, (min-width: 981px) 1024px, 100vw\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">In the last year and a half, I have had the opportunity to work for a client with a strongly microservice-oriented architecture to support his system consisting of several applications and services that interoperate in an asynchronous way using a Service Bus.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A Service Bus is an integration service that allows interoperability among heterogeneous components in a distributed system. When an application needs to send a message, it will do using a dedicated Service Bus method that, internally, will be responsible for routing it to one or more endpoints that can handle it. From the point of view of the application that manages the message, the Service Bus must be able to pick up the messages and trigger processes to handle them. When a message is successfully managed, it reports to the broker that the message can be deleted. If the processing fails, depending on the configuration of the error management policies, apply strategies to reprocess it.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The client uses the Masstransit framework as an implementation of a Service Bus. In particular, in the projects we worked on, we used it to integrate a web app with a windows service to manage some prolonged running operations without impacting the web application.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This framework offers us some important features, in addition to the basic features that each Service Bus should make available. These features are:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Transparent management of exceptions, retries and poisoned messages;<\/li><li>Competing consumers pattern;<\/li><li>Sagas management.<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">For a complete list of features, take a look at <a href=\"https:\/\/masstransit-project.com\/\/understand\/additions-to-transport.html\" target=\"_blank\" rel=\"noreferrer noopener\">this link<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">A real use case<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In the example I present, we will have a web app implemented with Razor Pages, which will give the user the ability to create meetings. Once created, we want to send emails and SMS notifications to the guests, but we want these operations not to impact the web app and instead are carried out by services created specifically for this type of task. The web application will send a message using MassTransit. Then, it&#8217;s the task of the framework that routes the message through the transport infrastructure (Azure Service Bus, Amazon SQS, RabbitMq). The services that will consume these messages will be two console applications: one will simulate sending emails, and the other sending SMS. With MassTransit, we will create two consumers who can manage the type of message published by the web app. In the article, you will find only the code related to MassTransit; the code related to the UI will be omitted; moreover, since the consumers will be almost similar, we will focus only on the code of the EmailSender. But you can find the complete code on <a href=\"https:\/\/github.com\/claudiom248\/MassTransitIntro\" target=\"_blank\" rel=\"noreferrer noopener\">my repository<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Web Application (Publisher)<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Our web app will be responsible for sending a message through our service bus.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The instance of the bus will be injected into the constructor and is of type IBus. This interface contains definitions of the methods needed to send messages through the bus.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\npublic IndexModel(IBus bus){    _bus = bus;} \n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">After clicking a UI button, a handler method will be triggered, which sends a message.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\npublic async Task&lt;IActionResult&gt; OnPostCreateMeetingAsync()\n{\n        var message = new MeetingCreatedMessage(Title, Creator, Participants?.Split(&#039;,&#039;), StartsOn, EndsOn); \n        await SendMessage(message);    \n        return Page();\n}\n\nprivate async Task SendMessage(MeetingCreatedMessage message)\n{\n        try\n        {\n                await _bus.Publish(message);\n                MessageSent = true;\n        }\n        catch\n        {\n                MessageSent = false;    \n        }\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">The message we send will be of type <em>MeetingCreatedMessage <\/em>and will be published: this means that we will use the publish\/subscribe communication model, ideal for our use case because the message will have more consumers who can manage it<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">EmailSender Console Application (Consumer1)<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Our first console application will be the consumer responsible for &#8220;sending&#8221; mail to the meeting participants. In this project, we have a class that deals with the consumption of the message. <\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\npublic class MeetingCreatedMessageConsumer : IConsumer&lt;MeetingCreatedMessage&gt; \n{  ...  }\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">The class implements <strong>IConsumer&lt;MeetingCreatedMessage><\/strong>, a Masstransit interface indicating that our consumer can process the specified type parameter messages.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\npublic async Task Consume(ConsumeContext&lt;MeetingCreatedMessage&gt; context)\n{\n        var meeting = context.Message; \n        \n        if (IsMeetingAlreadyEnded(meeting))    \n        {\n                return;\n        }\n        \n        var emails = CreateEmails(meeting);\n        await SendEmails(emails);\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">When a message is available in the queue to which the application is hooked, the MassTransit middleware will read the type in the metadata. It will identify the consumer who can manage the message type, then the Consume method will be called.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">MassTransit Configuration<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In the previous snippets, we have seen how to publish and consume a message with MassTransit. But that\u2019s not all: what we\u2019ve seen so far is the code that uses middleware to publish\/consume messages is the code independent of the transport technology we use. We need to configure the Masstransit middleware to specify the type of transport, settings, and endpoints.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Note that for each transport you want to use, you will need to install the appropriate MassTransit Nuget package; otherwise, you will not find the extension methods that allow you to build the bus instance.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Transport with Azure Service Bus<\/h2>\n\n\n\n<figure class=\"wp-block-image size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"302\" src=\"https:\/\/i0.wp.com\/blexin.com\/wp-content\/uploads\/2021\/05\/02-asb-resource-group.png?resize=1024%2C302&#038;ssl=1\" alt=\"\" class=\"wp-image-32949\" srcset=\"https:\/\/blexin.com\/wp-content\/uploads\/2021\/05\/02-asb-resource-group-980x289.png 980w, https:\/\/blexin.com\/wp-content\/uploads\/2021\/05\/02-asb-resource-group-480x142.png 480w\" sizes=\"auto, (min-width: 0px) and (max-width: 480px) 480px, (min-width: 481px) and (max-width: 980px) 980px, (min-width: 981px) 1024px, 100vw\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">We create a group of resources and a Service Bus type resource by selecting the Standard plan, essential to create topics and subscriptions.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"348\" src=\"https:\/\/i0.wp.com\/blexin.com\/wp-content\/uploads\/2021\/05\/03-asb-shared-access-policies.png?resize=1024%2C348&#038;ssl=1\" alt=\"\" class=\"wp-image-32952\" srcset=\"https:\/\/blexin.com\/wp-content\/uploads\/2021\/05\/03-asb-shared-access-policies-980x333.png 980w, https:\/\/blexin.com\/wp-content\/uploads\/2021\/05\/03-asb-shared-access-policies-480x163.png 480w\" sizes=\"auto, (min-width: 0px) and (max-width: 480px) 480px, (min-width: 481px) and (max-width: 980px) 980px, (min-width: 981px) 1024px, 100vw\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">In the&nbsp;shared&nbsp;access policies&nbsp;section,&nbsp;click&nbsp;RootManageSharedAccessKey&nbsp;and copy the&nbsp;<strong>Primary&nbsp;Connection&nbsp;String.<\/strong>&nbsp;We&nbsp;will&nbsp;use&nbsp;this&nbsp;key to&nbsp;grant&nbsp;MassTransit&nbsp;permissions&nbsp;to access and&nbsp;manage&nbsp;resources&nbsp;on the Bus.&nbsp;<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\npublic void ConfigureServices(IServiceCollection services)\n{\n        services.AddRazorPages();\n\n        var bus = Bus.Factory.CreateUsingAzureServiceBus(cfg =&gt; \n        {\n                cfg.Host(Configuration&#x5B;&quot;AzureServiceBus:ConnectionString&quot;]);\n        });\n        services.AddSingleton&lt;IBus&gt;(bus);\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">The creation of the bus is effortless: we build it using the method CreateUsingAzureServiceBus and specifying the connection string previously copied<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\nstatic async Task Main()\n{\n        var emailSender = new FakeEmailSender();\n\n        var bus = Bus.Factory.CreateUsingAzureServiceBus(cfg =&gt;\n        {\n                cfg.Host(Configuration&#x5B;&quot;AzureServiceBus:ConnectionString&quot;]);         \n                cfg.ReceiveEndpoint(Configuration&#x5B;&quot;AzureServiceBus:Queue&quot;], endpoint =&gt;\n                {\n                        endpoint.Consumer(() =&gt; new MeetingCreatedMessageConsumer(emailSender));\n                });\n        });\n        await bus.StartAsync();\n        Console.ReadKey();\n        await bus.StopAsync();\n} \n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">The bus configuration on the consumer side is similar, but you need to specify the endpoints too. These latter represent an abstraction of a path from which our app will read messages. With the definition of an endpoint, a queue will be created if it does not already exist.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Specifying the endpoint is not enough: we have to register the consumers called when the messages are received. This last operation will create, if not already exists, a topic that will have the same name of the message that the consumer can manage.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Let\u2019s run the projects and see what happens on our bus.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"166\" src=\"https:\/\/i0.wp.com\/blexin.com\/wp-content\/uploads\/2021\/05\/04-asb-topic.png?resize=1024%2C166&#038;ssl=1\" alt=\"\" class=\"wp-image-32957\" srcset=\"https:\/\/blexin.com\/wp-content\/uploads\/2021\/05\/04-asb-topic-980x159.png 980w, https:\/\/blexin.com\/wp-content\/uploads\/2021\/05\/04-asb-topic-480x78.png 480w\" sizes=\"auto, (min-width: 0px) and (max-width: 480px) 480px, (min-width: 481px) and (max-width: 980px) 980px, (min-width: 981px) 1024px, 100vw\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">In the Topics section, we can see that MassTransit has created a new topic with two subscriptions, one for each queue that must receive a copy of the message.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"181\" src=\"https:\/\/i0.wp.com\/blexin.com\/wp-content\/uploads\/2021\/05\/06-asb-subscriptions.png?resize=1024%2C181&#038;ssl=1\" alt=\"\" class=\"wp-image-32963\" srcset=\"https:\/\/blexin.com\/wp-content\/uploads\/2021\/05\/06-asb-subscriptions-980x173.png 980w, https:\/\/blexin.com\/wp-content\/uploads\/2021\/05\/06-asb-subscriptions-480x85.png 480w\" sizes=\"auto, (min-width: 0px) and (max-width: 480px) 480px, (min-width: 481px) and (max-width: 980px) 980px, (min-width: 981px) 1024px, 100vw\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">In the Subscriptions section we can see what queues are subscribed to the topic.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"200\" src=\"https:\/\/i0.wp.com\/blexin.com\/wp-content\/uploads\/2021\/05\/05-asb-queues.png?resize=1024%2C200&#038;ssl=1\" alt=\"\" class=\"wp-image-32960\" srcset=\"https:\/\/blexin.com\/wp-content\/uploads\/2021\/05\/05-asb-queues-980x192.png 980w, https:\/\/blexin.com\/wp-content\/uploads\/2021\/05\/05-asb-queues-480x94.png 480w\" sizes=\"auto, (min-width: 0px) and (max-width: 480px) 480px, (min-width: 481px) and (max-width: 980px) 980px, (min-width: 981px) 1024px, 100vw\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Finally, in the Queues section we can see our queues.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"905\" height=\"403\" data-attachment-id=\"32966\" data-permalink=\"https:\/\/blexin.com\/en\/blog-en\/masstransit-a-real-use-case\/attachment\/07-asb-queue-metric-2\/\" data-orig-file=\"https:\/\/i0.wp.com\/blexin.com\/wp-content\/uploads\/2021\/05\/07-asb-queue-metric.png?fit=905%2C403&amp;ssl=1\" data-orig-size=\"905,403\" data-comments-opened=\"0\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"07-asb-queue-metric\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/blexin.com\/wp-content\/uploads\/2021\/05\/07-asb-queue-metric.png?fit=905%2C403&amp;ssl=1\" src=\"https:\/\/i0.wp.com\/blexin.com\/wp-content\/uploads\/2021\/05\/07-asb-queue-metric.png?resize=905%2C403&#038;ssl=1\" alt=\"\" class=\"wp-image-32966\" srcset=\"https:\/\/blexin.com\/wp-content\/uploads\/2021\/05\/07-asb-queue-metric.png 905w, https:\/\/blexin.com\/wp-content\/uploads\/2021\/05\/07-asb-queue-metric-480x214.png 480w\" sizes=\"auto, (min-width: 0px) and (max-width: 480px) 480px, (min-width: 481px) 905px, 100vw\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Let\u2019s send messages using the web app, choose any queue on our Azure Bus, and see in the metrics section that the number of incoming and outgoing messages increases. Note that the metrics are not updated in real-time, so you should wait a few minutes before observing the increment. To ensure that messages arrive, we can see on the consumer console some log indicating that the emails and SMS were sent. This means that our consumers have received and processed the messages.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Transport with Amazon SQS\/SNS<\/h2>\n\n\n\n<figure class=\"wp-block-image size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"221\" src=\"https:\/\/i0.wp.com\/blexin.com\/wp-content\/uploads\/2021\/05\/08-sqs-iam.png?resize=1024%2C221&#038;ssl=1\" alt=\"\" class=\"wp-image-32969\" srcset=\"https:\/\/blexin.com\/wp-content\/uploads\/2021\/05\/08-sqs-iam-980x212.png 980w, https:\/\/blexin.com\/wp-content\/uploads\/2021\/05\/08-sqs-iam-480x104.png 480w\" sizes=\"auto, (min-width: 0px) and (max-width: 480px) 480px, (min-width: 481px) and (max-width: 980px) 980px, (min-width: 981px) 1024px, 100vw\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">The first step is to create an user in Identity and Access Management (IAM) and generate programmatic access keys.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"310\" src=\"https:\/\/i0.wp.com\/blexin.com\/wp-content\/uploads\/2021\/05\/09-sqs-iam-authorizations.png?resize=1024%2C310&#038;ssl=1\" alt=\"\" class=\"wp-image-32972\" srcset=\"https:\/\/blexin.com\/wp-content\/uploads\/2021\/05\/09-sqs-iam-authorizations-980x296.png 980w, https:\/\/blexin.com\/wp-content\/uploads\/2021\/05\/09-sqs-iam-authorizations-480x145.png 480w\" sizes=\"auto, (min-width: 0px) and (max-width: 480px) 480px, (min-width: 481px) and (max-width: 980px) 980px, (min-width: 981px) 1024px, 100vw\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Grant <strong>AmazonSQSFullAccess<\/strong> and <strong>AmazonSNSFullAccess <\/strong>permissions then copy the access keys<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\npublic void ConfigureServices(IServiceCollection services)\n{\n        services.AddRazorPages();\n\n        var bus = Bus.Factory.CreateUsingAmazonSqs(cfg =&gt;\n        {\n                cfg.Host(Configuration&#x5B;&quot;AmazonSqs:Region&quot;], host =&gt;\n                {\n                        host.AccessKey(Configuration&#x5B;&quot;AmazonSqs:AccessKey&quot;]);            \n                        host.SecretKey(Configuration&#x5B;&quot;AmazonSqs:SecretKey&quot;]);\n                });\n        });\n        services.AddSingleton&lt;IBus&gt;(bus);\n} \n<\/pre><\/div>\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\nstatic async Task Main()\n{\n        var emailSender = new FakeEmailSender();\n        \n        var bus = Bus.Factory.CreateUsingAmazonSqs(cfg =&gt;\n        {\n                cfg.Host(Configuration&#x5B;&quot;AmazonSqs:Region&quot;], host =&gt;\n                {\n                        host.AccessKey(Configuration&#x5B;&quot;AmazonSqs:AccessKey&quot;]);            \n                        host.SecretKey(Configuration&#x5B;&quot;AmazonSqs:SecretKey&quot;]);\n                });\n\n                cfg.ReceiveEndpoint(Configuration&#x5B;&quot;AmazonSqs:Queue&quot;], endpoint =&gt;\n                {\n                        endpoint.Consumer(() =&gt; new MeetingCreatedMessageConsumer(emailSender));\n                });\n        });\n        await bus.StartAsync();\n        Console.ReadKey();\n        await bus.StopAsync();\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">Even in this case, the bus creation and configuration are straightforward for both the publisher and the consumer. The difference is that we use a different method to create an AWS SQS\/SNS bus.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Now let&#8217;s run the projects.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"456\" src=\"https:\/\/i0.wp.com\/blexin.com\/wp-content\/uploads\/2021\/05\/10-sqs-topic.png?resize=1024%2C456&#038;ssl=1\" alt=\"\" class=\"wp-image-32976\" srcset=\"https:\/\/blexin.com\/wp-content\/uploads\/2021\/05\/10-sqs-topic-980x436.png 980w, https:\/\/blexin.com\/wp-content\/uploads\/2021\/05\/10-sqs-topic-480x214.png 480w\" sizes=\"auto, (min-width: 0px) and (max-width: 480px) 480px, (min-width: 481px) and (max-width: 980px) 980px, (min-width: 981px) 1024px, 100vw\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Like ASB, a topic with a subscription is created for each queue that should receive a copy of the message.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"288\" src=\"https:\/\/i0.wp.com\/blexin.com\/wp-content\/uploads\/2021\/05\/11-sqs-queues.png?resize=1024%2C288&#038;ssl=1\" alt=\"\" class=\"wp-image-32979\" srcset=\"https:\/\/blexin.com\/wp-content\/uploads\/2021\/05\/11-sqs-queues-980x276.png 980w, https:\/\/blexin.com\/wp-content\/uploads\/2021\/05\/11-sqs-queues-480x135.png 480w\" sizes=\"auto, (min-width: 0px) and (max-width: 480px) 480px, (min-width: 481px) and (max-width: 980px) 980px, (min-width: 981px) 1024px, 100vw\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Here are the queues:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"441\" src=\"https:\/\/i0.wp.com\/blexin.com\/wp-content\/uploads\/2021\/05\/12-sqs-queue-monitor.png?resize=1024%2C441&#038;ssl=1\" alt=\"\" class=\"wp-image-32982\" srcset=\"https:\/\/blexin.com\/wp-content\/uploads\/2021\/05\/12-sqs-queue-monitor-980x422.png 980w, https:\/\/blexin.com\/wp-content\/uploads\/2021\/05\/12-sqs-queue-monitor-480x206.png 480w\" sizes=\"auto, (min-width: 0px) and (max-width: 480px) 480px, (min-width: 481px) and (max-width: 980px) 980px, (min-width: 981px) 1024px, 100vw\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Let&#8217;s go to the Monitoring tab of any queue, and after sending some messages, we can see how the metric &#8220;Number Of Messages Received&#8221; goes up. As in ASB, the metrics don&#8217;t update in real-time, and you have to wait a while before observing them.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Transport with RabbitMQ<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In this last section we will see how to use RabbitMq, the most popular message broker.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\npublic void ConfigureServices(IServiceCollection services)\n{\n        services.AddRazorPages();\n\n        var bus = Bus.Factory.CreateUsingRabbitMq(cfg =&gt;\n        {\n                cfg.Host(Configuration&#x5B;&quot;RabbitMQ:Host&quot;], host =&gt;\n                {\n                        host.Username(Configuration&#x5B;&quot;RabbitMQ:Username&quot;]);            \n                        host.Password(Configuration&#x5B;&quot;RabbitMQ:Password&quot;]);\n                });\n        });\n        services.AddSingleton&lt;IBus&gt;(bus);\n} \n<\/pre><\/div>\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\nstatic async Task Main()\n{\n        var emailSender = new FakeEmailSender();\n\n        var bus = Bus.Factory.CreateUsingRabbitMq(cfg =&gt;\n        {\n                cfg.Host(Configuration&#x5B;&quot;RabbitMQ:Host&quot;], host =&gt;\n                {\n                        host.Username(Configuration&#x5B;&quot;RabbitMQ:Username&quot;]);            \n                        host.Password(Configuration&#x5B;&quot;RabbitMQ:Password&quot;]);\n                });\n\n                cfg.ReceiveEndpoint(Configuration&#x5B;&quot;RabbitMQ:Queue&quot;], endpoint =&gt;\n                {\n                        endpoint.Consumer(() =&gt; new MeetingCreatedMessageConsumer(emailSender));\n                });\n        });\n        await bus.StartAsync();\n        Console.ReadKey();\n        await bus.StopAsync();\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">As you see in the snippets, the configuration is similar to those previously seen; and the difference lies in the method of the factory used to build the bus and the way we configure the host.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Also, we run the three projects.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"455\" src=\"https:\/\/i0.wp.com\/blexin.com\/wp-content\/uploads\/2021\/05\/13-rabbit-exchanges-1024x455.png?resize=1024%2C455&#038;ssl=1\" alt=\"\" class=\"wp-image-32986\" srcset=\"https:\/\/blexin.com\/wp-content\/uploads\/2021\/05\/13-rabbit-exchanges-980x436.png 980w, https:\/\/blexin.com\/wp-content\/uploads\/2021\/05\/13-rabbit-exchanges-480x213.png 480w\" sizes=\"auto, (min-width: 0px) and (max-width: 480px) 480px, (min-width: 481px) and (max-width: 980px) 980px, (min-width: 981px) 1024px, 100vw\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">In the Exchanges tab, you will see that MassTransit created an exchange with the same name of the type of message to send and two Exchanges that have the same name of the queue to which they are respectively connected.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"322\" src=\"https:\/\/i0.wp.com\/blexin.com\/wp-content\/uploads\/2021\/05\/14-rabbit-queues.png?resize=1024%2C322&#038;ssl=1\" alt=\"\" class=\"wp-image-32989\" srcset=\"https:\/\/blexin.com\/wp-content\/uploads\/2021\/05\/14-rabbit-queues-980x309.png 980w, https:\/\/blexin.com\/wp-content\/uploads\/2021\/05\/14-rabbit-queues-480x151.png 480w\" sizes=\"auto, (min-width: 0px) and (max-width: 480px) 480px, (min-width: 481px) and (max-width: 980px) 980px, (min-width: 981px) 1024px, 100vw\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Here are the two queues.&nbsp;<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"419\" src=\"https:\/\/i0.wp.com\/blexin.com\/wp-content\/uploads\/2021\/05\/15-rabbit-queue-message-rates.png?resize=1024%2C419&#038;ssl=1\" alt=\"\" class=\"wp-image-32993\" srcset=\"https:\/\/blexin.com\/wp-content\/uploads\/2021\/05\/15-rabbit-queue-message-rates-980x401.png 980w, https:\/\/blexin.com\/wp-content\/uploads\/2021\/05\/15-rabbit-queue-message-rates-480x196.png 480w\" sizes=\"auto, (min-width: 0px) and (max-width: 480px) 480px, (min-width: 481px) and (max-width: 980px) 980px, (min-width: 981px) 1024px, 100vw\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">By sending messages and clicking on any queue, we will see that the frequency of messages increases.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusions<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">We have seen how MassTransit helps us create applications that interoperate through a Service Bus in a transport agnostic fashion, making it possible to interchange the different transport technologies without making almost any change to the code. This is just one of the many advantages that MassTransit offers us. For further information, I recommend you go to the <a href=\"https:\/\/masstransit-project.com\/\" target=\"_blank\" rel=\"noreferrer noopener\">official website of the project<\/a>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If&nbsp;you&nbsp;want&nbsp;to know&nbsp;about&nbsp;a library&nbsp;similar&nbsp;to&nbsp;MassTransit, I&nbsp;suggest&nbsp;you&nbsp;take a look&nbsp;at&nbsp;<a href=\"https:\/\/blexin.com\/en\/blog-en\/lets-solve-the-rebus-of-communication\/\" target=\"_blank\" rel=\"noreferrer noopener\">this&nbsp;article&nbsp;written&nbsp;by Genny<\/a> about&nbsp;Rebus.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">I hope that the reading has been to your liking and that it can be helpful to you.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To the next article!<\/p>\n\n\n\n\n","protected":false},"excerpt":{"rendered":"<p>Let\u2019s abstract the transport and create microservices applications that interoperate asynchronously <\/p>\n","protected":false},"author":0,"featured_media":32937,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_et_pb_use_builder":"off","_et_pb_old_content":"","_et_gb_content_width":"","_coblocks_attr":"","_coblocks_dimensions":"","_coblocks_responsive_height":"","_coblocks_accordion_ie_support":"","_crdt_document":"","inline_featured_image":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"_wpas_customize_per_network":false,"jetpack_post_was_ever_published":false},"categories":[688637524],"tags":[688637542,688637381,688637458,688637537],"class_list":["post-33005","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-blog-en","tag-dotnetcore-en","tag-azure-en","tag-rabbitmq-en","tag-swdesign-en"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Mass Transit: a real use case - Blexin<\/title>\n<meta name=\"description\" content=\"With Mass Transit you can manage an architecture with microservices that works asynchronously with each other: an experience in the field\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/blexin.com\/en\/blog-en\/masstransit-a-real-use-case\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Mass Transit: a real use case - Blexin\" \/>\n<meta property=\"og:description\" content=\"With Mass Transit you can manage an architecture with microservices that works asynchronously with each other: an experience in the field\" \/>\n<meta property=\"og:url\" content=\"https:\/\/blexin.com\/en\/blog-en\/masstransit-a-real-use-case\/\" \/>\n<meta property=\"og:site_name\" content=\"Blexin\" \/>\n<meta property=\"article:published_time\" content=\"2021-05-25T17:50:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-05-28T08:03:55+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/i0.wp.com\/blexin.com\/wp-content\/uploads\/2021\/05\/10_ITA_1105x656_blog-masstransit.png?fit=1105%2C656&ssl=1\" \/>\n\t<meta property=\"og:image:width\" content=\"1105\" \/>\n\t<meta property=\"og:image:height\" content=\"656\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"11 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/blexin.com\\\/en\\\/blog-en\\\/masstransit-a-real-use-case\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/blexin.com\\\/en\\\/blog-en\\\/masstransit-a-real-use-case\\\/\"},\"author\":{\"name\":\"\",\"@id\":\"\"},\"headline\":\"Mass Transit: a real use case\",\"datePublished\":\"2021-05-25T17:50:00+00:00\",\"dateModified\":\"2021-05-28T08:03:55+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/blexin.com\\\/en\\\/blog-en\\\/masstransit-a-real-use-case\\\/\"},\"wordCount\":1481,\"image\":{\"@id\":\"https:\\\/\\\/blexin.com\\\/en\\\/blog-en\\\/masstransit-a-real-use-case\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/i0.wp.com\\\/blexin.com\\\/wp-content\\\/uploads\\\/2021\\\/05\\\/10_ITA_1105x656_blog-masstransit.png?fit=1105%2C656&ssl=1\",\"keywords\":[\".NetCore\",\"Azure\",\"RabbitMQ\",\"softwaredesign\"],\"articleSection\":[\"Blog\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/blexin.com\\\/en\\\/blog-en\\\/masstransit-a-real-use-case\\\/\",\"url\":\"https:\\\/\\\/blexin.com\\\/en\\\/blog-en\\\/masstransit-a-real-use-case\\\/\",\"name\":\"Mass Transit: a real use case - Blexin\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/blexin.com\\\/en\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/blexin.com\\\/en\\\/blog-en\\\/masstransit-a-real-use-case\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/blexin.com\\\/en\\\/blog-en\\\/masstransit-a-real-use-case\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/i0.wp.com\\\/blexin.com\\\/wp-content\\\/uploads\\\/2021\\\/05\\\/10_ITA_1105x656_blog-masstransit.png?fit=1105%2C656&ssl=1\",\"datePublished\":\"2021-05-25T17:50:00+00:00\",\"dateModified\":\"2021-05-28T08:03:55+00:00\",\"author\":{\"@id\":\"\"},\"description\":\"With Mass Transit you can manage an architecture with microservices that works asynchronously with each other: an experience in the field\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/blexin.com\\\/en\\\/blog-en\\\/masstransit-a-real-use-case\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/blexin.com\\\/en\\\/blog-en\\\/masstransit-a-real-use-case\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/blexin.com\\\/en\\\/blog-en\\\/masstransit-a-real-use-case\\\/#primaryimage\",\"url\":\"https:\\\/\\\/i0.wp.com\\\/blexin.com\\\/wp-content\\\/uploads\\\/2021\\\/05\\\/10_ITA_1105x656_blog-masstransit.png?fit=1105%2C656&ssl=1\",\"contentUrl\":\"https:\\\/\\\/i0.wp.com\\\/blexin.com\\\/wp-content\\\/uploads\\\/2021\\\/05\\\/10_ITA_1105x656_blog-masstransit.png?fit=1105%2C656&ssl=1\",\"width\":1105,\"height\":656,\"caption\":\"mass transit\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/blexin.com\\\/en\\\/blog-en\\\/masstransit-a-real-use-case\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/blexin.com\\\/en\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Mass Transit: a real use case\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/blexin.com\\\/en\\\/#website\",\"url\":\"https:\\\/\\\/blexin.com\\\/en\\\/\",\"name\":\"Blexin\",\"description\":\"Con noi \u00e8 semplice\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/blexin.com\\\/en\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Mass Transit: a real use case - Blexin","description":"With Mass Transit you can manage an architecture with microservices that works asynchronously with each other: an experience in the field","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/blexin.com\/en\/blog-en\/masstransit-a-real-use-case\/","og_locale":"en_US","og_type":"article","og_title":"Mass Transit: a real use case - Blexin","og_description":"With Mass Transit you can manage an architecture with microservices that works asynchronously with each other: an experience in the field","og_url":"https:\/\/blexin.com\/en\/blog-en\/masstransit-a-real-use-case\/","og_site_name":"Blexin","article_published_time":"2021-05-25T17:50:00+00:00","article_modified_time":"2021-05-28T08:03:55+00:00","og_image":[{"width":1105,"height":656,"url":"https:\/\/i0.wp.com\/blexin.com\/wp-content\/uploads\/2021\/05\/10_ITA_1105x656_blog-masstransit.png?fit=1105%2C656&ssl=1","type":"image\/png"}],"twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"11 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/blexin.com\/en\/blog-en\/masstransit-a-real-use-case\/#article","isPartOf":{"@id":"https:\/\/blexin.com\/en\/blog-en\/masstransit-a-real-use-case\/"},"author":{"name":"","@id":""},"headline":"Mass Transit: a real use case","datePublished":"2021-05-25T17:50:00+00:00","dateModified":"2021-05-28T08:03:55+00:00","mainEntityOfPage":{"@id":"https:\/\/blexin.com\/en\/blog-en\/masstransit-a-real-use-case\/"},"wordCount":1481,"image":{"@id":"https:\/\/blexin.com\/en\/blog-en\/masstransit-a-real-use-case\/#primaryimage"},"thumbnailUrl":"https:\/\/i0.wp.com\/blexin.com\/wp-content\/uploads\/2021\/05\/10_ITA_1105x656_blog-masstransit.png?fit=1105%2C656&ssl=1","keywords":[".NetCore","Azure","RabbitMQ","softwaredesign"],"articleSection":["Blog"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/blexin.com\/en\/blog-en\/masstransit-a-real-use-case\/","url":"https:\/\/blexin.com\/en\/blog-en\/masstransit-a-real-use-case\/","name":"Mass Transit: a real use case - Blexin","isPartOf":{"@id":"https:\/\/blexin.com\/en\/#website"},"primaryImageOfPage":{"@id":"https:\/\/blexin.com\/en\/blog-en\/masstransit-a-real-use-case\/#primaryimage"},"image":{"@id":"https:\/\/blexin.com\/en\/blog-en\/masstransit-a-real-use-case\/#primaryimage"},"thumbnailUrl":"https:\/\/i0.wp.com\/blexin.com\/wp-content\/uploads\/2021\/05\/10_ITA_1105x656_blog-masstransit.png?fit=1105%2C656&ssl=1","datePublished":"2021-05-25T17:50:00+00:00","dateModified":"2021-05-28T08:03:55+00:00","author":{"@id":""},"description":"With Mass Transit you can manage an architecture with microservices that works asynchronously with each other: an experience in the field","breadcrumb":{"@id":"https:\/\/blexin.com\/en\/blog-en\/masstransit-a-real-use-case\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/blexin.com\/en\/blog-en\/masstransit-a-real-use-case\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/blexin.com\/en\/blog-en\/masstransit-a-real-use-case\/#primaryimage","url":"https:\/\/i0.wp.com\/blexin.com\/wp-content\/uploads\/2021\/05\/10_ITA_1105x656_blog-masstransit.png?fit=1105%2C656&ssl=1","contentUrl":"https:\/\/i0.wp.com\/blexin.com\/wp-content\/uploads\/2021\/05\/10_ITA_1105x656_blog-masstransit.png?fit=1105%2C656&ssl=1","width":1105,"height":656,"caption":"mass transit"},{"@type":"BreadcrumbList","@id":"https:\/\/blexin.com\/en\/blog-en\/masstransit-a-real-use-case\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/blexin.com\/en\/"},{"@type":"ListItem","position":2,"name":"Mass Transit: a real use case"}]},{"@type":"WebSite","@id":"https:\/\/blexin.com\/en\/#website","url":"https:\/\/blexin.com\/en\/","name":"Blexin","description":"Con noi \u00e8 semplice","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/blexin.com\/en\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/blexin.com\/wp-content\/uploads\/2021\/05\/10_ITA_1105x656_blog-masstransit.png?fit=1105%2C656&ssl=1","jetpack_shortlink":"https:\/\/wp.me\/pcyUBx-8Al","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/blexin.com\/en\/wp-json\/wp\/v2\/posts\/33005","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blexin.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blexin.com\/en\/wp-json\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/blexin.com\/en\/wp-json\/wp\/v2\/comments?post=33005"}],"version-history":[{"count":7,"href":"https:\/\/blexin.com\/en\/wp-json\/wp\/v2\/posts\/33005\/revisions"}],"predecessor-version":[{"id":33029,"href":"https:\/\/blexin.com\/en\/wp-json\/wp\/v2\/posts\/33005\/revisions\/33029"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blexin.com\/en\/wp-json\/wp\/v2\/media\/32937"}],"wp:attachment":[{"href":"https:\/\/blexin.com\/en\/wp-json\/wp\/v2\/media?parent=33005"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blexin.com\/en\/wp-json\/wp\/v2\/categories?post=33005"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blexin.com\/en\/wp-json\/wp\/v2\/tags?post=33005"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}