Working with .NET Aspire — Step2

Cesar Augusto
4 min readJan 3, 2024

--

Integrating with RabbitMQ

To follow this article, please see the first part here:

Integrating our API Service with RabbitMQ

To add a rabbitMQ we need to understand how we can do this, for example: Running a RabbitMQ when our app runs or using a rabbitMQ instance already running.

Using rabbitMQ container when our app runs

Open the class program.cs into AppHost and configure as below.

var rabbitMQ = builder.AddRabbitMQContainer("message_with_rabbitMQ", password: "rabbitmqpass");
var apiservice = builder.AddProject<Projects.AspireExample_ApiService>("apiservice")
.WithReference(rabbitMQ);

This code will configure and start a rabbitMQ container when our App runs.

Now, we need to configure our API Service to send messages to RabbitMQ.

Open the Manage Nuget packages for the API Service project and install the preview package Aspire.RabbitMQ.Client.

After installing the package, we need to add Dependency with the same connection name and add an endpoint to send a message

builder.AddRabbitMQ("message_with_rabbitMQ");
app.MapGet("/post-message", (IConnection connection) =>
{
var queueName = "test-queue";
using var channel = connection.CreateModel();
channel.QueueDeclare(queueName, exclusive: false, durable: true);
channel.BasicPublish(exchange: "", queueName, null, body: JsonSerializer.SerializeToUtf8Bytes(new { Title = "Book01", Description = "Description of Book01" }));

return "Message sent to RabbitMQ Queue";
});

Note: The .NET Aspire component will help us with a lot of configuration, but never forget that we need to follow the official documentation of our third-party service to have a better implementation.

Now, we already have an endpoint responsible for sending a message to the RabbitMQ instance.

Please, note that we don’t have a RabbitMQ instance running

Let’s execute our project.

When we start the AppHost project, we can access the Aspire Dashboard and see that now we have a rabbitMQ container running.

When we access the URL http://127.0.0.1:52962/ we can see the rabbitMQ management interface.

user: guest
password: rabbitmqpass (this password was defined at program.cs)
Docker Desktop IU

With everything running, I will request our endpoint /post-message

Now, we can see that we have a queue with messages in RabbitMQ.

Now, let’s stop our project and see what happens with the RabbitMQ container.

The container was stopped because we stopped our application.

Using rabbitMQ instance already running.

First, we need to have a rabbitMQ already running.

Docker desktop IU
RabbitMQ management IU

Now, we need to change our program.cs in the AppHost project as below.


var rabbitMQ = builder.AddRabbitMQConnection("message_with_rabbitMQ", connectionString: "amqp://guest:guest@localhost:5672/");

With the configuration above made, we can execute our project again

Now we don’t have any containers running

.NET Aspire Dashboard IU

I’ll request our API Service again http://localhost:5549/post-message

Done! Now, we have our API Service integrated with the RabbitMQ instance that already was running.

Final Tip

We need to wait for the next steps of .NET Aspire. It can be good.

We need to understand that this will help us to start faster with microservice but never forget that reply this “We need to have microservice? What I’ll win with this approach? Will we have time and a team to maintain this?”

Code used in this example

--

--

Cesar Augusto

Software & Solutions Architect | Software Engineer | 6X Microsoft Azure Certified | 4X AWS Certified