FastCSharp6.RabbitPublisher
1.4.0
dotnet add package FastCSharp6.RabbitPublisher --version 1.4.0
NuGet\Install-Package FastCSharp6.RabbitPublisher -Version 1.4.0
<PackageReference Include="FastCSharp6.RabbitPublisher" Version="1.4.0" />
paket add FastCSharp6.RabbitPublisher --version 1.4.0
#r "nuget: FastCSharp6.RabbitPublisher, 1.4.0"
// Install FastCSharp6.RabbitPublisher as a Cake Addin #addin nuget:?package=FastCSharp6.RabbitPublisher&version=1.4.0 // Install FastCSharp6.RabbitPublisher as a Cake Tool #tool nuget:?package=FastCSharp6.RabbitPublisher&version=1.4.0
FastCSharp's RabbitMQ Publisher
RabbitPublisher provides a simple approach for publishing messages to a RabbitMQ exchange.
It is a wrapper around the RabbitMQ.Client library.
Batch Publishing
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.
Usage
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.
Dependency injection Example (Check out BasicPublisher project at FastCSharp.TestRabbitImpl for full project)
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.direct
.
Run the code.
dotnet run
Execute 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.
Example using new (Check out BasicPublisher project at FastCSharp.TestRabbitImpl for full project)
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.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;
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 run
Execute 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.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. 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. |
-
net6.0
- FastCSharp6.Pool (>= 1.2.0)
- FastCSharp6.Publisher (>= 2.2.0)
- FastCSharp6.RabbitCommon (>= 1.1.0)
- Microsoft.Extensions.Configuration (>= 7.0.0)
- Microsoft.Extensions.Configuration.Binder (>= 7.0.3)
- Microsoft.Extensions.Configuration.Json (>= 7.0.0)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 7.0.0)
- Microsoft.Extensions.Logging (>= 7.0.0)
- RabbitMQ.Client (>= 6.5.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.