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 eventMessage) { Task.Run(() => { using var scope = _applicationServices.CreateScope(); var fullName = typeof(IEventHander).FullName; var subscribers = scope.ServiceProvider.GetServices>().ToList(); subscribers.ForEach(subscriber => { try { subscriber.Handle(eventMessage); } catch (Exception ex) { ex.PrintStack(); } }); }); } } }