CleanTemplate.Messaging
8.0.2
dotnet add package CleanTemplate.Messaging --version 8.0.2
NuGet\Install-Package CleanTemplate.Messaging -Version 8.0.2
<PackageReference Include="CleanTemplate.Messaging" Version="8.0.2" />
paket add CleanTemplate.Messaging --version 8.0.2
#r "nuget: CleanTemplate.Messaging, 8.0.2"
// Install CleanTemplate.Messaging as a Cake Addin #addin nuget:?package=CleanTemplate.Messaging&version=8.0.2 // Install CleanTemplate.Messaging as a Cake Tool #tool nuget:?package=CleanTemplate.Messaging&version=8.0.2
Clean Architecture Base Template for Messaging <img src="../icon.png" height="40" width="40"/>
This package is a Clean Architecture Base Template comprising all Baseic and Abstract and Contract types for Messaging between Microservices.
At this moment, we only use RabbitMQ
for Messaging in this package.
This package is one of the Cleantemplate side packages which provides the basic requirements for Calling Microservices using Messaging
.
The .Net version used in this project is net8.0
<p align="center" width="100%"> <img src="../icon.png" height="128" width="128"/> </p>
Contents
Dependencies
net8.0
- CleanTemplate (>= 7.5.0)
- RabbitMQ.Client (>= 6.8.1)
Installation
.Net CLI
dotnet add package CleanTemplate.Messaging --version x.x.x
Package Manager
NuGet\Install-Package CleanTemplate.Messaging -Version x.x.x
Package Reference
<PackageReference Include="CleanTemplate.Messaging" Version="x.x.x" />
Paket CLI
paket add CleanTemplate.Messaging --version x.x.x
Script & Interactive
#r "nuget: CleanTemplate.Messaging, x.x.x"
Cake
// Install CleanTemplate.Messaging as a Cake Addin
#addin nuget:?package=CleanTemplate.Messaging&version=x.x.x
// Install CleanTemplate.Messaging as a Cake Tool
#tool nuget:?package=CleanTemplate.Messaging&version=x.x.x
Usage
I tried to follow the basic format and structure of CleanTemplate
here.
For use of this package we need three project: 1- A Publisher Web API project as First Microservice: Microservice1 2- A Subscriber Web API project as Second Microservice: Microservice2 3- A Contract ClassLibrary Project: Contracts
Dependency Injection and Add Messaging to the projects
To use from CleanTemplate.Messaging
in your project you should first register it in Program.cs
of Publisher and Subscriber projects as below:
1 - Add below Extension Method (AddCleanMessaging(Assembly assembly)
) to Program.cs
of both Publisher and Subscriber projects:
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddCleanMessaging(assemblyOfYourPub/SubTypes)
var app = builder.Build();
app.Run();
2 - Also you should Add a DIModule
To your Contracts Project and Register it inside of both Publisher and Subscriber Projects.
// DIModule inside of your Contracts Project.
public class ContractDIModule : CleanBaseDIModule
{
}
// Register ContractDIModule inside of ClientWebAPI Program.cs
builder.Services.AddCleanTemplateDIModule<ContractDIModule>();
3 - Also you should add below section in appsettings.json
of both Publisher and Subscriber to configuring your RabbitMQ Connection Host.
"CleanRabbitMqHostSetting": {
"HostName": "localhost",
"UserName": "guest",
"Password": "guest"
}
Create Contract
You should create a class inside of your Contracts project for your RabbitMQ Message from Publisher to Subscriber in which inherit from CleanBaseRabbitMqMessage
.
Also you should create a Setting type inside of your Contracts project in which inheriting from CleanBaseRabbitMqSetting
for configuring your RabbitMQ Exchange, Queue, ExchangeType, ... .
public class Service1To2Message : CleanBaseRabbitMqMessage
{
// Some properties here
}
public class Service1To2Settings : CleanBaseRabbitMqSetting
{
public Service1To2Settings(IConfiguration configuration) : base(configuration)
{
}
}
Configuring RabbitMQ Settings
You should configuring RabbitMQ settings for both Publisher and Subscriber in their appsettings.json
files.
Notable Point Just Note that your Section Name in appsettings.json
should be equal to your Setting Class Name.
1 - For your Publisher:
"Service1To2Settings": {
"QueueName": "MyQueueName",
"ExchangeName": "MyExchangeName",
"ExchangeType" : "Direct" or "Topic" or "Fanout" or "Headers",
"RoutingKeies": [
"rk1",
"rk2",
.
.
.
"rkn"
]
}
2 - For your Subscriber:
"Service1To2Settings": {
"QueueName": "MyQueueName",
"ExchangeName": "MyExchangeName",
"ExchangeType" : "Direct" or "Topic" or "Fanout" or "Headers",
"RoutingKeies": [
"rk1",
"rk2",
.
.
.
"rkn"
]
}
Create Publisher
Create a publishr class In your Publisher project in which inheriting from CleanBaseRabbitMqPublisher<TMessage, TSetting>
.
public class Service1To2Publisher : CleanBaseRabbitMqPublisher<Service1To2Message, Service1To2Settings>
{
public Service1To2Publisher(ICleanRabbitMqConnectionFactory connectionFactory, Service1To2Settings settings)
: base(connectionFactory, settings)
{
}
}
Create Subscriber
Create a subscriber class In your Subscriber project in which inheriting from CleanBaseRabbitMqSubscriber<TMessage, TSetting>
.
public class Service1To2Subscriber : CleanBaseRabbitMqSubscriber<Service1To2Message, Service1To2Settings>
{
public Service1To2Subscriber(ICleanRabbitMqConnectionFactory connectionFactory, Service1To2Settings settings)
: base(connectionFactory, settings)
{
}
protected override async Task HandleAsync(Service1To2Message message)
{
await Console.Out.WriteLineAsync($"I Am Service2 Subscriber: Publisher Sent below data:\n{new { message.Name, message.Age }}");
}
}
Using the Publisher for Publishing Message
Finally to Sending Message from Publisher Microservice To Subscriber Microservice with RabbitMQ Message Broker, you should inject the publisher you created, into the controllers or services of the Publisher.
[Route("api/[controller]")]
[ApiController]
public class Service1Controller : ControllerBase
{
private readonly Service1To2Publisher _publisher;
public Service1Controller(Service1To2Publisher publisher)
{
_publisher = publisher;
}
[HttpGet]
public IActionResult SendMessage()
{
_publisher.PublishAsync(new Service1To2Message
{
// property initialization
});
return Ok();
}
}
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
-
net8.0
- CleanTemplate (>= 8.0.2)
- RabbitMQ.Client (>= 6.8.1)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
- Global Exception Handling For RabbitMq Messages added
- Bug fix and code cleanup