One of the things I faced the challenge of overcoming was using RabbitMQ and SignalR in-conjuction with one another.

I am building a ASP.NET Core application and want to consume a RabbitMQ queue when the application starts. When a message is published to that queue, it sends a message to all the clients connected through SignalR.

So how did I go about resolving this?

The Assumptions

This article assumes that you already have successfully added RabbitMQ and SignalR to your .NET Core application. But, if you need further assistance, these articles helped me:

The SignalR Hub

Setting up SignalR "Hub" if the first thing you need to do. The Hub is an abstract class in SignalR, and allows clients to be connected to it. Because of this, the Hub has the ability to send messages to all clients connected, or to a particular client. Here is an example of how to set one up:

You then have to configure that Hub in your .NET Core application. This is an example of it in .NET Core 3:

In earlier versions of .NET Core, you will use app.useSignalR syntax, but this has been depreciated as of version 3.

Consuming a RabbitMQ on Startup

The next thing to do is to consume a RabbitMQ when the application starts. In versions of .NET Framework, you could hack into a lifetime stage in the Global.asax file. So how can you do it in .NET Core?

Well it's a familiar story in .NET Core. In the startup.cs file, there is a Configure method. You can add "lifetime" as a parameter in that method. The "lifetime" parameter inherits IHostApplicationLifetime interface (IApplicationLifetime in versions of .NET Core prior to 3).

In the "lifetime" parameter, there is a delegate called ApplicationStarted. As you can guess, this gets executed once your application has started.

Then it's a case of creating a delegate and attaching it to ApplicationStarted. The delegate has a parameter of IServiceProvider. This allows the delegate to use Dependency Injection. The delegate gets a reference to IRabbitMQService which is a reference we created in Dependency Injection. IRabbitMQService has been set up as a single instance so we can refer to it when the application has started. We need this service so we have a reference to the RabbitMQ connection.

Inside the IRabbitMQService, there is a method called Connect. In this method, it consumes a queue in RabbitMQ. Upon receiving a message from that queue, it uses Dependency Injection to create a reference of the SignalR Hub we created. It's then a simple case of sending a message to all connected clients. Here is an example of Startup.cs

About Dave

I am a .NET developer, building web applications in .NET Framework and .NET Core with a SQL Server database.

Some of the .NET packages I have used include Entity Framework and MVC.

I've also used many JavaScript frameworks such as React and jQuery, and have experience building CSS in SASS.

Related Tags

RabbitMQ
SignalR

Twitter Feed