FastCSharp6.RabbitSubscriber 1.1.0

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

// Install FastCSharp6.RabbitSubscriber as a Cake Tool
#tool nuget:?package=FastCSharp6.RabbitSubscriber&version=1.1.0                

FastCSharp's RabbitMQ Subscriber

RabbitSubscriber provides a simple approach for subscribing to a RabbitMQ queue.
It is a wrapper around the RabbitMQ.Client library.

Usage

All you need to do is create a new subscriber to an existing queue and register a callback.

Minimal Example (Check out BasicSubscriber project at FastCSharp.TestRabbitImpl for more complex project examples)

Create a new minimal console project.

dotnet new console -o BasicSubscriber
cd .\BasicSubscriber\
dotnet add package FastCSharp.RabbitSubscriber
dotnet add package Microsoft.Extensions.Configuration.Json
dotnet add package Microsoft.Extensions.Logging.Console

Open the BasicSubscriber.csproj file and add the following configuration.

  <ItemGroup>
    <None Update="appsettings.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
  </ItemGroup>

Create a json file named appsettings.json and add the following configuration.

{
    "RabbitSubscriberConfig" : 
    {
        "HostName"  : "localhost",
        "Port"      : 5672,
        "VirtualHost": "test-vhost",
        "UserName"  : "guest",
        "Password"  : "guest",
        "HeartbeatTimeout"  : "00:00:20",
        "Queues"    :
        {
            "DIRECT_QUEUE"    : 
            {
                "Name":"test.direct.q",
                "PrefetchCount":1,
                "PrefetchSize":0
            }
        }
    }
}

Go to the Rabbit Management UI and create a new queue named test.direct.q. You may bind it to the amq.direct exchange if you want to publish messages with a Publisher.

Open the Program.cs file and replace the code with the one below.

using FastCSharp.RabbitSubscriber;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;

ILoggerFactory loggerFactory = LoggerFactory.Create(builder => builder.AddConsole());
var logger = loggerFactory.CreateLogger("Program");
IConfiguration defaultConfiguration = new ConfigurationBuilder()
    .AddJsonFile("rabbitsettings.CLUSTER.json", true, true)
    .Build();

var subscriberFactory = new RabbitSubscriberFactory(defaultConfiguration, loggerFactory);
using var directSubscriber = subscriberFactory.NewSubscriber<string>("DIRECT_QUEUE");
directSubscriber.Register(async (message) =>
{
    logger.LogInformation($"Received {message}");
    return await Task.FromResult(true);
});

logger.LogInformation(" Press [enter] to exit.");
Console.ReadLine();

All set. Run the project:

dotnet run

Now you can go to the Rabbit Management UI and publish a message to the test.direct.q queue. Remember to enclose the message in double quotes (e.g. "Hello World").

Adding a Circuit Breaker

Add the following packages to the project:

dotnet add package FastCSharp.CircuitBreaker

Open the Program.cs file and replace the code with the one below.

using FastCSharp.CircuitBreaker;
using FastCSharp.RabbitSubscriber;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;

ILoggerFactory loggerFactory = LoggerFactory.Create(builder => builder.AddConsole());
var logger = loggerFactory.CreateLogger("Program");
IConfiguration defaultConfiguration = new ConfigurationBuilder()
    .AddJsonFile("appsettings.json", true, true)
    .Build();

var circuit = new EventDrivenCircuitBreaker(
    new ConsecutiveFailuresBreakerStrategy(
        5, 
        new FixedBackoff(new TimeSpan(0, 0, 0, 0, 5))));

var subscriberFactory = new RabbitSubscriberFactory(defaultConfiguration, loggerFactory);
using var directSubscriber = subscriberFactory.NewSubscriber<string>("DIRECT_QUEUE");

circuit.OnOpen += (sender) => directSubscriber.UnSubscribe();
circuit.OnReset += (sender) => directSubscriber.Reset();

directSubscriber.Register(async (message) =>
{
    return await circuit.Wrap(async () =>
    {
        logger.LogInformation($"Received {message}");
        return await Task.FromResult(true);
    });
});

logger.LogInformation(" Press [enter] to exit.");
Console.ReadLine();

Breaking down subscriber flux control

The subscriber can be stopped by calling:

subscriber.UnSubscribe();

This is a useful callback when a circuit breaker is triggered and the OnOpen or OnBreak events are fired.

The subscriber can be reset by calling:

subscriber.Reset();

This is a useful callback when a circuit breaker is triggered and the OnReset event is fired.

Check the FastCSharp.CircuitBreaker package for more information on circuit breakers.

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
2.1.0 203 12/19/2023
2.0.0 134 12/17/2023
1.1.0 122 12/12/2023
1.0.0 141 12/7/2023