https://github.com/jasdvl/sample-aspnetcore-rabbitmq
Sample integration of RabbitMQ in ASP.NET using YAML for configuration management
https://github.com/jasdvl/sample-aspnetcore-rabbitmq
angular aspnetcore csharp rabbitmq signalr yaml
Last synced: 2 days ago
JSON representation
Sample integration of RabbitMQ in ASP.NET using YAML for configuration management
- Host: GitHub
- URL: https://github.com/jasdvl/sample-aspnetcore-rabbitmq
- Owner: jasdvl
- License: mit
- Created: 2024-10-07T22:14:05.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2025-02-18T23:01:42.000Z (about 1 year ago)
- Last Synced: 2025-06-14T15:06:00.789Z (10 months ago)
- Topics: angular, aspnetcore, csharp, rabbitmq, signalr, yaml
- Language: C#
- Homepage:
- Size: 970 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# RabbitMQ ASP.NET Core Integration Sample

## Screenshots
Check out the [screenshots](./screenshots.md) for a visual overview.
## Project Overview
This repository demonstrates how to integrate RabbitMQ into ASP.NET applications by utilizing both message publishers and consumers, with a focus on configuring RabbitMQ for client-side usage.
The solution includes multiple projects to simulate workflows in a public library with RabbitMQ as the message broker. Additionally, it showcases how to manage RabbitMQ client configurations through a centralized `YAML` file embedded as a resource.
**Note:**
> This project is still under development. Features and functionalities may be added or changed in future updates.
> The codebase may undergo structural improvements in future updates.
> See the [TODO List](#todo-list) for more details on planned improvements.
> Please note that time constraints might delay updates to the TODO list.
### Key Components
1. **PubLib.Messaging.RabbitMQ**
- This project defines RabbitMQ consumers and publishers.
- The RabbitMQ client configuration is managed via an embedded `rabbitmq-config.yml` file, which contains settings for exchanges, queues, bindings, and publishers/consumers.
This setup is specifically for configuring the client-side interaction with RabbitMQ and does not involve server configuration.
#### Example of the `rabbitmq-config.yml` file used for client configuration:
```yaml
RabbitMQ:
HostName: localhost
Port: 5672
UserName: guest
Password: guest
Exchanges:
- Name: membership-status-exchange
Type: topic
Durable: true
AutoDelete: false
Publishers:
- MembershipStatusPublisher
- Name: book-order-exchange
Type: topic
Durable: true
AutoDelete: false
Publishers:
- BookOrderPublisher
Queues:
- Name: membership-status-queue
Durable: true
Exclusive: false
AutoDelete: false
Consumers:
- MembershipStatusConsumer
- Name: book-reservation-queue
Durable: true
Exclusive: false
AutoDelete: false
Consumers:
- BookReservationConsumer
- Name: book-provision-queue
Durable: true
Exclusive: false
AutoDelete: false
Consumers:
- BookProvisionConsumer
Bindings:
- Queue: membership-status-queue
Exchange: membership-status-exchange
BindingKey: "membership.status.*"
- Queue: book-reservation-queue
Exchange: book-order-exchange
BindingKey: "book.reservation.*"
- Queue: book-provision-queue
Exchange: book-order-exchange
BindingKey: "book.provision.*"
```
- Inject the configuration into other projects via the `AddRabbitMQConfig()` extension method:
```csharp
public static class ConfigurationExtensions
{
public static IConfigurationBuilder AddRabbitMQConfig(this IConfigurationBuilder builder)
{
return builder.AddYamlResource("PubLib.Messaging.RabbitMQ.rabbitmq-config.yml");
}
}
```
This method enables other projects to directly load project-specific RabbitMQ settings without handling resource paths manually.
2. **publib.frontdesk.client (Angular)**
- This Angular client allows users to apply for library membership and reserve books once registered.
- Book reservations and other actions are communicated to the ASP.NET Core backend through a REST API.
- The client also displays real-time notifications from the ASP.NET backend using SignalR.
3. **PubLib.FrontDesk.Server (ASP.NET Core)**
- The backend for the Angular client, exposing REST endpoints to process membership applications and book reservations.
- Publishes these requests as messages to RabbitMQ in JSON format.
- Also contains a consumer that listens for notifications (e.g., book ready for pickup) from RabbitMQ and forwards them to the Angular client via SignalR.
4. **PubLib.BackOffice (Blazor)**
- A Blazor Web App (interactive SSR) for managing library operations.
- It allows the Back Office to notify the Front Desk (via RabbitMQ) when a reserved book is ready for pickup.
- Contains RabbitMQ consumers that receive membership applications and book reservations from the Front Desk to process and display within the Blazor application.
## Prerequisites
- [.NET 9 SDK](https://dotnet.microsoft.com/download/dotnet/9.0)
- [Node.js](https://nodejs.org/en/download/) for the Angular client
- [Docker](https://www.docker.com/) for running RabbitMQ
- [RabbitMQ](https://www.rabbitmq.com/download.html)
## Setting up the SSL/TLS Certificates (Windows)
The developer certificates should be automatically created when starting the solution in Visual Studio. If this does not happen, or if the browser does not recognize the existing certificate as valid, please try the following:
### 1. Angular Development Server
- If ```%APPDATA%\\ASP.NET\\https\\publib.frontdesk.client.pem``` exists, it must be added to the list of Trusted Root Certification Authorities. Run the following command as an administrator:
```bash
certutil -addstore -f "Root" %APPDATA%\\ASP.NET\\https\\publib.frontdesk.client.pem
```
- If ```%APPDATA%\\ASP.NET\\https\\publib.frontdesk.client.pem``` does not exist, you can create a developer certificate as follows:
```bash
openssl req -x509 -nodes -days 365 -newkey rsa:2048 ^
-keyout %APPDATA%\\ASP.NET\\https\\publib.frontdesk.client.key ^
-out %APPDATA%\\ASP.NET\\https\\publib.frontdesk.client.pem ^
-subj "/CN=localhost" -addext "subjectAltName=IP:127.0.0.1"
```
Afterward, it must be added to the list of Trusted Root Certification Authorities. To do this, run the following command with administrator privileges:
```bash
certutil -addstore -f "Root" %APPDATA%\\ASP.NET\\https\\publib.frontdesk.client.pem
```
### 2. Blazor Web App
You can regenerate the developer certificates as follows:
```bash
dotnet dev-certs https --clean
dotnet dev-certs https --trust
```
## Getting Started
### 1. Start the RabbitMQ Server
To start a RabbitMQ server instance using Docker, run the following command:
```bash
docker run -it --rm --name rabbitmq -p 5672:5672 -p 15672:15672 rabbitmq:3.13-management
```
Alternatively, you can explore other installation options [here](https://www.rabbitmq.com/docs/download).
### 2. Clone the Repository
```bash
git clone https://github.com/jasdvl/sample-aspnetcore-rabbitmq.git
```
### 3. Run the Projects
Depending on whether you're running the solution in Visual Studio or from the command line, follow the steps below:
#### **3.1 Running the Solution in Visual Studio**
After downloading and opening the solution in Visual Studio, it is recommended to configure multiple startup projects to run the application efficiently.
While it's possible to start each project individually by setting it as the startup project, the most convenient approach is to configure multiple startup projects, so that all necessary services are launched simultaneously.
Follow these steps to set up the three relevant projects as startup projects.
**Steps to Configure Multiple Startup Projects in Visual Studio:**
1. **Open the Solution:**
After downloading and extracting the solution from GitHub, open the `.sln` file in Visual Studio.
2. **Access Startup Project Settings:**
- In **Solution Explorer**, right-click on the solution node (at the top).
- Select **Set Startup Projects...** from the context menu.
3. **Select Multiple Startup Projects:**
- In the dialog that appears, choose **Multiple startup projects**.
- For each of the following projects, select **Start** from the drop-down menu next to the project name:
- PubLib.BackOffice
- PubLib.FrontDesk.Server
- publib.frontdesk.client
4. **Save Changes:**
Click **OK** to save your changes.
Now, all three projects will be configured to start together when you run the solution.
**Note:**
> If you're unfamiliar with how to set multiple startup projects, you can check the
> official [Visual Studio documentation](https://learn.microsoft.com/en-us/visualstudio/ide/how-to-set-multiple-startup-projects?view=vs-2022).
---
#### **3.2 Running the Solution from the Command Line**
You can also run the projects manually from the command line. Follow the instructions below to run each part of the application.
First, navigate to the project directory:
```bash
cd sample-aspnetcore-rabbitmq/src
```
**1. Running the Back Office (Blazor Web App)**
Navigate to the Blazor Web App directory and run the application:
```bash
cd PubLib.BackOffice
dotnet run
```
**2. Running the Front Desk (Angular & ASP.NET Backend)**
First, start the ASP.NET backend for publishing messages to RabbitMQ:
```bash
cd ..\PubLib.FrontDesk.Server
dotnet run
```
Then, start the Angular frontend by navigating to the client directory and running the following commands:
```bash
cd ..\publib.frontdesk.client
npm install
npm start
```
**3. Sending and Viewing Messages**
- Use the **PubLib.FrontDesk.Client** to send membership applications and book reservations via the REST API.
- These requests will be published to RabbitMQ.
- The **PubLib.BackOffice** will consume these messages and display them in real time.
- The **PubLib.BackOffice** can also notify the Front Desk when a reserved book is ready for pickup.
This notification is sent via RabbitMQ and then forwarded as a SignalR message from the **PubLib.FrontDesk.Server** to the Angular client.
## TODO List
### Priority 1
- Fix compiler warnings
- Correct or adjust inconsistent or incorrect namespaces
- Add XML comments
- ~~Replace events with Channel\ (superior asynchronous messaging with built-in backpressure and concurrent producer-consumer support.)~~
### Priority 2
- Refactor code
### Priority 3
These items are considered "nice to have" and would enhance the system's capabilities. However, I’m not sure when I will be able to address them due to time constraints:
- Implement RPC (Remote Procedure Call) pattern for request/response handling
- Add message prioritization to queues
- Implement Dead Letter Queues (DLQ) for failed message handling
- Add message TTL (Time-to-Live) for automatic message expiration
- Implement delayed messaging for deferred processing
- Use Quorum Queues for higher availability and reliability
## Branching Strategy
Since I am the sole developer on this project, I primarily work on the `main` branch. I prefer to keep things simple by committing directly to `main` for most tasks. However, if a new feature requires multiple related commits or substantial changes, I will create feature branches to manage those updates. Once the feature is complete, the branch will be merged back into `main`. My goal is to keep the main branch stable and up to date.