FastCSharp6.RabbitPublisher 1.4.0

dotnet add package FastCSharp6.RabbitPublisher --version 1.4.0
NuGet\Install-Package FastCSharp6.RabbitPublisher -Version 1.4.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="FastCSharp6.RabbitPublisher" Version="1.4.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add FastCSharp6.RabbitPublisher --version 1.4.0
#r "nuget: FastCSharp6.RabbitPublisher, 1.4.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// 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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.4.0 169 12/17/2023
1.3.3 78 12/15/2023
1.3.1 83 12/15/2023
1.3.0 90 12/12/2023
1.2.0 98 12/7/2023