Skip to main content

Overview

The messaging infrastructure in AspNetRun Microservices enables asynchronous communication between services through event-driven architecture. It uses MassTransit as the abstraction layer over RabbitMQ for reliable message delivery.

Architecture

The messaging system is built on:
  • BuildingBlocks.Messaging: Core messaging abstractions and events
  • MassTransit: Message broker abstraction and routing
  • RabbitMQ: Message broker for reliable message delivery
  • Integration Events: Event definitions for cross-service communication

Key Components

Message Broker Configuration

The AddMessageBroker extension method configures MassTransit with RabbitMQ:
public static IServiceCollection AddMessageBroker
    (this IServiceCollection services, IConfiguration configuration, Assembly? assembly = null)
{
    services.AddMassTransit(config =>
    {
        config.SetKebabCaseEndpointNameFormatter();

        if (assembly != null)
            config.AddConsumers(assembly);

        config.UsingRabbitMq((context, configurator) =>
        {
            configurator.Host(new Uri(configuration["MessageBroker:Host"]!), host =>
            {
                host.Username(configuration["MessageBroker:UserName"]);
                host.Password(configuration["MessageBroker:Password"]);
            });
            configurator.ConfigureEndpoints(context);
        });
    });

    return services;
}

Configuration Settings

Add these settings to your appsettings.json:
{
  "MessageBroker": {
    "Host": "amqp://rabbitmq:5672",
    "UserName": "guest",
    "Password": "guest"
  }
}

Service Registration

Register the message broker in your service’s Program.cs:
using BuildingBlocks.Messaging.MassTransit;

// Register message broker with consumers
builder.Services.AddMessageBroker(
    builder.Configuration, 
    Assembly.GetExecutingAssembly()
);
The Assembly parameter automatically discovers and registers all IConsumer<T> implementations in your service.

Features

Endpoint Naming

MassTransit uses kebab-case endpoint naming convention automatically:
  • BasketCheckoutEventbasket-checkout-event
  • OrderCreatedEventorder-created-event

Automatic Consumer Discovery

When you pass an assembly, MassTransit automatically:
  1. Scans for classes implementing IConsumer<TMessage>
  2. Registers them in the DI container
  3. Creates queues and exchanges in RabbitMQ
  4. Routes messages to appropriate consumers

Reliable Delivery

RabbitMQ ensures:
  • Message persistence
  • Automatic retries on failure
  • Dead letter queues for failed messages
  • Acknowledgments for processed messages

Message Flow

Next Steps

Events

Learn about the IntegrationEvent base class

Integration Events

Explore specific event implementations

Build docs developers (and LLMs) love