RxEventbus.Core
1.0.14
dotnet add package RxEventbus.Core --version 1.0.14
NuGet\Install-Package RxEventbus.Core -Version 1.0.14
<PackageReference Include="RxEventbus.Core" Version="1.0.14" />
<PackageVersion Include="RxEventbus.Core" Version="1.0.14" />
<PackageReference Include="RxEventbus.Core" />
paket add RxEventbus.Core --version 1.0.14
#r "nuget: RxEventbus.Core, 1.0.14"
#:package RxEventbus.Core@1.0.14
#addin nuget:?package=RxEventbus.Core&version=1.0.14
#tool nuget:?package=RxEventbus.Core&version=1.0.14
English | chinese
RxEventbus
A lightweight System.Reactive EventBus designed to help .NET applications achieve low coupling, asynchronous communication, and high extensibility between modules.
π Core Features
- Lightweight and Efficient: Built upon Reactive Extensions for .NET (System.Reactive .net ), providing an efficient and simple event publishing and subscription mechanism.
- Module Decoupling: Achieves indirect communication between modules through an event-driven architecture, effectively reducing code coupling.
- Asynchronous Event Handling: Supports asynchronous event handlers, ensuring event processing does not block the main thread.
- Automatic Registration and Subscription: Automatically discovers and registers all event handlers, subscribing them upon application startup.
- Global Error Handling: Provides a default global event error handling mechanism, which can be overridden as needed.
π‘ How to Use
1. Installation
Install the NuGet package in your .NET project using the NuGet Package Manager Console:
dotnet add package RxEventbus.Core
π¦ Find more details about the NuGet package here: RxEventbus.Core on NuGet.org
2. Service Registration
In your Program.cs or Startup.cs file, simply call the AddEventBus() extension method to register the EventBus and all event handlers.
// Program.cs
public class Program
{
public static void Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddEventBus(); // Register EventBus and Handlers
}
}
3. Defining Events
Define an event type object for your event. This is a simple C# record example, recommended to be defined within your application's domain models or a designated events directory.
// Example Event Payload
public record StockInEvent(int ProductId, int Quantity, string Location);
4. Publishing Events
Obtain an IAppEventBus instance via dependency injection, then call its Publish method, passing an AppEvent<T> object where T is your event's payload type.
using HaiyuEBR.Service.RxEventbus.Events; // Ensure this using directive is present
// Assume StockInEvent record is defined elsewhere, e.g., in a shared library or domain model
// public record StockInEvent(int ProductId, int Quantity, string Location);
public class OrderService
{
private readonly IAppEventBus _eventBus;
public OrderService(IAppEventBus eventBus)
{
_eventBus = eventBus;
}
public void ProcessOrder(int productId, int quantity, string location)
{
// Create an AppEvent object with your specific payload
var stockInPayload = new StockInEvent(productId, quantity, location);
var stockInAppEvent = new AppEvent<StockInEvent>(stockInPayload);
_eventBus.Publish(stockInAppEvent);
Console.WriteLine($"Published Stock In Event: Product ID={stockInAppEvent.Payload.ProductId}");
}
}
5. Handling Events
To handle an event, simply create a class that implements the IAppEventHandler<T> interface, where T is the type of your event's payload. This handler will be automatically discovered and registered upon application startup.
using HaiyuEBR.Service.RxEventbus; // Ensure this using directive is present
using HaiyuEBR.Service.RxEventbus.Events; // Ensure this using directive is present
using System.Threading.Tasks;
// Assume StockInEvent record is defined elsewhere
// public record StockInEvent(int ProductId, int Quantity, string Location);
public class TestStockInEventHandler : IAppEventHandler<StockInEvent>
{
public Task HandleAsync(AppEvent<StockInEvent> evt)
{
Console.WriteLine($"[TestStockInEventHandler] Handling Stock In Event: Product ID={evt.Payload.ProductId}, Quantity={evt.Payload.Quantity}, Location={evt.Payload.Location}");
return Task.CompletedTask;
}
// Optional: Override OnEventHandledAsync, OnErrorAsync, OnCompletedAsync for specific behavior
public Task OnEventHandledAsync(AppEvent<StockInEvent> evt)
{
Console.WriteLine($"[TestStockInEventHandler] Finished handling Stock In Event for Product ID: {evt.Payload.ProductId}");
return Task.CompletedTask;
}
}
π Console Output
Published Stock In Event: Product ID=101
[TestStockInEventHandler] Handling Stock In Event: Product ID=101, Quantity=50, Location=δ»εΊA
[TestStockInEventHandler] Finished handling Stock In Event for Product ID: 101
π Asynchronous & Concurrency Considerations
- Default Asynchronous Processing: The
HandleAsyncmethod ofIAppEventHandler<T>returns aTask, ensuring that event processing is non-blocking. - Concurrency Control: For time-consuming or CPU-intensive operations within event handlers, to prevent blocking the event publishing thread, you can add
.ObserveOn(System.Reactive.Concurrency.Scheduler.Default)within the event bus's internal subscription chain. This offloads event processing to the thread pool for improved concurrency.
π Future Improvements (Roadmap)
- Simplified event type configuration, filtering event types based on the event object itself, enhancing extensibility. (Completed)
π€ Contributing
We welcome and appreciate your contributions! If you have any questions, suggestions, or find a bug, please feel free to submit an Issue or a Pull Request on GitHub.
π License
This project is released under the MIT License.
Β© 2025 Godaday. All rights reserved.
| 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 (>= 9.0.5)
- System.Reactive (>= 6.0.1)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.