CodeWF.IOC.EventBus 3.4.5.5

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

NuGet NuGet License

CodeWF.EventBus支持任意IOC容器的项目,请搜索 NuGet 包CodeWF.IOC.EventBus并安装最新版,安装完成后,根据 IOC 容器注册单例、获取服务的 API 不同,做相应修改即可。下面是AddEventBusUseEventBus扩展方法逻辑:

public static class EventBusExtensions
{
    public static void AddEventBus(Action<Type, Type> addSingleton1,
        Action<Type> addSingleton2, params Assembly[] assemblies)
    {
        addSingleton1(typeof(IEventBus), typeof(CodeWF.EventBus.EventBus));
        HandleCommandObject(addSingleton2, assemblies);
    }

    public static void UseEventBus(Func<Type, object> resolveAction, params Assembly[] assemblies)
    {
        if (!(resolveAction(typeof(IEventBus)) is IEventBus messenger))
        {
            throw new InvalidOperationException("Please call AddEventBus before calling UseEventBus");
        }

        HandleCommandObject(type => messenger.Subscribe(resolveAction(type)),
            assemblies);
    }

    private static void HandleCommandObject(Action<Type> handleRecipient, params Assembly[] assemblies)
    {
        foreach (var assembly in assemblies.Concat(new []{Assembly.GetCallingAssembly()}))
        {
            var types = assembly.GetTypes()
                .Where(t => t.IsClass
                            && !t.IsAbstract
                            && t.GetCustomAttributes<EventAttribute>().Any()
                            && t.GetMethods(BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public |
                                            BindingFlags.NonPublic).Any(m =>
                                m.GetCustomAttributes<EventHandlerAttribute>().Any()));

            foreach (var type in types)
            {
                handleRecipient(type);
            }
        }
    }
}

比如上面的ASP.NET Core程序在安装CodeWF.IOC.EventBus包后,注册也可以这样写:

// ....

// 1、注册事件总线,将标注`EventHandler`特性方法的类采用单例方式注入IOC容器
EventBusExtensions.AddEventBus(
    (t1, t2) => builder.Services.AddSingleton(t1, t2),
    t => builder.Services.AddSingleton(t),
    Assembly.GetExecutingAssembly());

var app = builder.Build();

// ...

// 2、将上面已经注入IOC容器的类取出、关联处理方法到事件总线管理
EventBusExtensions.UseEventBus(t => app.Services.GetRequiredService(t), Assembly.GetExecutingAssembly());

// ...
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 is compatible.  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. 
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
3.4.5.5 24 5/20/2026
3.4.5.1 125 2/13/2026
3.4.5 224 2/26/2025
3.4.4.2 241 8/20/2024
3.4.4.1 206 8/20/2024
3.4.4 207 6/24/2024
3.4.3 187 6/24/2024
3.4.2 201 6/24/2024
3.4.1 199 6/23/2024
3.3.1 202 6/17/2024
3.3.0 205 6/17/2024
3.2.0 191 6/17/2024
3.0.1 203 6/17/2024
3.0.0 214 6/16/2024