You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
40 lines
1.3 KiB
40 lines
1.3 KiB
using Infrastructure.Extensions;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using System;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Infrastructure.Events
|
|
{
|
|
public class EventPublisher : IEventPublisher
|
|
{
|
|
private readonly IServiceProvider _applicationServices;
|
|
|
|
public EventPublisher(IServiceProvider applicationServices)
|
|
{
|
|
this._applicationServices = applicationServices;
|
|
}
|
|
|
|
[System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1031:Do not catch general exception types", Justification = "<挂起>")]
|
|
public virtual void Publish<T>(T eventMessage)
|
|
{
|
|
Task.Run(() =>
|
|
{
|
|
using var scope = _applicationServices.CreateScope();
|
|
var fullName = typeof(IEventHander<T>).FullName;
|
|
var subscribers = scope.ServiceProvider.GetServices<IEventHander<T>>().ToList();
|
|
subscribers.ForEach(subscriber =>
|
|
{
|
|
try
|
|
{
|
|
subscriber.Handle(eventMessage);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
ex.PrintStack();
|
|
}
|
|
});
|
|
});
|
|
}
|
|
}
|
|
} |