Giter Site home page Giter Site logo

y.eventbus's Introduction

Y.EventBus

基于.NET 平台 C# 语言 提供的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; } 
    }

添加通信管道

 context.Services.Subscribes(p =>
 {
    p.Subscribe<TestEto,TestEventHandler>();
 });

注入EventBus

context.Services.AddEventBus();

创建订阅Eto

var scope = context.ServiceProvider.CreateScope();

var eventhandlerManager = scope.ServiceProvider.GetRequiredService<IEventHandlerManager>();

await authorizeManager.AddAuthorizeRegiester();

await eventhandlerManager.CreateChannles();

EventHandler定义

    public class TestEventHandler : IEventHandler<TestEto>,ITransientInjection
    {
        private ILogger _logger;
        public TestEventHandler(ILoggerFactory factory)
        {
            _logger = factory.CreateLogger<TestEventHandler>();
        }   
        public Task HandelrAsync(TestEto eto)
        {
            _logger.LogInformation($"{typeof(TestEto).Name}--{eto.Name}--{eto.Description}");
            return Task.CompletedTask;
        }
    }
    //注意EventHandler的生命周期请使用AddTrasint() 或者AddScoped()
//构造函数注入即可使用
TestEto eto = null;

for(var i = 0; i < 100; i++)
{
        eto = new TestEto()
        {
            Name ="LocalEventBus" + i.ToString(),
            Description ="wyg"+i.ToString(),
        };
        await _localEventBus.PublichAsync(eto,CancellationToken.None);
}

image

y.eventbus's People

Contributors

calo-yg avatar

Stargazers

 avatar

Watchers

 avatar

y.eventbus's Issues

有限容量通道存在消息写入时长过长问题

有限容量通道中消息堆积存在消息处理过慢的的情况,在等待情况下发送消息会存在时间过长,可以使用CancellationToken配置等下消息写入时长,并且注册CancellationToken.Cancel回调

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.