Package containing RabbitMQ simplified Publisher.
$ dotnet add package FastCSharp.RabbitPublisherRabbitPublisher provides a simple approach for publishing messages to a RabbitMQ exchange.
It is a wrapper around the RabbitMQ.Client library.
It includes an implementation for batch publishing. Send an IEnumerable of messages and they will be published in a single batch. This is useful when you need to publish a large number of messages with very high throughput.
All you need to do is create a new publisher to an existing exchange and publish a message.
The example below shows how to publish a message to a direct exchange. Swagger is also configured to allow testing.
The code needed to run is manly in the Runner class.
Checkout FastCSharp.TestRabbitImpl project for a more complete example.
Create a new minimal API project.
dotnet new web -o BasicPublisher
cd .\BasicPublisher\
dotnet add package FastCSharp.RabbitPublisher
Add the following configuration to appsettings.json.
"RabbitPublisherConfig" : {
"Timeout" : "00:00:10",
"Exchanges" :
{
"DIRECT_EXCHANGE" : {
"Name" : "amq.direct",
"Type" : "direct"
}
}
}
The file should look something like this.
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*",
"RabbitPublisherConfig" : {
"Timeout" : "00:00:10",
"Exchanges" :
{
"DIRECT_EXCHANGE" : {
"Name" : "amq.direct",
"Type" : "direct"
}
}
}
}
Open Program.cs and replace the code with the one below.
using FastCSharp.Publisher;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddRabbitPublisher<string>(builder.Configuration);
var app = builder.Build();
app.MapGet("/", async (string message, IRabbitPublisher<string> publisher) => {
return await publisher.ForExchange("DIRECT_EXCHANGE").Publish(message);
});
app.Run();
Create a queue (e.g. named "test") in RabbitMQ and bin it to the exchange .
amq.directRun the code.
dotnet runExecute the endpoint with a message visiting the created endpoint (replace the 5033 port for the one configured in your API).
http://localhost:5033/?message=Hello%20World
That's it. You should see the message in the queue.
We will use the same configuration as the previous example.
Create a new minimal API project.
dotnet new web -o BasicPublisher
cd .\BasicPublisher\
dotnet add package FastCSharp.RabbitPublisherAdd the following configuration to appsettings.json.
"RabbitPublisherConfig" : {
"Timeout" : "00:00:10",
"Exchanges" :
{
"DIRECT_EXCHANGE" : {
"Name" : "amq.direct",
"Type" : "direct"
}
}
}The file should look something like this.
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*",
"RabbitPublisherConfig" : {
"Timeout" : "00:00:10",
"Exchanges" :
{
"DIRECT_EXCHANGE" : {
"Name" : "amq.direct",
"Type" : "direct"
}
}
}
}Open Program.cs and replace the code with the one below.
using FastCSharp.Publisher;
using FastCSharp.RabbitPublisher.Common;
using FastCSharp.RabbitPublisher.Impl;
using FastCSharp.RabbitPublisher.Injection;
var builder = WebApplication.CreateBuilder(args);
RabbitPublisherConfig config = new();
builder.Configuration.GetSection(RabbitOptions.SectionName).Bind(config);
ILoggerFactory loggerFactory = LoggerFactory.Create(builder => builder.AddConsole());
var connectionPool = new RabbitConnectionPool(config, loggerFactory);
var app = builder.Build();
app.MapGet("/", async (string message) => {
IRabbitPublisher<string> publisher = new RabbitPublisher<string>(connectionPool, loggerFactory, config);
return await publisher.ForExchange("DIRECT_EXCHANGE").Publish(message);
});
app.Run();Create a queue (e.g. named "test") in RabbitMQ and bin it to the exchange amq.direct.
Run the code.
dotnet runExecute the endpoint with a message visiting the created endpoint (replace the 5033 port for the one configured in your API).
http://localhost:5033/?message=Hello%20World
That's it. You should see the message in the queue.