HMENetCore.EventBus
10.0.1
.NET 8.0
This package targets .NET 8.0. The package is compatible with this framework or higher.
.NET Standard 2.1
This package targets .NET Standard 2.1. The package is compatible with this framework or higher.
dotnet add package HMENetCore.EventBus --version 10.0.1
NuGet\Install-Package HMENetCore.EventBus -Version 10.0.1
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="HMENetCore.EventBus" Version="10.0.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="HMENetCore.EventBus" Version="10.0.1" />
<PackageReference Include="HMENetCore.EventBus" />
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add HMENetCore.EventBus --version 10.0.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: HMENetCore.EventBus, 10.0.1"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package HMENetCore.EventBus@10.0.1
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=HMENetCore.EventBus&version=10.0.1
#tool nuget:?package=HMENetCore.EventBus&version=10.0.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
HMENetCore.EventBus
简介
HMENetCore.EventBus 是一个用于应用程序各部分之间通过事件进行解耦通信的组件。它允许发布者发布事件,同时订阅者监听并处理这些事件,而无需直接相互引用。
运行环境
- 跨平台支持: Supports .NET Standard 2.1, .NET 8, and .NET 10.
安装
dotnet add package HMENetCore.EventBus
快速入门
服务注册
builder.Services.AddEventBus(
config: builder.Configuration.Get<RabbitMQEventBusConfig>()!,
eventHandlerTypes: Assembly.GetExecutingAssembly()
.GetTypes()
.Where(t => t.IsClass && !t.IsAbstract
&& typeof(IIntegrationEventHandler).IsAssignableFrom(t))
);
事件发布
public record OrderEvent(string OrderId, decimal Amount, DateTime CreatedAt);
public class OrderService
{
private readonly IEventBus _eventBus;
public async Task UpdateOrder(OrderEvent Order)
{
await _eventBus.PublishAsync("OrderEvent",
new OrderEvent(Order.OrderId, Order.Amount));
}
}
订阅事件处理方式
1. 基础接口实现
/// <summary>
/// 直接实现IIntegrationEventHandler
/// </summary>
[EventName("UserUpdated")]
public class BasicHandler : IIntegrationEventHandler
{
public async Task Handle(string eventName, string eventData)
{
// 处理逻辑
Console.WriteLine($"收到事件:{eventName},数据:{eventData}");
}
}
2. 强类型处理(推荐)
/// <summary>
/// 继承JsonIntegrationEventHandler
/// </summary>
[EventName("OrderEvent")]
public class TypedHandler : JsonIntegrationEventHandler<OrderEvent>
{
public override Task HandleJson(string eventName, OrderEvent eventData)
{
// 通过eventData.OrderId访问强类型数据
return Task.CompletedTask;
}
}
3. 动态JSON处理
/// <summary>
/// 继承DynamicIntegrationEventHandler
/// </summary>
[EventName("PaymentProcessed")]
public class DynamicHandler : DynamicIntegrationEventHandler
{
public override Task HandleDynamic(string eventName, JsonElement eventData)
{
var amount = eventData.GetProperty("Amount").GetDecimal();
// 处理逻辑
Console.WriteLine($"收到事件:{eventName},数据:{amount}");
}
}
批量订阅
[EventName("xxxx")]
[EventName("xxxxxx")]
public class OrderEventHandler : IIntegrationEventHandler
{
public async Task Handle(string eventName, string eventData)
{
switch(eventName)
{
case "OrderCreated":
// 处理逻辑...
break;
case "OrderCancelled":
// 处理逻辑...
break;
}
}
}
实践
事件设计
📌 使用过去时态命名(如OrderShipped) 📌 事件类声明为readonly record 📌 包含完整的上下文信息
错误处理
⚠️ 业务异常应被捕获处理 🔄 系统异常会自动重试3次
性能优化
🚀 高频事件使用批量处理模式 💾 大消息体考虑使用外部存储
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. 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 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 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 | netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.1 is compatible. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
.NETStandard 2.1
- Microsoft.Extensions.Options (>= 10.0.1)
- RabbitMQ.Client (>= 7.2.0)
- System.Text.Json (>= 10.0.1)
-
net10.0
- Microsoft.Extensions.Options (>= 10.0.1)
- RabbitMQ.Client (>= 7.2.0)
-
net8.0
- Microsoft.Extensions.Options (>= 10.0.1)
- RabbitMQ.Client (>= 7.2.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.