-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathDomainEvents.cs
49 lines (45 loc) · 1.66 KB
/
DomainEvents.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
namespace ServiceControl.Infrastructure.DomainEvents
{
using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using NServiceBus.Logging;
public class DomainEvents : IDomainEvents
{
static readonly ILog Log = LogManager.GetLogger<DomainEvents>();
readonly IServiceProvider serviceProvider;
public DomainEvents(IServiceProvider serviceProvider) => this.serviceProvider = serviceProvider;
public async Task Raise<T>(T domainEvent, CancellationToken cancellationToken) where T : IDomainEvent
{
var handlers = serviceProvider.GetServices<IDomainHandler<T>>();
foreach (var handler in handlers)
{
try
{
await handler.Handle(domainEvent, cancellationToken)
.ConfigureAwait(false);
}
catch (Exception e)
{
Log.Error($"Unexpected error publishing domain event {typeof(T)}", e);
throw;
}
}
var ieventHandlers = serviceProvider.GetServices<IDomainHandler<IDomainEvent>>();
foreach (var handler in ieventHandlers)
{
try
{
await handler.Handle(domainEvent, cancellationToken)
.ConfigureAwait(false);
}
catch (Exception e)
{
Log.Error($"Unexpected error publishing domain event {typeof(T)}", e);
throw;
}
}
}
}
}