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.
38 lines
1.2 KiB
38 lines
1.2 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;
|
|
}
|
|
|
|
public virtual void Publish<T>(T eventMessage)
|
|
{
|
|
Task.Run(() =>
|
|
{
|
|
using var scope = _applicationServices.CreateScope();
|
|
var subscribers = scope.ServiceProvider.GetServices(typeof(IEventHander<>).MakeGenericType(eventMessage.GetType())).ToList();
|
|
subscribers.ForEach(subscriber =>
|
|
{
|
|
try
|
|
{
|
|
subscriber.GetType().GetMethod("Handle", new Type[] { eventMessage.GetType() }).Invoke(subscriber, new object[] { eventMessage });
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
ex.PrintStack();
|
|
}
|
|
});
|
|
});
|
|
}
|
|
}
|
|
} |