Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f1b7fd4

Browse files
committed
First cut of refactoring outbound interceptors and wiring up an outbound auditing interceptor.
1 parent f552fe4 commit f1b7fd4

25 files changed

Lines changed: 362 additions & 132 deletions
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
5+
namespace Nimbus.MessageContracts.ControlMessages
6+
{
7+
public class AuditEvent : IBusEvent
8+
{
9+
public object MessageBody { get; set; }
10+
public Dictionary<string, object> Properties { get; set; }
11+
public DateTimeOffset Timestamp { get; set; }
12+
13+
public AuditEvent()
14+
{
15+
}
16+
17+
public AuditEvent(object messageBody, IEnumerable<KeyValuePair<string, object>> properties, DateTimeOffset timestamp)
18+
{
19+
MessageBody = messageBody;
20+
Properties = properties.ToDictionary(kvp => kvp.Key, kvp => kvp.Value);
21+
Timestamp = timestamp;
22+
}
23+
}
24+
}

src/Nimbus.MessageContracts/Nimbus.MessageContracts.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
<ItemGroup>
4444
<Compile Include="BusMulticastRequest.cs" />
4545
<Compile Include="BusRequest.cs" />
46+
<Compile Include="ControlMessages\AuditEvent.cs" />
4647
<Compile Include="Exceptions\BusException.cs" />
4748
<Compile Include="Exceptions\InvalidRequestTypeException.cs" />
4849
<Compile Include="IBusCommand.cs" />

src/Nimbus/Extensions/BrokeredMessageExtensions.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,16 @@ internal static BrokeredMessage WithReplyToRequestId(this BrokeredMessage messag
2929
return message.WithProperty(MessagePropertyKeys.InReplyToRequestId, requestId);
3030
}
3131

32+
internal static BrokeredMessage DestinedForQueue(this BrokeredMessage message, string queuePath)
33+
{
34+
return message.WithProperty(MessagePropertyKeys.SentToQueue, queuePath);
35+
}
36+
37+
internal static BrokeredMessage DestinedForTopic(this BrokeredMessage message, string topicPath)
38+
{
39+
return message.WithProperty(MessagePropertyKeys.SentToTopic, topicPath);
40+
}
41+
3242
internal static BrokeredMessage WithReplyTo(this BrokeredMessage message, string replyTo)
3343
{
3444
message.ReplyTo = replyTo;

src/Nimbus/Infrastructure/BrokeredMessageServices/BrokeredMessageFactory.cs

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,8 @@
66
using Microsoft.ServiceBus.Messaging;
77
using Nimbus.Configuration.LargeMessages.Settings;
88
using Nimbus.Configuration.Settings;
9-
using Nimbus.DependencyResolution;
109
using Nimbus.Extensions;
1110
using Nimbus.Infrastructure.Dispatching;
12-
using Nimbus.Interceptors.Inbound;
13-
using Nimbus.Interceptors.Outbound;
1411
using Nimbus.MessageContracts.Exceptions;
1512

1613
namespace Nimbus.Infrastructure.BrokeredMessageServices
@@ -22,10 +19,8 @@ internal class BrokeredMessageFactory : IBrokeredMessageFactory
2219
private readonly ReplyQueueNameSetting _replyQueueName;
2320
private readonly IClock _clock;
2421
private readonly ICompressor _compressor;
25-
private readonly IDependencyResolver _dependencyResolver;
2622
private readonly IDispatchContextManager _dispatchContextManager;
2723
private readonly ILargeMessageBodyStore _largeMessageBodyStore;
28-
private readonly IOutboundInterceptorFactory _outboundInterceptorFactory;
2924
private readonly ISerializer _serializer;
3025
private readonly ITypeProvider _typeProvider;
3126

@@ -34,10 +29,8 @@ public BrokeredMessageFactory(MaxLargeMessageSizeSetting maxLargeMessageSize,
3429
ReplyQueueNameSetting replyQueueName,
3530
IClock clock,
3631
ICompressor compressor,
37-
IDependencyResolver dependencyResolver,
3832
IDispatchContextManager dispatchContextManager,
3933
ILargeMessageBodyStore largeMessageBodyStore,
40-
IOutboundInterceptorFactory outboundInterceptorFactory,
4134
ISerializer serializer,
4235
ITypeProvider typeProvider)
4336
{
@@ -46,10 +39,8 @@ public BrokeredMessageFactory(MaxLargeMessageSizeSetting maxLargeMessageSize,
4639
_replyQueueName = replyQueueName;
4740
_clock = clock;
4841
_compressor = compressor;
49-
_dependencyResolver = dependencyResolver;
5042
_dispatchContextManager = dispatchContextManager;
5143
_largeMessageBodyStore = largeMessageBodyStore;
52-
_outboundInterceptorFactory = outboundInterceptorFactory;
5344
_serializer = serializer;
5445
_typeProvider = typeProvider;
5546
}
@@ -96,15 +87,6 @@ public Task<BrokeredMessage> Create(object serializableObject = null)
9687
// Use the CorrelationId for the current dispatch, otherwise start a new CorrelationId using the message we're sending
9788
brokeredMessage.CorrelationId = currentDispatchContext.CorrelationId ?? brokeredMessage.MessageId;
9889

99-
using (var scope = _dependencyResolver.CreateChildScope())
100-
{
101-
var interceptors = _outboundInterceptorFactory.CreateInterceptors(scope);
102-
foreach (var interceptor in interceptors)
103-
{
104-
await interceptor.Decorate(brokeredMessage, serializableObject);
105-
}
106-
}
107-
10890
return brokeredMessage;
10991
});
11092
}
@@ -182,7 +164,7 @@ public Type GetBodyType(BrokeredMessage message)
182164
var candidates = _typeProvider.AllMessageContractTypes().Where(t => t.FullName == typeName).ToArray();
183165
if (candidates.Any() == false)
184166
throw new Exception("The type '{0}' was not discovered by the type provider and cannot be loaded.".FormatWith(typeName));
185-
167+
186168
// The TypeProvider should not provide a list of duplicates
187169
return candidates.Single();
188170
}

src/Nimbus/Infrastructure/Commands/BusCommandSender.cs

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
using System;
22
using System.Threading.Tasks;
33
using Microsoft.ServiceBus.Messaging;
4+
using Nimbus.DependencyResolution;
45
using Nimbus.Extensions;
6+
using Nimbus.Interceptors.Outbound;
7+
using Nimbus.MessageContracts;
58
using Nimbus.Routing;
69

710
namespace Nimbus.Infrastructure.Commands
@@ -13,44 +16,60 @@ internal class BusCommandSender : ICommandSender
1316
private readonly ILogger _logger;
1417
private readonly INimbusMessagingFactory _messagingFactory;
1518
private readonly IRouter _router;
19+
private readonly IDependencyResolver _dependencyResolver;
20+
private readonly IOutboundInterceptorFactory _outboundInterceptorFactory;
1621

1722
public BusCommandSender(IBrokeredMessageFactory brokeredMessageFactory,
23+
IDependencyResolver dependencyResolver,
1824
IKnownMessageTypeVerifier knownMessageTypeVerifier,
1925
ILogger logger,
2026
INimbusMessagingFactory messagingFactory,
27+
IOutboundInterceptorFactory outboundInterceptorFactory,
2128
IRouter router)
2229
{
2330
_brokeredMessageFactory = brokeredMessageFactory;
2431
_knownMessageTypeVerifier = knownMessageTypeVerifier;
2532
_logger = logger;
2633
_messagingFactory = messagingFactory;
2734
_router = router;
35+
_dependencyResolver = dependencyResolver;
36+
_outboundInterceptorFactory = outboundInterceptorFactory;
2837
}
2938

30-
public async Task Send<TBusCommand>(TBusCommand busCommand)
39+
public async Task Send<TBusCommand>(TBusCommand busCommand) where TBusCommand : IBusCommand
3140
{
3241
var commandType = busCommand.GetType();
33-
3442
_knownMessageTypeVerifier.AssertValidMessageType(commandType);
3543

3644
var message = await _brokeredMessageFactory.Create(busCommand);
3745

38-
await Deliver(commandType, message);
46+
await Deliver(busCommand, commandType, message);
3947
}
4048

41-
public async Task SendAt<TBusCommand>(TBusCommand busCommand, DateTimeOffset whenToSend)
49+
public async Task SendAt<TBusCommand>(TBusCommand busCommand, DateTimeOffset whenToSend) where TBusCommand : IBusCommand
4250
{
4351
var commandType = busCommand.GetType();
4452
_knownMessageTypeVerifier.AssertValidMessageType(commandType);
4553

4654
var message = (await _brokeredMessageFactory.Create(busCommand)).WithScheduledEnqueueTime(whenToSend);
4755

48-
await Deliver(commandType, message);
56+
await Deliver(busCommand, commandType, message);
4957
}
5058

51-
private async Task Deliver(Type commandType, BrokeredMessage message)
59+
private async Task Deliver<TBusCommand>(TBusCommand busCommand, Type commandType, BrokeredMessage message) where TBusCommand : IBusCommand
5260
{
5361
var queuePath = _router.Route(commandType, QueueOrTopic.Queue);
62+
message.DestinedForQueue(queuePath);
63+
64+
using (var scope = _dependencyResolver.CreateChildScope())
65+
{
66+
var interceptors = _outboundInterceptorFactory.CreateInterceptors(scope);
67+
foreach (var interceptor in interceptors)
68+
{
69+
await interceptor.OnCommandSending(busCommand, message);
70+
}
71+
}
72+
5473
var sender = _messagingFactory.GetQueueSender(queuePath);
5574

5675
_logger.Debug("Sending command {0} to {1} [MessageId:{2}, CorrelationId:{3}]",
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
using System;
22
using System.Threading.Tasks;
3+
using Nimbus.MessageContracts;
34

45
namespace Nimbus.Infrastructure.Commands
56
{
67
internal interface ICommandSender
78
{
8-
Task Send<TBusCommand>(TBusCommand busCommand);
9-
Task SendAt<TBusCommand>(TBusCommand busCommand, DateTimeOffset whenToSend);
9+
Task Send<TBusCommand>(TBusCommand busCommand) where TBusCommand : IBusCommand;
10+
Task SendAt<TBusCommand>(TBusCommand busCommand, DateTimeOffset whenToSend) where TBusCommand : IBusCommand;
1011
}
1112
}

src/Nimbus/Infrastructure/Events/BusEventSender.cs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
using System.Threading.Tasks;
2+
using Nimbus.DependencyResolution;
23
using Nimbus.Extensions;
4+
using Nimbus.Interceptors.Outbound;
5+
using Nimbus.MessageContracts;
36
using Nimbus.Routing;
47

58
namespace Nimbus.Infrastructure.Events
@@ -11,28 +14,44 @@ internal class BusEventSender : IEventSender
1114
private readonly IBrokeredMessageFactory _brokeredMessageFactory;
1215
private readonly ILogger _logger;
1316
private readonly IKnownMessageTypeVerifier _knownMessageTypeVerifier;
17+
private readonly IDependencyResolver _dependencyResolver;
18+
private readonly IOutboundInterceptorFactory _outboundInterceptorFactory;
1419

1520
public BusEventSender(IBrokeredMessageFactory brokeredMessageFactory,
21+
IDependencyResolver dependencyResolver,
1622
IKnownMessageTypeVerifier knownMessageTypeVerifier,
1723
ILogger logger,
1824
INimbusMessagingFactory messagingFactory,
25+
IOutboundInterceptorFactory outboundInterceptorFactory,
1926
IRouter router)
2027
{
2128
_messagingFactory = messagingFactory;
2229
_router = router;
30+
_dependencyResolver = dependencyResolver;
31+
_outboundInterceptorFactory = outboundInterceptorFactory;
2332
_brokeredMessageFactory = brokeredMessageFactory;
2433
_logger = logger;
2534
_knownMessageTypeVerifier = knownMessageTypeVerifier;
2635
}
2736

28-
public async Task Publish<TBusEvent>(TBusEvent busEvent)
37+
public async Task Publish<TBusEvent>(TBusEvent busEvent) where TBusEvent : IBusEvent
2938
{
3039
var eventType = busEvent.GetType();
3140

3241
_knownMessageTypeVerifier.AssertValidMessageType(eventType);
3342

3443
var message = await _brokeredMessageFactory.Create(busEvent);
3544
var topicPath = _router.Route(eventType, QueueOrTopic.Topic);
45+
46+
using (var scope = _dependencyResolver.CreateChildScope())
47+
{
48+
var interceptors = _outboundInterceptorFactory.CreateInterceptors(scope);
49+
foreach (var interceptor in interceptors)
50+
{
51+
await interceptor.OnEventPublishing(busEvent, message);
52+
}
53+
}
54+
3655
var topicSender = _messagingFactory.GetTopicSender(topicPath);
3756

3857
_logger.Debug("Publishing event {0} to {1} [MessageId:{2}, CorrelationId:{3}]",
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
using System.Threading.Tasks;
2+
using Nimbus.MessageContracts;
23

34
namespace Nimbus.Infrastructure.Events
45
{
56
internal interface IEventSender
67
{
7-
Task Publish<TBusEvent>(TBusEvent busEvent);
8+
Task Publish<TBusEvent>(TBusEvent busEvent) where TBusEvent : IBusEvent;
89
}
910
}

src/Nimbus/Infrastructure/MessageDispatcherFactory.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using Nimbus.Infrastructure.Events;
88
using Nimbus.Infrastructure.RequestResponse;
99
using Nimbus.Interceptors.Inbound;
10+
using Nimbus.Interceptors.Outbound;
1011

1112
namespace Nimbus.Infrastructure
1213
{
@@ -16,6 +17,7 @@ internal class MessageDispatcherFactory : IMessageDispatcherFactory
1617
private readonly IClock _clock;
1718
private readonly IDependencyResolver _dependencyResolver;
1819
private readonly IInboundInterceptorFactory _inboundInterceptorFactory;
20+
private readonly IOutboundInterceptorFactory _outboundInterceptorFactory;
1921
private readonly ILogger _logger;
2022
private readonly INimbusMessagingFactory _messagingFactory;
2123

@@ -24,14 +26,16 @@ public MessageDispatcherFactory(IBrokeredMessageFactory brokeredMessageFactory,
2426
IDependencyResolver dependencyResolver,
2527
IInboundInterceptorFactory inboundInterceptorFactory,
2628
ILogger logger,
27-
INimbusMessagingFactory messagingFactory)
29+
INimbusMessagingFactory messagingFactory,
30+
IOutboundInterceptorFactory outboundInterceptorFactory)
2831
{
2932
_brokeredMessageFactory = brokeredMessageFactory;
3033
_clock = clock;
3134
_dependencyResolver = dependencyResolver;
3235
_inboundInterceptorFactory = inboundInterceptorFactory;
3336
_logger = logger;
3437
_messagingFactory = messagingFactory;
38+
_outboundInterceptorFactory = outboundInterceptorFactory;
3539
}
3640

3741
public IMessageDispatcher Create(Type openGenericHandlerType, IReadOnlyDictionary<Type, Type[]> handlerMap)
@@ -41,7 +45,7 @@ public IMessageDispatcher Create(Type openGenericHandlerType, IReadOnlyDictionar
4145

4246
private IMessageDispatcher BuildDispatcher(Type openGenericHandlerType, IReadOnlyDictionary<Type, Type[]> handlerMap)
4347
{
44-
if (openGenericHandlerType == typeof(IHandleCommand<>))
48+
if (openGenericHandlerType == typeof (IHandleCommand<>))
4549
{
4650
return new CommandMessageDispatcher(_brokeredMessageFactory,
4751
_clock,
@@ -75,6 +79,7 @@ private IMessageDispatcher BuildDispatcher(Type openGenericHandlerType, IReadOnl
7579
_clock,
7680
_dependencyResolver,
7781
_inboundInterceptorFactory,
82+
_outboundInterceptorFactory,
7883
_logger,
7984
_messagingFactory,
8085
handlerMap);

src/Nimbus/Infrastructure/MessagePropertyKeys.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,7 @@ public static class MessagePropertyKeys
1313
public const string LargeBodyBlobIdentifier = "LargeBodyBlobIdentifier";
1414
public const string MessageType = "MessageType";
1515
public const string InReplyToRequestId = "InReplyToRequestId";
16+
public const string SentToQueue = "SentToQueue";
17+
public const string SentToTopic = "SentToTopic";
1618
}
1719
}

0 commit comments

Comments
 (0)