ZeidLab.ToolBox.EventBuss is a lightweight, high-performance in-process event handling and messaging library for .NET applications. It provides a clean and efficient way to implement event-driven architectures, enabling loose coupling between components through a publisher-subscriber model. The library supports both event publishing (fire-and-forget) and request-response patterns, making it versatile for various communication needs within your application.
- Event Publishing: Fire-and-forget event publishing with automatic handler discovery and invocation
- Request Handling: Send requests and optionally receive responses using a clean, typed interface
- Automatic Registration: Scan assemblies to automatically discover and register event and request handlers
- High-Performance: Optimized for minimal overhead and maximum throughput in critical paths
- Background Processing: Events are processed asynchronously in a background service
- Parallel Execution: Configurable parallel processing of events for improved throughput
- Dependency Injection: Seamless integration with Microsoft's dependency injection system
To use ZeidLab.ToolBox.EventBuss in your project, you can install it via NuGet:
dotnet add package ZeidLab.ToolBox.EventBussFor more information, please visit EventBuss Package on NuGet.
With each release, we add new features and fix bugs. You can find the full changelog at EventBuss Releases.
EventBuss provides a straightforward API for event publishing and request handling in your .NET applications.
Register EventBuss in your application's service collection during startup:
// Add with default configurationbuilder.Services.AddEventBuss();// Or with custom configurationbuilder.Services.AddEventBuss(options =>{// Register handlers from specific assembliesoptions.RegisterFromAssembly(typeof(Program).Assembly);// Discover handlers from referenced assembliesoptions.FromDependencies=true;// Configure parallel processing (optional)// The default value is the number of available processors cores// One thread per core is recommended for optimal performanceoptions.MaxDegreeOfParallelism=4;});Create event object by implementing the IAppEvent interface. It can be a simple record or class, however we recommend you to use a readonly type for optimized performance. In this example it is a readonly record struct . Then, use the IEventBussService to publish events:
// Define an eventpublicreadonlyrecordstructUserCreatedEvent(stringUserId,stringUsername):IAppEvent;// Publish an event_eventBussService.Publish(newUserCreatedEvent("user123","johndoe"));Implement the IAppEventHandler<T> interface to handle specific events:
publicclassUserCreatedEventHandler:IAppEventHandler<UserCreatedEvent>{privatereadonlyILogger<UserCreatedEventHandler>_logger;publicUserCreatedEventHandler(ILogger<UserCreatedEventHandler>logger){_logger=logger;}publicasyncTaskHandleAsync(UserCreatedEvent@event,CancellationTokencancellationToken){_logger.LogInformation("User created: {UserId}, {Username}",@event.UserId,@event.Username);awaitTask.CompletedTask;}}Send requests and receive responses using the request-response pattern. The request can be a simple record or class, however we recommend you to use a readonly type for optimized performance. In this example it is a readonly record struct :
// Define a request and handlerpublicreadonlyrecordstructGetUserRequest(stringUserId):IRequest<UserDetails>;publicclassGetUserRequestHandler:IRequestHandler<GetUserRequest,UserDetails>{privatereadonlyIUserRepository_repository;publicGetUserRequestHandler(IUserRepositoryrepository){_repository=repository;}publicasyncTask<UserDetails>HandleAsync(GetUserRequestrequest,CancellationTokencancellationToken){returnawait_repository.GetUserByIdAsync(request.UserId,cancellationToken);}}// Send the request and get a responsevaruserDetails=await_eventBussService.SendAsync<GetUserRequest,UserDetails>(newGetUserRequest("user123"),cancellationToken);| Component | Description |
|---|---|
IEventBussService | Core service for publishing events and sending requests |
IAppEvent | Marker interface for event objects |
IAppEventHandler<T> | Interface for event handlers |
IRequest<TResponse> | Interface for request objects that expect a response |
IRequest | Interface for request objects that don't expect a response |
IRequestHandler<TRequest,TResponse> | Interface for request handlers that return a response |
IRequestHandler<TRequest> | Interface for request handlers that do not return a response |
EventBussOptions | Configuration options for EventBuss |
Star this repository and follow me on GitHub to stay informed about new releases and updates. Your support fuels this project's growth!
If my content adds value to your projects, consider supporting me via crypto.
- Bitcoin: bc1qlfljm9mysdtu064z5cf4yq4ddxgdfztgvghw3w
- USDT(TRC20): TJFME9tAnwdnhmqGHDDG5yCs617kyQDV39
Thank you for being part of this community—let’s build smarter, together
Contributions are welcome! Please feel free to submit issues, feature requests, or pull requests.
- Fork the repository
- Create a new branch for your feature or bugfix
- Commit your changes following the project guidelines
- Push your branch and submit a pull request
This project is licensed under the MIT License. See the LICENSE file for details.