Skip to content

About

A practical .NET 10 messaging sample demonstrating RabbitMQ publish/subscribe and direct routing patterns with independent producers and consumers.

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Repository files navigation

RabbitMq

A practical .NET 10 / C# messaging sample that demonstrates a user-registration event flowing through RabbitMQ to independent email, SMS, and order-fee consumers.

Language: C# Framework: .NET 10 Broker: RabbitMQ

Table of Contents

Overview

RabbitMq is a small, console-based reference project for learning RabbitMQ with the official .NET client. A registration command collects a user's phone number and email address, serializes the data as JSON, and publishes it through two messaging patterns:

  1. Publish/subscribe — a fanout exchange broadcasts the registration event to the email and SMS services.
  2. Point-to-point routing — a direct exchange routes the same payload to the order-fee service through a dedicated queue.

Each consumer is an independent executable. This separation makes the sample useful for understanding how a broker decouples producers from downstream services.

What This Sample Demonstrates

  • Creating asynchronous RabbitMQ connections and channels with RabbitMQ.Client
  • Declaring durable exchanges and queues
  • Publishing UTF-8 JSON messages
  • Broadcasting an event with a fanout exchange
  • Routing a message with a direct exchange and routing key
  • Consuming messages asynchronously with AsyncEventingBasicConsumer
  • Acknowledging each successfully handled message manually
  • Sharing a simple message contract across console applications

Architecture and Message Flow

flowchart LR
    R[RegisterUser\nproducer] -->|JSON User event| F{{User_Register\nfanout exchange}}
    F --> Q1[(SendEmail queue)]
    F --> Q2[(SendSMS queue)]
    Q1 --> E[SendEmailProject\nconsumer]
    Q2 --> S[SendSMSProject\nconsumer]

    R -->|routing key: FinallyOrder| D{{Users-Order\ndirect exchange}}
    D --> Q3[(FinallyOrder queue)]
    Q3 --> C[CalculateOrderFee\nconsumer]
Loading

End-to-end sequence

  1. RegisterUser asks for a phone number and email address.
  2. It creates a User object and serializes it to JSON.
  3. The payload is published to the User_Register fanout exchange. RabbitMQ delivers a copy to both SendEmail and SendSMS queues.
  4. The same payload is published to the Users-Order direct exchange with the FinallyOrder routing key. RabbitMQ routes it to the FinallyOrder queue.
  5. SendEmailProject, SendSMSProject, and CalculateOrderFee each consume their own message and write the simulated action to the console.
  6. Every consumer sends a manual acknowledgement after processing its delivery.

Projects

Project Role Responsibility
RegisterUser Producer Reads registration data from the console and publishes the JSON payload to the fanout and direct exchanges.
SendEmailProject Consumer Receives fanout events from the SendEmail queue and simulates sending an email.
SendSMSProject Consumer Receives fanout events from the SendSMS queue and simulates sending an SMS.
CalculateOrderFee Consumer Receives direct-routed messages from the FinallyOrder queue and simulates order-fee handling.
Util Shared library Defines the shared User message contract used by all executables.

The solution file is RabbitMq.slnx.

Technology Stack

Technology Purpose
C# Application language
.NET 10 Target framework for all projects
RabbitMQ AMQP message broker
RabbitMQ.Client 7.2.1 Official RabbitMQ .NET client
Newtonsoft.Json 13.0.4 JSON serialization and deserialization

Prerequisites

Before running the sample, install or have access to:

The applications use RabbitMQ's local default connection details:

Setting Value
Host localhost
Port 5672
Username guest
Password guest

Note: RabbitMQ restricts the default guest account to local connections. These defaults are appropriate for local learning only; use a dedicated, least-privilege account and secure configuration in deployed environments.

Quick Start

1. Clone the repository

git clone https://github.com/MohammadHasanp/RabbitMq.git
cd RabbitMq

2. Start RabbitMQ

If RabbitMQ is not already available locally, start it with the management plugin enabled:

docker run -d \
  --name rabbitmq \
  -p 5672:5672 \
  -p 15672:15672 \
  rabbitmq:3-management

The management dashboard will be available at http://localhost:15672. Sign in locally with guest / guest.

3. Restore and build

dotnet restore RabbitMq.slnx
dotnet build RabbitMq.slnx

4. Start the consumers

Open three terminals and keep each consumer running before sending a registration event:

# Terminal 1
dotnet run --project SendEmailProject/SendEmailProject.csproj
# Terminal 2
dotnet run --project SendSMSProject/SendSMSProject.csproj
# Terminal 3
dotnet run --project CalculateOrderFee/CalculateOrderFee.csproj

5. Start the producer

In a fourth terminal, run:

dotnet run --project RegisterUser/RegisterUser.csproj

Enter the requested phone number and email address. When prompted with Send Again?, enter y to publish another event or any other value to exit the publishing loop.

RabbitMQ Topology

The topology is declared by the applications at startup. The exchanges and queues below are durable, so their definitions survive a broker restart.

Type Name Durable Binding / routing behavior
Fanout exchange User_Register Yes Broadcasts each registration event to all bound queues.
Direct exchange Users-Order Yes Routes messages matching the FinallyOrder routing key.
Queue SendEmail Yes Bound to User_Register; consumed by SendEmailProject.
Queue SendSMS Yes Bound to User_Register; consumed by SendSMSProject.
Queue FinallyOrder Yes Bound to Users-Order with the FinallyOrder routing key; consumed by CalculateOrderFee.

Durable infrastructure does not by itself make published messages persistent. The current sample focuses on exchange and queue routing; it does not set persistent message properties.

Message Contract

All applications reference the Util project and use the following JSON-compatible contract:

public class User
{
    public string Phone { get; set; }
    public string Email { get; set; }
}

A typical payload is:

{
  "Phone": "+1-555-0100",
  "Email": "user@example.com"
}

Configuration

Connection settings and topology names are currently defined directly in each application's Program.cs file.

var factory = new ConnectionFactory
{
    HostName = "localhost",
    UserName = "guest",
    Password = "guest"
};

For another broker, update the host and credentials consistently in all four executable projects. For a production-ready application, move connection details, exchange names, and queue names to configuration or environment variables rather than committing credentials to source code.

Expected Output

Given a registration with phone +1-555-0100 and email user@example.com, the consumers print messages similar to:

Send Email =>user@example.com
Send Message =>+1-555-0100
Calculate Order =>+1-555-0100 + user@example.com

The order in which these lines appear is not guaranteed because each consumer runs independently and processes messages asynchronously.

Development

Build the full solution

dotnet build RabbitMq.slnx

Run an individual project

dotnet run --project RegisterUser/RegisterUser.csproj
dotnet run --project SendEmailProject/SendEmailProject.csproj
dotnet run --project SendSMSProject/SendSMSProject.csproj
dotnet run --project CalculateOrderFee/CalculateOrderFee.csproj

Repository layout

RabbitMq/
├── CalculateOrderFee/     # Direct-exchange consumer
├── RegisterUser/          # Console producer
├── SendEmailProject/      # Fanout consumer for email notifications
├── SendSMSProject/        # Fanout consumer for SMS notifications
├── Util/                  # Shared User contract
├── RabbitMq.slnx          # Solution definition
└── README.md              # Project documentation

Current Scope and Production Considerations

This repository is intentionally focused on illustrating RabbitMQ routing patterns in concise console applications. It is a learning sample, not a complete production messaging platform.

Before using this approach in production, consider adding:

  • Configuration through environment variables, secret storage, or application settings
  • Input validation and message schema/version management
  • TLS, non-default credentials, and restricted RabbitMQ permissions
  • Persistent messages when delivery across broker restarts is required
  • Retry policies, dead-letter exchanges, and poison-message handling
  • Structured logging, health checks, monitoring, and tracing
  • Graceful shutdown and connection/channel lifecycle management
  • Idempotent consumers to safely handle redelivery
  • Automated unit, integration, and broker-backed tests

Author

Created and maintained by MohammadHasanp.

About

A practical .NET 10 messaging sample demonstrating RabbitMQ publish/subscribe and direct routing patterns with independent producers and consumers.

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages