Z.EventBus 1.0.1

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

Z.EventBus

基于.NET 平台 C# 语言 提供的Channel打造的异步事件总线库

Channel使用

使用

  • EventDiscriptorAttribute 特性

        [AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = false)]
        public class EventDiscriptorAttribute : Attribute
        {
            /// <summary>
            /// 事件名称
            /// </summary>
            public string EventName { get; private set; }
            /// <summary>
            /// channel 容量设置
            /// </summary>
            public int Capacity { get; private set; }
            /// <summary>
            /// 是否维持一个生产者多个消费者模型
            /// </summary>
            public bool SigleReader { get; private set; }
    
            public EventDiscriptorAttribute(string eventName, int capacity = 1000, bool sigleReader = true)
            {
                EventName = eventName;
                Capacity = capacity;
                SigleReader = sigleReader;
            }
        }
    
  • Eto 实现特性

        [EventDiscriptor("test",1000,false)]
        public class TestEto
        {
            public string Name { get; set; }    
    
            public string Description { get; set; } 
        }
    
  • 添加通信管道

     // 注入事件总线
    builder.Services.AddEventBus();
    
  • 注入EventBus

    builder.Services.EventBusSubscribes(c =>
    {
        c.Subscribe<TestDto, TestEventHandler>();
    });
    
  • 创建订阅队列Channle(不使用后台订阅可以不初始化)

    
    builder.Services.BuildServiceProvider()
        .InitChannles();
    
    
  • 使用了Z.Module模块化可以这样

    •   public override void ConfigureServices(ServiceConfigerContext context)
        {
            // 注入事件总线
            context.Services.AddEventBus();
      
            context.Services.EventBusSubscribes(c =>
            {
                c.Subscribe<TestDto, TestEventHandler>();
      
            });
        }
      
        public override void OnInitApplication(InitApplicationContext context)
        {
            context.ServiceProvider.InitChannles();
        }
      
EventHandler定义
public class TestEventHandler : IEventHandler<TestDto>, ITransientDependency
{
    private Microsoft.Extensions.Logging.ILogger _logger;
    public TestEventHandler(ILoggerFactory factory)
    {
        _logger = factory.CreateLogger<TestEventHandler>();
    }
    public Task HandelrAsync(TestDto eto)
    {
        _logger.LogInformation($"{typeof(TestDto).Name}--{eto.Name}--{eto.Description}");
        return Task.CompletedTask;
    }
}
添加两个测试方法
  • 同步消费事件

    •   /// <summary>
        /// 同步消费事件
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task EventCache()
        {
            for (var i = 0; i < 100; i++)
            {
                TestDto eto = new TestDto()
                {
                    Name = "LocalEventBus" + i.ToString(),
                    Description = "zzzz" + i.ToString(),
                };
                await _localEvent.PushAsync(eto);
            }
      
            Log.Warning("我什么时候开始得");
        }
      
    • image
  • 队列消费事件

    •  /// <summary>
       /// 后台消费事件
       /// </summary>
       /// <returns></returns>
       [HttpGet]
       public async Task EventChnnalCache()
       {
           for (var i = 0; i < 100; i++)
           {
               TestDto eto = new TestDto()
               {
                   Name = "LocalEventBus" + i.ToString(),
                   Description = "zzzz" + i.ToString(),
               };
               await _localEvent.EnqueueAsync(eto);
           }
      
           Log.Warning("我什么时候开始得");
       }
      
    • image
Product Compatible and additional computed target framework versions.
.NET 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 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 (1)

Showing the top 1 NuGet packages that depend on Z.EventBus:

Package Downloads
Z.Fantasy.Core

Core包

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.1 277 12/6/2023
1.0.0 157 12/5/2023