forked from hibernating-rhinos/rhino-esb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConsumerInterceptor.cs
39 lines (34 loc) · 1.63 KB
/
ConsumerInterceptor.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
using System;
using System.Linq;
using Rhino.ServiceBus.Exceptions;
using Rhino.ServiceBus.Internal;
using Rhino.ServiceBus.Sagas;
namespace Rhino.ServiceBus.Config
{
public class ConsumerInterceptor : IConsumerInterceptor
{
public void ItemCreated(Type createdType, bool isTransient)
{
if (typeof(IMessageConsumer).IsAssignableFrom(createdType) == false)
return;
var interfaces = createdType.GetInterfaces()
.Where(x => x.IsGenericType && x.IsGenericTypeDefinition == false)
.Select(x => x.GetGenericTypeDefinition())
.ToList();
if (interfaces.Contains(typeof(InitiatedBy<>)) &&
interfaces.Contains(typeof(ISaga<>)) == false)
{
throw new InvalidUsageException("Message consumer: " + createdType + " implements InitiatedBy<TMsg> but doesn't implment ISaga<TState>. " + Environment.NewLine +
"Did you forget to inherit from ISaga<TState> ?");
}
if (interfaces.Contains(typeof(InitiatedBy<>)) == false &&
interfaces.Contains(typeof(Orchestrates<>)))
{
throw new InvalidUsageException("Message consumer: " + createdType + " implements Orchestrates<TMsg> but doesn't implment InitiatedBy<TState>. " + Environment.NewLine +
"Did you forget to inherit from InitiatedBy<TState> ?");
}
if(isTransient == false)
throw new InvalidUsageException("Consumers are required to be transient.");
}
}
}