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
<PackageReference Include="RDC23.EventBus.RabbitMQ" Version="3.0.1" />
<PackageVersion Include="RDC23.EventBus.RabbitMQ" Version="3.0.1" />
<PackageReference Include="RDC23.EventBus.RabbitMQ" />
paket add RDC23.EventBus.RabbitMQ --version 3.0.1
#r "nuget: RDC23.EventBus.RabbitMQ, 3.0.1"
#:package RDC23.EventBus.RabbitMQ@3.0.1
#addin nuget:?package=RDC23.EventBus.RabbitMQ&version=3.0.1
#tool nuget:?package=RDC23.EventBus.RabbitMQ&version=3.0.1
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 | Versions 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. |
-
net8.0
- Microsoft.Extensions.Hosting.Abstractions (>= 8.0.0)
- Newtonsoft.Json (>= 13.0.3)
- RabbitMQ.Client (>= 6.8.1)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.