-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
新增了一个`EventExtensions.cs`文件,定义了一个`EventExtensions`静态类,该类中定义了一个`PublishAsync`扩展方法,该方法允许`IEvent`类型的对象直接调用`PublishAsync`方法进行事件发布。 相关工作项: #1
- Loading branch information
Showing
3 changed files
with
29 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
namespace Biwen.QuickApi.Events | ||
{ | ||
public static class EventExtensions | ||
{ | ||
/// <summary> | ||
/// 事件广播扩展 | ||
/// </summary> | ||
/// <param name="event"></param> | ||
/// <param name="cancellationToken"></param> | ||
/// <returns></returns> | ||
/// <exception cref="InvalidOperationException"></exception> | ||
public static async Task PublishAsync<T>(this T @event, CancellationToken cancellationToken = default) where T : IEvent | ||
{ | ||
if (ServiceRegistration.ServiceProvider is null) throw new InvalidOperationException("mush UseBiwenQuickApis() first!"); | ||
using var scope = ServiceRegistration.ServiceProvider.CreateScope(); | ||
var publisher = scope.ServiceProvider.GetRequiredService<Publisher>(); | ||
await publisher.PublishAsync(@event, cancellationToken); | ||
} | ||
} | ||
} |