Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

提供事件总线的发布订阅 #17

Open
vipwan opened this issue May 9, 2024 · 2 comments
Open

提供事件总线的发布订阅 #17

vipwan opened this issue May 9, 2024 · 2 comments
Labels
documentation Improvements or additions to documentation enhancement New feature or request

Comments

@vipwan
Copy link
Owner

vipwan commented May 9, 2024

using Biwen.QuickApi.Events;
using Microsoft.AspNetCore.Mvc;

namespace Biwen.QuickApi.DemoWeb.Apis
{
    public class MyEvent : BaseRequest<MyEvent>,IEvent
    {
        [FromQuery]
        public string? Message { get; set; }
    }

    public class MyEventHandler : EventSubscriber<MyEvent>
    {
        private readonly ILogger<MyEventHandler> _logger;
        public MyEventHandler(ILogger<MyEventHandler> logger)
        {
            _logger = logger;
        }

        public override Task HandleAsync(MyEvent @event, CancellationToken ct)
        {
            _logger.LogInformation($"msg 2 : {@event.Message}");
            return Task.CompletedTask;
        }
    }

    /// <summary>
    /// 更早执行的Handler
    /// </summary>
    public class MyEventHandler2 : EventSubscriber<MyEvent>
    {
        private readonly ILogger<MyEventHandler> _logger;
        public MyEventHandler2(ILogger<MyEventHandler> logger)
        {
            _logger = logger;
        }

        public override Task HandleAsync(MyEvent @event, CancellationToken ct)
        {
            _logger.LogInformation($"msg 1 : {@event.Message}");
            return Task.CompletedTask;
        }
    }

    /// <summary>
    /// 抛出异常的Handler
    /// </summary>
    [EventSubscriber(Order =-2,ThrowIfError =false)]
   public class MyEventHandler3 : EventSubscriber<MyEvent>
    {
        private readonly ILogger<MyEventHandler> _logger;
        public MyEventHandler3(ILogger<MyEventHandler> logger)
        {
            _logger = logger;
        }

        public override Task HandleAsync(MyEvent @event, CancellationToken ct)
        {
            throw new Exception("error");
        }
    }

    [QuickApi("event")]
    public class EventApi : BaseQuickApi<MyEvent>
    {
        public override async ValueTask<IResultResponse> ExecuteAsync(MyEvent request)
        {
            //publish event
            await PublishAsync(request);
            return IResultResponse.Content("send event");
        }
    }
}
@vipwan vipwan added the documentation Improvements or additions to documentation label May 9, 2024
vipwan added a commit that referenced this issue May 9, 2024
vipwan added a commit that referenced this issue May 9, 2024
@vipwan vipwan added the enhancement New feature or request label May 9, 2024
vipwan added a commit that referenced this issue May 10, 2024
@vipwan
Copy link
Owner Author

vipwan commented May 10, 2024

修复一个订阅者订阅多个事件的情况

[EventSubscriber(IsAsync = true,Order =0, ThrowIfError =false)]
public class MyEventHandler4 : IEventSubscriber<MyEvent>, IEventSubscriber<MyEvent2>
{
    private readonly ILogger<MyEventHandler4> _logger;
    public MyEventHandler4(ILogger<MyEventHandler4> logger)
    {
        _logger = logger;
    }

    public Task HandleAsync(MyEvent @event, CancellationToken ct)
    {
        _logger.LogInformation($"muti msg 1 : {@event.Message}");
        return Task.CompletedTask;
    }
    public Task HandleAsync(MyEvent2 @event, CancellationToken ct)
    {
        _logger.LogInformation($"muti msg 2 : {@event.Message}");
        return Task.CompletedTask;
    }
}

vipwan added a commit that referenced this issue May 11, 2024
新增了一个`EventExtensions.cs`文件,定义了一个`EventExtensions`静态类,该类中定义了一个`PublishAsync`扩展方法,该方法允许`IEvent`类型的对象直接调用`PublishAsync`方法进行事件发布。

相关工作项: #1
@vipwan
Copy link
Owner Author

vipwan commented May 11, 2024

提供IEvent的扩展,使得 事件可以脱离QuickApi 在其他Service中使用 :

public class MyEvent : IEvent
{
    public string? Message { get; set; }
}

await new MyEvent { Message = "1234567890" }.PublishAsync();

@vipwan vipwan pinned this issue May 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant