RDC23.EventBus.RabbitMQ 3.0.1

dotnet add package RDC23.EventBus.RabbitMQ --version 3.0.1
                    
NuGet\Install-Package RDC23.EventBus.RabbitMQ -Version 3.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="RDC23.EventBus.RabbitMQ" Version="3.0.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="RDC23.EventBus.RabbitMQ" Version="3.0.1" />
                    
Directory.Packages.props
<PackageReference Include="RDC23.EventBus.RabbitMQ" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add RDC23.EventBus.RabbitMQ --version 3.0.1
                    
#r "nuget: RDC23.EventBus.RabbitMQ, 3.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.
#:package RDC23.EventBus.RabbitMQ@3.0.1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=RDC23.EventBus.RabbitMQ&version=3.0.1
                    
Install as a Cake Addin
#tool nuget:?package=RDC23.EventBus.RabbitMQ&version=3.0.1
                    
Install as a Cake Tool

Configuration

Program.cs

In the program.cs configures RabbitMQ connection settings and registers services for dependency injection, including an EventBusSender and YourListeners hosted service instances.

builder.Services.AddSingleton(new RabbitMQConnectionFactory("rabbit-mq", "username_rabbit-mq", "password_rabbit-mq"));
builder.Services.AddSingleton<EventBusSender>();
builder.Services.AddHostedService<YourFirstListener>();
builder.Services.AddHostedService<YourSecondListener>();
/// Other Listeners

Example of Listener

Implement your own event listener (YourFirstListener) using EventBusListener, handling messages from a RabbitMQ queue named "queue_name" and naming the consumer "consumer_name". Be sure to replace bool with the appropriate data type for your response and int with the type of data you intend to handle.

public class YourFirstListener: EventBusListener<int, bool>
{
    private readonly IServiceProvider _serviceProvider;

        public YourFirstListener(IServiceProvider serviceProvider, RabbitMQConnectionFactory rabbitMQConnectionFactory) : base("queue_name", rabbitMQConnectionFactory, "consumer_name")
    {
        _serviceProvider = serviceProvider;
    }

    protected override async Task<EventBusResult<bool>> ExecuteAsync(int yourData)
    {
        using (var scope = _serviceProvider.CreateScope())
        {
                try
                {
                    // Your code logic here
                    return EventBusResult<bool>.Success(true);
                }
                catch (Exception ex)
                {
                    // Your code logic here
                    return EventBusResult<bool>.Failure(ex);
                }
        }
    }
}

Example of Sender

Utilize EventBusSender (_eventBusSender) within a controller (YourController).

private readonly EventBusSender _eventBusSender;

public YourController(EventBusSender eventBusSender)
{
    _eventBusSender = eventBusSender;
}

Use now EventBusSender (_eventBusSender) to dispatch a message to a RabbitMQ queue named "queue_name". Ensure to replace bool with the suitable data type for the your response and int with the data type you wish to send.

EventBusResult<bool> eventBusResult = await _eventBusSender.SendMessage<int, bool>("queue_name", yourData);
if (eventBusResult.Succeeded)
{
    // Your code logic here. Access the body of EventBusResult.
    Console.WriteLine($"EventBusResultValue is: {eventBusResult.Body}");
}
else
}
    // Your code logic here. Throw an exception from EventBusResult's exception.
    throw new Exception(eventBusResult.Exception);
}
Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  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.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.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.