FastCSharp.RabbitPublisher 1.0.1

There is a newer version of this package available.
See the version list below for details.
dotnet add package FastCSharp.RabbitPublisher --version 1.0.1                
NuGet\Install-Package FastCSharp.RabbitPublisher -Version 1.0.1                
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="FastCSharp.RabbitPublisher" Version="1.0.1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add FastCSharp.RabbitPublisher --version 1.0.1                
#r "nuget: FastCSharp.RabbitPublisher, 1.0.1"                
#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 FastCSharp.RabbitPublisher as a Cake Addin
#addin nuget:?package=FastCSharp.RabbitPublisher&version=1.0.1

// Install FastCSharp.RabbitPublisher as a Cake Tool
#tool nuget:?package=FastCSharp.RabbitPublisher&version=1.0.1                

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 net7.0 is compatible.  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.5.0 537 1/5/2024
1.4.0 156 12/17/2023
1.0.1 157 11/28/2023
1.0.0 158 10/20/2023
0.4.3-alpha 102 9/25/2023
0.4.2-alpha 116 9/23/2023
0.4.1-alpha 99 9/23/2023
0.3.1-alpha 113 9/22/2023
0.0.1-alpha 113 9/18/2023
0.0.0-alpha 129 4/9/2023