.NET 8 Web App, Web API and RabbitMQ

Original article by Fauly Coelho link

Dear reader,

In this article we will explore the advantage of using RabbitMQ, benefits from greater scalability, reliability, and performance, while maintaining a clear separation of responsibilities between the different components of the application. Moreover, this project adheres to clean architecture principles and implements health checks to ensure the robustness of our services.

Introduction

  • Credit Card Transaction Process:

The web project, developed with .NET 8 and C#, is responsible for receiving user credit card transaction data. These data are then validated and stored in the database for transaction logging. After the insertion into the database, the web application sends a message to RabbitMQ informing the ID of the new transaction that needs to be processed.

  • Payment Processing API:

The API is responsible for receiving messages from RabbitMQ containing the IDs of transactions to be processed.
Upon receiving a message, the API queries the database to retrieve the transaction details associated with the ID.
After obtaining the transaction details, the API executes the payment processing, using the credit card information and interacting with the corresponding payment system.

Pre-Requirements:

Before you begin, make sure you have the following software installed on your machine

Visual Studio Community

.NET 8 SDK

Docker

Let’s create our containers to support the stack.

Create a new container with RabbitMQ:

docker run -d --name rabbitmq -p 5672:5672 -p 15672:15672 -e RABBITMQ_DEFAULT_USER=guest -e RABBITMQ_DEFAULT_PASS=guest rabbitmq:3-management

Create a new container with SQL Server DB:

docker run -d --name meu-sql-server -p 1433:1433 -e "ACCEPT_EULA=Y" -e "SA_PASSWORD=SuaSenhaSegura" -v /caminho/para/data:/var/opt/mssql mcr.microsoft.com/mssql/server:2019-latest

Clone the following repository: https://github.com/faulycoelho/article-web-api-rabbitmq

git clone https://github.com/faulycoelho/article-web-api-rabbitmq
cd .\article-web-api-rabbitmq\

Use the Package Manager Console to create the tables:

update-database

After that, run both projects, Web and Api.

In this point you should have access to the project and they should have ‘Healthy’ response:

Web: http://localhost:5101/health

API: http://localhost:5120/health

Expected response:

{
"status": "Healthy",
"totalDuration": "00:00:00.2439938",
"entries": {
"rabbitmq-check": {
"data": {},
"duration": "00:00:00.0169928",
"status": "Healthy",
"tags": [
"rabbitmq"
]
},
"sqlserver-check": {
"data": {},
"duration": "00:00:00.2411064",
"status": "Healthy",
"tags": [
"sqlserver"
]
}
}
}

Now, access the payment page and create a new payment:

http://localhost:5101/

The web application is responsible for persisting transaction data into the database and signaling a new payment transaction that needs processing by sending a message to RabbitMQ

After that, a API’s background service listens for messages on RabbitMQ and processes them. This processing is handled within the PaymentWorker.cs class.

Exchange

Conclusion

Advantages of Using RabbitMQ:

Instead of processing the payment directly in the web application, a more scalable and robust approach would be to use a message queue like RabbitMQ.

By adopting RabbitMQ, the web application only sends a message to the queue indicating the need to process a transaction, without worrying about the processing itself.

This separates the transaction process from the web application, making it lighter, more scalable, and agile since it doesn’t have to deal with payment processing. If the credit card is declined, if the payment gateway is offline, if there’s a network issue, or if you’re experiencing a high volume of transactions, you can handle them later without impacting your web application and user experience.

Additionally, the use of a message queue offers greater reliability and resilience to the system, as messages can be re-queued in case of processing failure, ensuring that no transaction is lost.

You can find that project and more in my Github:

https://github.com/faulycoelho/article-web-api-rabbitmq

Commentaires

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *

Français