EventFlux.RabbitFlow
1.0.3
dotnet add package EventFlux.RabbitFlow --version 1.0.3
NuGet\Install-Package EventFlux.RabbitFlow -Version 1.0.3
<PackageReference Include="EventFlux.RabbitFlow" Version="1.0.3" />
<PackageVersion Include="EventFlux.RabbitFlow" Version="1.0.3" />
<PackageReference Include="EventFlux.RabbitFlow" />
paket add EventFlux.RabbitFlow --version 1.0.3
#r "nuget: EventFlux.RabbitFlow, 1.0.3"
#:package EventFlux.RabbitFlow@1.0.3
#addin nuget:?package=EventFlux.RabbitFlow&version=1.0.3
#tool nuget:?package=EventFlux.RabbitFlow&version=1.0.3
EventFlux.RabbitFlow
EventFlux.RabbitFlow is a lightweight event-driven library that integrates with RabbitMQ, providing an efficient way to handle event-based messaging in .NET applications.
Features
- Seamless integration with RabbitMQ
- Supports event-driven CQRS patterns
- Easy event publishing and subscription
- Automatic dependency injection support
- Simple conventions for event handlers
- Context propagation support (headers)
Requirements
- .NET 6.0 or later (library targets modern .NET; check project file for exact target)
- RabbitMQ broker reachable from your application
Installation
Install the package from NuGet:
Install-Package EventFlux.RabbitFlow
or with the .NET CLI:
dotnet add package EventFlux.RabbitFlow
Quick Start
- Define an event
Create an event that implements IEventRequest:
public class ExampleEventRequest : IEventRequest
{
public string Str { get; set; }
}
- Create an event handler
Implement the IEventHandler<T> interface to handle the event:
public class ExampleEventRequestHandler : IEventHandler<ExampleEventRequest>
{
public async Task Handle(ExampleEventRequest @event)
{
Console.WriteLine("Consume data: " + @event.Str);
await Task.CompletedTask;
}
}
- Configure Dependency Injection
Register the library and RabbitMQ connection in Program.cs. The library exposes an extension method AddEventFluxRabbitFlow that scans assemblies for handlers and configures the required services. Example:
builder.Services.AddEventFluxRabbitFlow(
typeof(Program).Assembly,
new ConnectionFactory() { Uri = new Uri("amqp://guest:guest@localhost:5672") },
"publisher_queue"
);
- Subscribe to events
Resolve the IEventBroker and subscribe to events (typically done as part of your application startup):
var _sp = builder.Services.BuildServiceProvider();
var _eventBus = _sp.GetRequiredService<EventFlux.RabbitFlow.Abstractions.IEventBroker>();
await _eventBus.SubscribeAsync<ExampleEventRequest, ExampleEventRequestHandler>();
- Publish an event
In your API controller or service, publish an event:
[HttpPost]
public async Task<IActionResult> Post()
{
ExampleEventRequest @event = new ExampleEventRequest { Str = "Hello World" };
await _eventBus.PublishAsync(@event);
return Ok();
}
- Publish an event with context
In your API controller or service, publish an event:
[HttpPost]
public async Task<IActionResult> Post()
{
// Set context
_contextAccessor.Context = new EventFluxContext();
_contextAccessor.Context.Items["UserId"] = "12345";
_contextAccessor.Context.Items["CorrelationId"] = Guid.NewGuid().ToString();
TestIntegrationEvent @event = new TestIntegrationEvent("aasdnasfnfavsfh");
await _eventBus.PublishAsync(@event);
return Ok();
}
- Consume event with context
Inject IEventFluxContextAccessor into your event handler to access the context data:
public class ExampleEventRequestHandler : IEventHandler<ExampleEventRequest>
{
private readonly IEventFluxContextAccessor _contextAccessor;
public ExampleEventRequestHandler(IEventFluxContextAccessor contextAccessor)
{
_contextAccessor = contextAccessor;
}
public async Task Handle(ExampleEventRequest @event)
{
Console.WriteLine("Consume data: " + @event.Str);
if (_contextAccessor.Context != null)
{
if (_contextAccessor.Context.Items.TryGetValue("UserId", out var userId))
{
Console.WriteLine($"UserId: {userId}");
}
}
}
}
Configuration
- Connection settings are provided via
RabbitMQ.Client.ConnectionFactorywhen callingAddEventFluxRabbitFlow. - You can customize queue names, routing, and other behavior by extending the provided abstractions such as
IPersistenceConnectionandIEventBusSubscriptionsManager.
Exchange Support
The library uses a default exchange name derived from the configured service name. You can also publish and subscribe to events using a different exchange by passing an exchange name to the provided overloads.
- Default exchange: when you register the library with a
serviceName(the third parameter inAddEventFluxRabbitFlow), that name is used as the default exchange. - Custom exchange: call the overloads that accept an
exchangeNameto publish or subscribe on a different exchange.
Examples:
Publish to a custom exchange:
// publish to the default exchange
await _eventBus.PublishAsync(@event);
// publish to a custom exchange named "eventbus_flow_2"
await _eventBus.PublishAsync(@event, "eventbus_flow_2");
Subscribe to events on a custom exchange:
// subscribe using default exchange
await _eventBus.SubscribeAsync<ExampleEventRequest, ExampleEventRequestHandler>();
// subscribe to the same event on a specific exchange
await _eventBus.SubscribeAsync<ExampleEventRequest, ExampleEventRequestHandler>("eventbus_flow_2");
Project layout
EventBusRabbitMQ.cs— core RabbitMQ integration and publish/subscribe orchestrationEventBusSubscriptionsManager.cs— manages in-memory mapping between events and handlersPersistenceConnection.cs— manages RabbitMQ connection lifecycleRabbitFlowExtensions.cs— DI registration and extension methods
| Product | Versions 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 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 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 is compatible. 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. |
-
net6.0
- EventFlux (>= 1.2.0)
- Newtonsoft.Json (>= 13.0.3)
- Polly (>= 8.5.2)
- RabbitMQ.Client (>= 7.1.2)
-
net7.0
- EventFlux (>= 1.2.0)
- Newtonsoft.Json (>= 13.0.3)
- Polly (>= 8.5.2)
- RabbitMQ.Client (>= 7.1.2)
-
net8.0
- EventFlux (>= 1.2.0)
- Newtonsoft.Json (>= 13.0.3)
- Polly (>= 8.5.2)
- RabbitMQ.Client (>= 7.1.2)
-
net9.0
- EventFlux (>= 1.2.0)
- Newtonsoft.Json (>= 13.0.3)
- Polly (>= 8.5.2)
- RabbitMQ.Client (>= 7.1.2)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.