Monday, August 20, 2018

Exploring MassTransit InMemory Scheduled Messaging using RabbitMQ and .Net Core

Last week I demoed how to create a MassTransit client/server application using RabbitMQ, .Net Core and Linux.

Today we will explore another very interesting functionality: the ability to Schedule messages and send them in the future.

But before that, let me state that the more I've been using MassTransit, the more I'm liking it but there are a few use cases that I still would like to test. For today's post, we cover the scheduled message use case, testing the in-memory solution.

Persistence Requirements

Before we start, a few highlights from their web site:
Scheduled messages can be achieved using:
  • Quartz.Net in a hosted server
  • Azure Service Bus
  • RabbitMQ by installing a plugin
  • a test-driven In-Memory implementation

Because most of them are already complex enough and would require more than a simple blog post to cover, I decided to spike out the in-memory solution as I'm interested in how MassTransit handles those use cases more on how to configure the underlying storage.

Referencing Packages

MassTransit's Scheduling Api utilizes Quartz.Net. So, for it to work, we will need the to add a reference to the MassTransit.Quartz package to your project with:

$ dotnet add reference <project-name> MassTransit.Quartz --version 5.1.3

Once the reference is added and a dotnet restore is done, you'll then have the necessary extension methods to do the initialization.


The initalization code for the in-memory implementation as simple as adding a
call to UseInMemoryScheduler() on your bus configuration.

Important: Using the in-memory scheduler uses non-durable storage. If the process terminates, any scheduled messages will be lost, immediately, never to be found again. For any production system, using a standalone service is recommended with persistent storage.

Source Code

This gist contains most of what you need. Remember, you need to have some instance of RabbitMQ running and point to it to make this code work:

The source for this post is located on my GitHub page at:

Running the Demo app

So, I run my app and my code schedules a message to be sent 3 seconds after sent by the user.

This is my output:


Hope this serves as an introduction to the scheduling feature within MassTransit. Being a not very popular framework, sometimes it's difficult to find sample code. Hope this helps you with this feature.

Next week I will revise sagas. And I would still like to review transactions, auditing and who knows, creating a real world scenario in which a web app communicates to the backend using MassTransit as an intermediary.

See Also

Why you should start using .Net Core
Package Management in .Net Core
Exporting Html to Pdf using only JavaScript
Importing CSVs with .Net Core and C#
Exporting a CSV generated in-memory in Asp.Net with C#
Building and Running Asp.Net Core apps on Linux
Asp.Net / Asp.Net Core: Generating views in the backend

For more posts on .Net Core, please click here.