ArgEventBus 1.0.0

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

事件发布订阅

本地事件

本地事件使用 EventManager 类进行管理,适用于单实例应用:

// 定义事件参数类
public class UserUpdatedEventArgs
{
    public string UserId { get; set; }
    public DateTime UpdateTime { get; set; }
}

// 实现事件观察者
public class UserCacheInvalidator : IEventObserver<UserUpdatedEventArgs>
{
    // 定义观察者执行顺序
    public int Order => 1;
    
    // 实现事件处理方法
    public async Task HandleEventAsync(UserUpdatedEventArgs eventArgs)
    {
        // 处理用户更新事件,例如清除缓存
        Console.WriteLine($"用户 {eventArgs.UserId} 在 {eventArgs.UpdateTime} 被更新");
        await Task.CompletedTask;
    }
}

// 发布事件
await ArgEventBus.PubSub.EventManager.Instance.PublishEventAsync(new UserUpdatedEventArgs
{
    UserId = "123",
    UpdateTime = DateTime.UtcNow
});

分布式事件(Redis)

分布式事件使用 RedisEventManager 类进行管理,适用于多实例分布式应用:

// 定义事件参数类(与本地事件相同)
public class UserUpdatedEventArgs
{
    public string UserId { get; set; }
    public DateTime UpdateTime { get; set; }
}

// 实现事件观察者(与本地事件相同)
public class UserCacheInvalidator : IEventObserver<UserUpdatedEventArgs>
{
    public int Order => 1;
    
    public async Task HandleEventAsync(UserUpdatedEventArgs eventArgs)
    {
        // 处理用户更新事件
        Console.WriteLine($"用户 {eventArgs.UserId} 在 {eventArgs.UpdateTime} 被更新");
        await Task.CompletedTask;
    }
}

// 发布分布式事件
await ArgEventBus.PubSub.RedisEventManager.Instance.PublishEventAsync(new UserUpdatedEventArgs
{
    UserId = "123",
    UpdateTime = DateTime.UtcNow
});

自动确认机制

RedisEventManager 支持事件的自动确认机制,确保事件被正确处理:

// 在Startup.cs中配置服务
public void ConfigureServices(IServiceCollection services)
{
    // 添加Redis事件服务,启用自动确认
    services.AddRedisEventServices("localhost:6379", enableAutoAck: true);
}

// 实现支持自动确认的观察者
public class AutoAckObserver : IEventObserver<SomeEventArgs>
{
    public int Order => 1;
    
    public async Task<bool> HandleEventAsync(SomeEventArgs eventArgs)
    {
        try {
            // 处理事件逻辑
            await DoSomethingAsync(eventArgs);
            
            // 返回true表示处理成功,事件将被确认
            return true;
        }
        catch {
            // 返回false表示处理失败,事件不会被确认,将被重新处理
            return false;
        }
    }
}

高级功能

自定义序列化

可以为Redis缓存和事件管理器配置自定义的序列化设置:

// 配置自定义序列化设置
var jsonSettings = new JsonSerializerSettings
{
    TypeNameHandling = TypeNameHandling.All,
    ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
    NullValueHandling = NullValueHandling.Ignore
};

// 使用自定义序列化设置初始化Redis事件管理器
RedisEventManager.Initialize("localhost:6379", jsonSettings);

事件重试机制

RedisEventManager 支持事件处理失败时的重试机制:

// 在Startup.cs中配置服务
public void ConfigureServices(IServiceCollection services)
{
    // 添加Redis事件服务,配置重试策略
    services.AddRedisEventServices(options => {
        options.ConnectionString = "localhost:6379";
        options.EnableAutoAck = true;
        options.MaxRetryCount = 3;
        options.RetryDelayMs = 1000; // 1秒后重试
    });
}
//非redis
public void ConfigureServices(IServiceCollection services)
{
    // 添加事件服务,配置重试策略
    services.AddEventServices();
}
Product 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 was computed.  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. 
.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 is compatible.  net463 was computed.  net47 was computed.  net471 was computed.  net472 is compatible.  net48 is compatible.  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. 
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
1.0.0 80 8/10/2025