Looking for an free and open-source alternative to
NServiceBus? Maybe
MassTransit could
be what you are looking for. Let's understand the platform and how it could be
used on your next project.
What is MassTransit?
MassTransit is a lightweight service bus for building distributed .NET applications.
The main goal is to provide a consistent, .NET friendly abstraction over the
message transport (RabbitMQ,
Azure Service Bus, etc.). MassTransit is not a new project.
They've been around since 2007
and were created as an alternative to
NServiceBus. In early
2014, the framework was rewritten to support asynchronous programming as well as
leveraged the power of messaging platform. The code was also rewritten,
resulting in an entirely new, completely asynchronous, and highly optimized
framework for message processing.
Why MassTransit
Like NServiceBus, MassTransit helps decoupling your backend from your frontend
(and in general, decoupling services), leveraging enterprise design patterns
like CQRS and
Eventual Consistency.
Some of the features you will find in MassTransit are:
MassTransit is also extremely customizable and as mentioned previously, can run
on different transports (RabbitMQ,
Azure Service Bus, etc) providing enormous benefits as both are strong and stable platforms with
different characteristics. It also supports the new
.NET Standard
on .NET Core and runs on multi-platforms.
With that introduction, let's review some code. If worked with NSB before, you're probably looking for three things:
How to initialize the bus
How to initialize the host (your backend, where your handlers run)
How to initialize a client (where you send messages from).
Please take a look at examples below to understand how it all works.
Initializing the Bus
The below code shows how to initialize a simple MassTransit Bus:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
The below code shows how to initialize a simple MassTransit server:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
The below code shows how to initialize a simple MassTransit client that talks to
the above service:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Before we end, I'd like to bring to your attention other things you should evaluate before committing on such
an important framework to your organization. I'd recommend that you research about:
Support: NServiceBus, being backed by a commercial organization deserves a
point on this item. Having a commercial organization backing the product
may potentially be a more compelling argument to your employer. NSB offers
multiple support options including
consulting
and
on-site training.
Online Documentation: both frameworks have good documentation but NSB is definitely ahead on this criteria. You will find way more documentation about NSB.
Community: both MT and NSB have decent online communities, with the
latter being bigger and more active.
Access to current technologies: MT and NSB will definitely provide
you access to modern technologies like cloud and serverless but NSB seems
to be ahead on that regard.
Active development: both NSB and MT have very active developments.
Open-Source: I preferably like to have access to the source code of
the product I'm using, in case there are issues
Ecosystem: Particular, NSB's parent company, offers lots of other
products that are worth considering. These products integrate well with NServiceBus and depending on your use of the platform may
Final Thoughts
This ends our quick introduction about MassTransit. I've been using
NServiceBus for quite
some time and while I recognize its value, I wanted to explore viable (and cheaper) alternatives for my open-source projects. I've been using
MT for a couple of years I can confirm that it's a solid
alternative to NServiceBus. MassTransit today is also a bigger and more solid project than it was 5 years ago and definitely deserves your attention.
However, choosing a critical framework like this is not only about code. I recommend you to also evaluate other criteria such as support, documentation, training and ecosystem. On pretty much every criteria (except for price) NServiceBus is ahead. And maybe those things will count for your organization.
More about MassTransit
Want to learn more about MassTransit? Please consider reading these articles: