HMENetCore.EventBus 6.0.48

dotnet add package HMENetCore.EventBus --version 6.0.48
                    
NuGet\Install-Package HMENetCore.EventBus -Version 6.0.48
                    
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="6.0.48" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="HMENetCore.EventBus" Version="6.0.48" />
                    
Directory.Packages.props
<PackageReference Include="HMENetCore.EventBus" />
                    
Project file
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 6.0.48
                    
#r "nuget: HMENetCore.EventBus, 6.0.48"
                    
#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@6.0.48
                    
#: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=6.0.48
                    
Install as a Cake Addin
#tool nuget:?package=HMENetCore.EventBus&version=6.0.48
                    
Install as a Cake Tool

HMENetCore.EventBus

简介

HMENetCore.EventBus 是一个用于应用程序各部分之间通过事件进行解耦通信的组件。它允许发布者发布事件,同时订阅者监听并处理这些事件,而无需直接相互引用。

.NET支持

  • 跨平台支持: Supports .NET Standard 2.1, .NET 6, .NET 8, and .NET 9.

安装

dotnet add package HMENetCore.EventBus --version 6.0.48

快速入门

服务注册

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 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 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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
6.0.48 125 8/11/2025
6.0.47 180 8/8/2025
6.0.46 211 8/7/2025
6.0.45 141 7/16/2025
6.0.42 152 6/18/2025
6.0.40 277 6/10/2025
6.0.39 238 5/15/2025
6.0.32 201 4/9/2025
6.0.31 176 3/17/2025
6.0.30 129 2/19/2025
6.0.10 123 2/5/2025
6.0.8 155 11/15/2024
6.0.1 167 9/12/2024