Introduction to MassTransit: A Guide to Streamlined Messaging in C# (2024)

If you're interested in building distributed, scalable, and robust applications in C#, you may have heard of MassTransit. This open-source messaging framework simplifies working with message brokers like RabbitMQ and Azure Service Bus, allowing you to focus more on business logic and less on infrastructure concerns.

What Is MassTransit?

MassTransit is a free, open-source distributed application framework for .NET. It's a service bus for sending messages between different parts of your application, or even across different applications. With MassTransit, you can implement various messaging patterns such as publish/subscribe, request/response, and more, using a consistent and easy-to-understand API.

Why Use MassTransit?

  1. Simplicity: The framework abstracts away much of the boilerplate code required when dealing with message brokers directly.
  2. Extensibility: Easily extend MassTransit by adding middleware components or integrating it with dependency injection frameworks.
  3. Asynchronous Messaging: Embrace async programming with MassTransit’s native support for asynchronous messaging.

It can also let you work with low level messaging tools like RabbitMQ with a LOT less code, as you'll see in a moment.

Getting Started with MassTransit in C#

To kick things off, let's install the MassTransit NuGet package. We'll use the RabbitMQ version for this example.

Install-Package MassTransit.RabbitMQ

Basic Setup

Here's a basic example demonstrating how to set up a MassTransit service bus and a consumer, using RabbitMQ for the transport layer.

using MassTransit;using MassTransitTest;Console.WriteLine("Hello, World!");IBusControl busControl = null;try{ busControl = Bus.Factory.CreateUsingRabbitMq(cfg => { cfg.Host("localhost", "/", h => { h.Username("guest"); h.Password("guest"); }); cfg.ReceiveEndpoint("my_queue", e => { e.Consumer<MyConsumer>(); }); }); busControl.Start(); // This is non-blocking Console.WriteLine("Press any key to exit");}catch (Exception ex){ Console.WriteLine($"An error occurred: {ex.Message}");}Console.ReadKey();if (busControl != null){ busControl.Stop();}
using MassTransit;namespace MassTransitTest;public class MyConsumer : IConsumer<MyMessage>{ public async Task Consume(ConsumeContext<MyMessage> context) { Console.WriteLine($"Received: {context.Message.Text}"); }}
namespace MassTransitTest;public class MyMessage{ public string Text { get; set; }}

That's all the code you need for a console app that will act as a consumer. Next you'll need something that sends messages.

Sending Messages

Sending messages is just as easy. The following files demonstrate how to set up a console app that sends messages continuously, once per second.

using MassTransit;using MassTransitTest;Console.WriteLine("Sender!");var busControl = Bus.Factory.CreateUsingRabbitMq(cfg =>{ cfg.Host("localhost", "/", h => { h.Username("guest"); h.Password("guest"); });});var sendToUri = new Uri("rabbitmq://localhost/my_queue");var endpoint = await busControl.GetSendEndpoint(sendToUri);await busControl.StartAsync(); // This is non-blockingint messageCount = 0;var cts = new CancellationTokenSource();Console.WriteLine("Sending messages. Press any key to stop...");while (!cts.Token.IsCancellationRequested){ messageCount++; await endpoint.Send(new MyMessage { Text = $"Message {messageCount}" }); Console.WriteLine($"Sent: Message {messageCount}"); await Task.Delay(TimeSpan.FromSeconds(1), cts.Token);}await busControl.StopAsync();

It will need access to the message type as well, which can come from a common project both apps reference or can just be duplicated:

namespace MassTransitTest;public class MyMessage{ public string Text { get; set; }}

Now if you run both apps, one should start sending messages while the other consumes them. Assuming you have RabbitMQ set up, that is. The next section shows how to get that going quickly using docker.

Before you can dive into MassTransit, you'll need a message broker. RabbitMQ is one of the most popular choices, and fortunately, it's easy to set up using Docker. So let's go ahead and run RabbitMQ.

Prerequisites

  • Docker installed on your machine

Run RabbitMQ

You can pull and run the RabbitMQ Docker image using the following command:

docker run -d --hostname my-rabbit --name rabbitmq-server -p 5672:5672 -p 15672:15672 rabbitmq:3-management

This command will:

  • Run RabbitMQ as a daemon (-d)
  • Set the hostname to my-rabbit
  • Name the Docker container rabbitmq-server
  • Map port 5672 for RabbitMQ and 15672 for the management interface

Once the container is running, you can access the RabbitMQ management interface at http://localhost:15672/. The default username and password are guest and guest, respectively.

Stop RabbitMQ

When you're done, you can stop the RabbitMQ container with the following command:

docker stop rabbitmq-server

And if you want to remove the container:

docker rm rabbitmq-server

Output

Putting it all together, if you run the sample while RabbitMQ is running you should get output like this:

PS> dotnet runHello, World!Press any key to exitReceived: Message 1Received: Message 2Received: Message 3Received: Message 4Received: Message 5

Conclusion

In a world full of complexities, MassTransit provides a simplified approach to messaging in C#. This article only scratches the surface of its capabilities - a hello world intro if you will - but there's certainly a lot to like about MassTransit. Follow some of the links below to learn more.

References

If you're looking for more tips like this, subscribe to my weekly tips newsletter and be sure to follow me on YouTube.

Introduction to MassTransit: A Guide to Streamlined Messaging in C# (2024)
Top Articles
2023 Fantasy Football Draft Guide: Rankings, cheat sheets and analysis
Fantasy Baseball Cheat Sheet - Printable PDF For All League Types | Fantasy Alarm
Craigslist Home Health Care Jobs
Ups Stores Near
Maria Dolores Franziska Kolowrat Krakowská
Booknet.com Contract Marriage 2
craigslist: south coast jobs, apartments, for sale, services, community, and events
Trade Chart Dave Richard
Owatc Canvas
Snarky Tea Net Worth 2022
Deshret's Spirit
Carter Joseph Hopf
A.e.a.o.n.m.s
litter - tłumaczenie słowa – słownik angielsko-polski Ling.pl
Ave Bradley, Global SVP of design and creative director at Kimpton Hotels & Restaurants | Hospitality Interiors
Ukraine-Russia war: Latest updates
Persona 4 Golden Taotie Fusion Calculator
Zürich Stadion Letzigrund detailed interactive seating plan with seat & row numbers | Sitzplan Saalplan with Sitzplatz & Reihen Nummerierung
Cnnfn.com Markets
Labor Gigs On Craigslist
Hilo Hi Craigslist
Images of CGC-graded Comic Books Now Available Using the CGC Certification Verification Tool
Committees Of Correspondence | Encyclopedia.com
Mals Crazy Crab
Craigslist Free Stuff Merced Ca
Fort Mccoy Fire Map
Little Rock Skipthegames
Wsbtv Fish And Game Report
Arrest Gif
Page 2383 – Christianity Today
Cognitive Science Cornell
Ocala Craigslist Com
Lindy Kendra Scott Obituary
Davita Salary
Fedex Walgreens Pickup Times
Unm Hsc Zoom
oklahoma city community "puppies" - craigslist
Magicseaweed Capitola
Studentvue Columbia Heights
Los Garroberros Menu
Marcus Roberts 1040 Answers
Vons Credit Union Routing Number
Sour OG is a chill recreational strain -- just have healthy snacks nearby (cannabis review)
Candise Yang Acupuncture
Ssc South Carolina
White County
Wgu Admissions Login
Killer Intelligence Center Download
Noga Funeral Home Obituaries
Haunted Mansion Showtimes Near Millstone 14
Craigslist Sarasota Free Stuff
Peugeot-dealer Hedin Automotive: alles onder één dak | Hedin
Latest Posts
Article information

Author: Edwin Metz

Last Updated:

Views: 5631

Rating: 4.8 / 5 (58 voted)

Reviews: 89% of readers found this page helpful

Author information

Name: Edwin Metz

Birthday: 1997-04-16

Address: 51593 Leanne Light, Kuphalmouth, DE 50012-5183

Phone: +639107620957

Job: Corporate Banking Technician

Hobby: Reading, scrapbook, role-playing games, Fishing, Fishing, Scuba diving, Beekeeping

Introduction: My name is Edwin Metz, I am a fair, energetic, helpful, brave, outstanding, nice, helpful person who loves writing and wants to share my knowledge and understanding with you.