VassasCo.Utility.EventBus
1.0.0
dotnet add package VassasCo.Utility.EventBus --version 1.0.0
NuGet\Install-Package VassasCo.Utility.EventBus -Version 1.0.0
<PackageReference Include="VassasCo.Utility.EventBus" Version="1.0.0" />
<PackageVersion Include="VassasCo.Utility.EventBus" Version="1.0.0" />
<PackageReference Include="VassasCo.Utility.EventBus" />
paket add VassasCo.Utility.EventBus --version 1.0.0
#r "nuget: VassasCo.Utility.EventBus, 1.0.0"
#:package VassasCo.Utility.EventBus@1.0.0
#addin nuget:?package=VassasCo.Utility.EventBus&version=1.0.0
#tool nuget:?package=VassasCo.Utility.EventBus&version=1.0.0
EventBus
轻量级进程内事件总线 — 类型安全的事件发布/订阅,零依赖,支持特性自动注册。
微信公众号:VassasCo,欢迎关注。
文件说明
| 文件 | 内容 |
|---|---|
EventBus.cs |
核心实现:订阅/发布、多通道、粘性事件、异常隔离 |
EventSubscribeAttribute.cs |
特性定义:[EventSubscribe] 标注方法自动订阅 |
EventBusExtensions.cs |
扩展方法:Register() 扫描特性并批量注册 |
快速开始
安装
# 单独安装
dotnet add package VassasCo.Utility.EventBus
# 或安装全套工具包
dotnet add package VassasCo.Utility
1. 定义事件
public record OrderCreatedEvent(string OrderId, decimal Amount);
public record UserLoggedInEvent(string UserName, string Role);
2. 手动订阅 & 发布
// 订阅
var token = EventBus.Default.Subscribe<OrderCreatedEvent>(e =>
Console.WriteLine($"订单 {e.OrderId},金额 {e.Amount:C}"));
// 发布
EventBus.Default.Publish(new OrderCreatedEvent("ORD-001", 999));
// 取消订阅
token.Dispose();
3. 特性订阅(推荐大型项目)
public class OrderService : IDisposable
{
private readonly IDisposable _token;
public OrderService()
{
_token = EventBus.Default.Register(this);
}
[EventSubscribe]
private void OnOrderCreated(OrderCreatedEvent e)
=> Console.WriteLine($"收到订单: {e.OrderId}");
[EventSubscribe(Channels = new[] { "Payment" })]
private void OnPaymentOrder(OrderCreatedEvent e)
=> Console.WriteLine($"[Payment] {e.OrderId}");
public void Dispose() => _token.Dispose();
}
// 一行注册,一行解绑
var service = new OrderService();
EventBus.Default.Publish(new OrderCreatedEvent("ORD-002", 1500));
service.Dispose();
功能详解
优先级
数字越小越先执行,默认 100。适用于多个订阅者之间存在执行顺序依赖的场景。
EventBus.Default.Subscribe<OrderCreatedEvent>(OnHandle1, priority: 10);
EventBus.Default.Subscribe<OrderCreatedEvent>(OnHandle2, priority: 20);
// 发布时 OnHandle1 先执行,OnHandle2 后执行
特性写法:
[EventSubscribe(Priority = 10)]
private void OnHandle1(OrderCreatedEvent e) { }
多通道
订阅方监听多个通道(命中任意即触发),发布方指定单个通道。
// 订阅 Payment 和 Shipping 两个通道
EventBus.Default.Subscribe<OrderCreatedEvent>(
handler: e => Console.WriteLine(e.OrderId),
channels: new[] { "Payment", "Shipping" });
// 发布到 Payment 通道 → 上面会收到
EventBus.Default.Publish(e, channel: "Payment");
// 不带通道发布 → 只有 channels = null 的订阅者收到
EventBus.Default.Publish(e);
通道匹配规则:
| 订阅方通道 | 发布方通道 | 结果 |
|---|---|---|
null |
null |
匹配 |
null |
"Payment" |
匹配 |
["Payment"] |
null |
不匹配 |
["Payment","Shipping"] |
"Payment" |
匹配 |
["Payment"] |
"Other" |
不匹配 |
条件过滤
手动订阅用 Lambda,特性订阅用 FilterMethod 或 FilterProperty。
// Lambda 过滤 — 任意复杂度
EventBus.Default.Subscribe<OrderCreatedEvent>(
handler: e => Handle(e),
filter: e => e.Amount >= 1000 && e.OrderId.StartsWith("VIP"));
// 特性 — 自定义过滤方法
[EventSubscribe(FilterMethod = nameof(ShouldHandle))]
private void OnOrder(OrderCreatedEvent e) { }
private bool ShouldHandle(OrderCreatedEvent e) => e.Amount >= 1000;
// 特性 — 按属性值范围过滤(省去写方法)
[EventSubscribe(FilterProperty = "Amount", FilterMin = 100, FilterMax = 500)]
private void OnMidRange(OrderCreatedEvent e) { }
异步处理
// 手动异步订阅
EventBus.Default.Subscribe<OrderCreatedEvent>(
asyncHandler: async e => await SaveToDbAsync(e));
// 特性异步订阅
[EventSubscribe]
private async Task OnOrderAsync(OrderCreatedEvent e)
{
await Task.Delay(100);
Console.WriteLine($"异步处理: {e.OrderId}");
}
// 异步发布 — 等待所有订阅者完成
await EventBus.Default.PublishAsync(e);
粘性事件
发布后新注册的订阅者会立即收到该类型最后一次发布的粘性事件。
// 发布粘性事件
EventBus.Default.PublishSticky(new UserLoggedInEvent("Admin", "管理员"));
// ... 稍后订阅 ...
EventBus.Default.Subscribe<UserLoggedInEvent>(e =>
Console.WriteLine($"{e.UserName} 已登录")); // 立刻被触发!
异常隔离
某个订阅者抛异常不影响其他订阅者,异常通过 HandlerError 事件集中处理。
EventBus.Default.HandlerError += (_, e) =>
{
Log.Error($"订阅者异常: {e.Exception.Message},事件: {e.Event}");
};
取消订阅
var token = EventBus.Default.Subscribe<OrderCreatedEvent>(OnOrder);
token.Dispose(); // 单条取消
EventBus.Default.UnsubscribeAll<OrderCreatedEvent>(); // 按类型全部取消
EventBus.Default.Clear(); // 清空全部
兼容性
- .NET Standard 2.0
- .NET 6
- .NET 8
- .NET 9
- .NET 10
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. 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 was computed. 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 is compatible. 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. |
| .NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
| .NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen40 was computed. tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- No dependencies.
-
net10.0
- No dependencies.
-
net6.0
- No dependencies.
-
net8.0
- No dependencies.
-
net9.0
- No dependencies.
NuGet packages (1)
Showing the top 1 NuGet packages that depend on VassasCo.Utility.EventBus:
| Package | Downloads |
|---|---|
|
VassasCo.Utility
C# 桌面开发工具库 (WinForm / WPF / Avalonia): - ConfigHelper:零代码实体类⇋JSON/XML配置双向映射(带注释、热重载、原子保存、列表展开) - LogHelper:异步高性能日志系统(建造者模式、异步队列、自动清理、14种日志类型) - CrashDumpHelper:崩溃捕获+FirstChance异常追踪+MiniDump生成 - SnowflakeIdHelper:分布式雪花ID生成器(单调时钟杜绝回拨、集群WorkerId分配、ID反解) - ScheduleHelper:全能定时任务调度器(CRON/固定速率/固定延迟、重试退避、超时取消、日历过滤、线程池、优雅关闭) - RetryHelper:智能重试器(指数退避+抖动+断路器三态+降级+超时,同步/异步) - EventBus:进程内事件总线(类型发布/订阅、特性自动注册、优先级、条件过滤、粘性事件、多通道),支持 .NET Standard 2.0 / .NET 6 / .NET 8 / .NET 9 / .NET 10 - ExcelMapper:原生对象→Excel映射(嵌套类子表头合并、数组独立Sheet、Dictionary自适应、全可配置样式) |
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 1.0.0 | 275 | 6/25/2026 |