-
Notifications
You must be signed in to change notification settings - Fork 80
/
AzureBusHelper.cs
948 lines (879 loc) · 34.4 KB
/
AzureBusHelper.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
#region IMPORTANT NOTE
// This was once copied almost exactly into the eventhub except for a string difference and required changes be replicated there until a refactor is done... said refactor was never done but I'm upgrading the .NET Core implementation... so drift
#endregion
#region Copyright
// // -----------------------------------------------------------------------
// // <copyright company="Chinchilla Software Limited">
// // Copyright Chinchilla Software Limited. All rights reserved.
// // </copyright>
// // -----------------------------------------------------------------------
#endregion
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using Chinchilla.Logging;
using Cqrs.Authentication;
using Cqrs.Bus;
using Cqrs.Commands;
using Cqrs.Configuration;
using Cqrs.Events;
using Cqrs.Exceptions;
using Cqrs.Messages;
#if NETSTANDARD2_0 || NET48_OR_GREATER
using Azure.Messaging.ServiceBus;
using BrokeredMessage = Azure.Messaging.ServiceBus.ServiceBusReceivedMessage;
using IMessageReceiver = Azure.Messaging.ServiceBus.ServiceBusReceiver;
#else
using Microsoft.ServiceBus.Messaging;
using IMessageReceiver = Microsoft.ServiceBus.Messaging.SubscriptionClient;
#endif
using Newtonsoft.Json;
namespace Cqrs.Azure.ServiceBus
{
/// <summary>
/// A helper for Azure Service Bus and Event Hub.
/// </summary>
/// <typeparam name="TAuthenticationToken">The <see cref="Type"/> of the authentication token.</typeparam>
public class AzureBusHelper<TAuthenticationToken>
: IAzureBusHelper<TAuthenticationToken>
{
/// <summary>
/// Instantiates a new instance of <see cref="AzureBusHelper{TAuthenticationToken}"/>.
/// </summary>
public AzureBusHelper(IAuthenticationTokenHelper<TAuthenticationToken> authenticationTokenHelper, ICorrelationIdHelper correlationIdHelper, ILogger logger, IMessageSerialiser<TAuthenticationToken> messageSerialiser, IBusHelper busHelper, IHashAlgorithmFactory hashAlgorithmFactory, IConfigurationManager configurationManager, IDependencyResolver dependencyResolver)
{
AuthenticationTokenHelper = authenticationTokenHelper;
CorrelationIdHelper = correlationIdHelper;
Logger = logger;
MessageSerialiser = messageSerialiser;
BusHelper = busHelper;
DependencyResolver = dependencyResolver;
ConfigurationManager = configurationManager;
Signer = hashAlgorithmFactory;
}
/// <summary>
/// Gets or sets the <see cref="IAuthenticationTokenHelper{TAuthenticationToken}"/>.
/// </summary>
protected IAuthenticationTokenHelper<TAuthenticationToken> AuthenticationTokenHelper { get; private set; }
/// <summary>
/// Gets or sets the <see cref="ICorrelationIdHelper"/>.
/// </summary>
protected ICorrelationIdHelper CorrelationIdHelper { get; private set; }
/// <summary>
/// Gets or sets the <see cref="ILogger"/>.
/// </summary>
protected ILogger Logger { get; private set; }
/// <summary>
/// Gets or sets the <see cref="IMessageSerialiser{TAuthenticationToken}"/>.
/// </summary>
protected IMessageSerialiser<TAuthenticationToken> MessageSerialiser { get; private set; }
/// <summary>
/// Gets or sets the <see cref="IBusHelper"/>.
/// </summary>
protected IBusHelper BusHelper { get; private set; }
/// <summary>
/// Gets or sets the <see cref="IConfigurationManager"/>.
/// </summary>
protected IConfigurationManager ConfigurationManager { get; private set; }
/// <summary>
/// Gets or sets the <see cref="IDependencyResolver"/>.
/// </summary>
protected IDependencyResolver DependencyResolver { get; private set; }
/// <summary>
/// The configuration key for the default message refreshing setting as used by <see cref="IConfigurationManager"/>.
/// </summary>
protected const string DefaultMessagesShouldRefreshConfigurationKey = "Cqrs.Azure.Messages.ShouldRefresh";
/// <summary>
/// The <see cref="IHashAlgorithmFactory"/> to use to sign messages.
/// </summary>
protected IHashAlgorithmFactory Signer { get; private set; }
/// <summary>
/// Prepares a <see cref="ICommand{TAuthenticationToken}"/> to be sent specifying the framework it is sent via.
/// </summary>
/// <typeparam name="TCommand">The <see cref="Type"/> of<see cref="ICommand{TAuthenticationToken}"/> being sent.</typeparam>
/// <param name="command">The <see cref="ICommand{TAuthenticationToken}"/> to send.</param>
/// <param name="framework">The framework the <paramref name="command"/> is being sent from.</param>
public virtual
#if NETSTANDARD2_0 || NET48_OR_GREATER
async Task PrepareCommandAsync
#else
void PrepareCommand
#endif
<TCommand>(TCommand command, string framework)
where TCommand : ICommand<TAuthenticationToken>
{
if (command.AuthenticationToken == null || command.AuthenticationToken.Equals(default(TAuthenticationToken)))
command.AuthenticationToken = AuthenticationTokenHelper.GetAuthenticationToken();
command.CorrelationId = CorrelationIdHelper.GetCorrelationId();
if (string.IsNullOrWhiteSpace(command.OriginatingFramework))
command.OriginatingFramework = framework;
var frameworks = new List<string>();
if (command.Frameworks != null)
frameworks.AddRange(command.Frameworks);
frameworks.Add(framework);
command.Frameworks = frameworks;
#if NETSTANDARD2_0 || NET48_OR_GREATER
await Task.CompletedTask;
#endif
}
/// <summary>
/// Prepares and validates a <see cref="ICommand{TAuthenticationToken}"/> to be sent specifying the framework it is sent via.
/// </summary>
/// <typeparam name="TCommand">The <see cref="Type"/> of<see cref="ICommand{TAuthenticationToken}"/> being sent.</typeparam>
/// <param name="command">The <see cref="ICommand{TAuthenticationToken}"/> to send.</param>
/// <param name="framework">The framework the <paramref name="command"/> is being sent from.</param>
public virtual
#if NETSTANDARD2_0 || NET48_OR_GREATER
async Task<bool> PrepareAndValidateCommandAsync
#else
bool PrepareAndValidateCommand
#endif
<TCommand>(TCommand command, string framework)
where TCommand : ICommand<TAuthenticationToken>
{
Type commandType = command.GetType();
string loggerMethodName = $"{GetType().FullName}\\PrepareAndValidateEvent({commandType.FullName})";
if (command.Frameworks != null && command.Frameworks.Contains(framework))
{
// if this is the only framework in the list, then it's fine to handle as it's just pre-stamped, if there is more than one framework, then exit.
if (command.Frameworks.Count() != 1)
{
Logger.LogInfo("The provided command has already been processed in Azure.", loggerMethodName);
#if NETSTANDARD2_0 || NET48_OR_GREATER
return await Task.FromResult(false);
#else
return false;
#endif
}
}
ICommandValidator<TAuthenticationToken, TCommand> commandValidator = null;
try
{
commandValidator = DependencyResolver.Resolve<ICommandValidator<TAuthenticationToken, TCommand>>();
}
catch (Exception exception)
{
Logger.LogDebug("Locating an ICommandValidator failed.", loggerMethodName, exception);
}
if (commandValidator != null && !commandValidator.IsCommandValid(command))
{
Logger.LogInfo("The provided command is not valid.", loggerMethodName);
#if NETSTANDARD2_0 || NET48_OR_GREATER
return await Task.FromResult(false);
#else
return false;
#endif
}
#if NETSTANDARD2_0 || NET48_OR_GREATER
await PrepareCommandAsync
#else
PrepareCommand
#endif
(command, framework);
#if NETSTANDARD2_0 || NET48_OR_GREATER
return await Task.FromResult(true);
#else
return true;
#endif
}
/// <summary>
/// Deserialises and processes the <paramref name="messageBody"/> received from the network through the provided <paramref name="receiveCommandHandler"/>.
/// </summary>
/// <param name="serviceBusReceiver">The channel the message was received on.</param>
/// <param name="messageBody">A serialised <see cref="IMessage"/>.</param>
/// <param name="receiveCommandHandler">The handler method that will process the <see cref="ICommand{TAuthenticationToken}"/>.</param>
/// <param name="messageId">The network id of the <see cref="IMessage"/>.</param>
/// <param name="signature">The signature of the <see cref="IMessage"/>.</param>
/// <param name="signingTokenConfigurationKey">The configuration key for the signing token as used by <see cref="IConfigurationManager"/>.</param>
/// <param name="skippedAction">The <see cref="Action"/> to call when the <see cref="ICommand{TAuthenticationToken}"/> is being skipped.</param>
/// <param name="lockRefreshAction">The <see cref="Action"/> to call to refresh the network lock.</param>
/// <returns>The <see cref="ICommand{TAuthenticationToken}"/> that was processed.</returns>
public virtual
#if NETSTANDARD2_0 || NET48_OR_GREATER
async Task<ICommand<TAuthenticationToken>> ReceiveCommandAsync
#else
ICommand<TAuthenticationToken> ReceiveCommand
#endif
(IMessageReceiver serviceBusReceiver, string messageBody, Func<ICommand<TAuthenticationToken>,
#if NETSTANDARD2_0 || NET48_OR_GREATER
Task<bool?>
#else
bool?
#endif
> receiveCommandHandler, string messageId, string signature, string signingTokenConfigurationKey,
#if NETSTANDARD2_0 || NET48_OR_GREATER
Func<Task>
#else
Action
#endif
skippedAction = null,
#if NETSTANDARD2_0 || NET48_OR_GREATER
Func<Task>
#else
Action
#endif
lockRefreshAction = null)
{
ICommand<TAuthenticationToken> command;
try
{
command = MessageSerialiser.DeserialiseCommand(messageBody);
}
catch (JsonSerializationException exception)
{
JsonSerializationException checkException = exception;
bool safeToExit = false;
do
{
if (checkException.Message.StartsWith("Could not load assembly"))
{
safeToExit = true;
break;
}
} while ((checkException = checkException.InnerException as JsonSerializationException) != null);
if (safeToExit)
{
const string pattern = @"(?<=^Error resolving type specified in JSON ').+?(?='\. Path '\$type')";
Match match = new Regex(pattern).Match(exception.Message);
if (match.Success)
{
string[] typeParts = match.Value.Split(',');
if (typeParts.Length == 2)
{
string classType = typeParts[0];
bool isRequired = BusHelper.IsEventRequired($"{classType}.IsRequired");
if (!isRequired)
{
if (skippedAction != null)
#if NETSTANDARD2_0 || NET48_OR_GREATER
await skippedAction();
return await Task.FromResult<ICommand<TAuthenticationToken>>(null);
#else
skippedAction();
return null;
#endif
}
}
}
}
throw;
}
string commandTypeName = command.GetType().FullName;
CorrelationIdHelper.SetCorrelationId(command.CorrelationId);
AuthenticationTokenHelper.SetAuthenticationToken(command.AuthenticationToken);
string identifyMessage = null;
var identifiedEvent = command as ICommandWithIdentity<TAuthenticationToken>;
if (identifiedEvent != null)
identifyMessage = $" for aggregate {identifiedEvent.Rsn}";
#if NETSTANDARD2_0 || NET48_OR_GREATER
string topicPath = serviceBusReceiver == null ? "UNKNOWN" : serviceBusReceiver.EntityPath;
#else
string topicPath = serviceBusReceiver == null ? "UNKNOWN" : serviceBusReceiver.TopicPath;
#endif
Logger.LogInfo($"A command message arrived from topic {topicPath} with the {messageId} was of type {commandTypeName}{identifyMessage}.");
#if NETSTANDARD2_0 || NET48_OR_GREATER
await VerifySignatureAsync
#else
VerifySignature
#endif
(signingTokenConfigurationKey, signature, "A command", messageId, commandTypeName, identifyMessage, messageBody);
bool canRefresh;
if (!ConfigurationManager.TryGetSetting($"{commandTypeName}.ShouldRefresh", out canRefresh))
canRefresh = false;
if (canRefresh && serviceBusReceiver != null)
{
if (lockRefreshAction == null)
Logger.LogWarning($"A command message arrived with the {messageId} was of type {commandTypeName} and was destined to support lock renewal, but no action was provided.");
else
#if NETSTANDARD2_0 || NET48_OR_GREATER
await lockRefreshAction
#else
lockRefreshAction
#endif
();
}
// a false response means the action wasn't handled, but didn't throw an error, so we assume, by convention, that this means it was skipped.
bool? result =
#if NETSTANDARD2_0 || NET48_OR_GREATER
await receiveCommandHandler
#else
receiveCommandHandler
#endif
(command);
if (result != null && !result.Value)
if (skippedAction != null)
#if NETSTANDARD2_0 || NET48_OR_GREATER
await skippedAction
#else
skippedAction
#endif
();
#if NETSTANDARD2_0 || NET48_OR_GREATER
return await Task.FromResult(command);
#else
return command;
#endif
}
/// <summary>
/// The default command handler that
/// check if the <see cref="ICommand{TAuthenticationToken}"/> has already been processed by this framework,
/// checks if the <see cref="ICommand{TAuthenticationToken}"/> is required,
/// finds the handler from the provided <paramref name="routeManager"/>.
/// </summary>
/// <param name="command">The <see cref="ICommand{TAuthenticationToken}"/> to process.</param>
/// <param name="routeManager">The <see cref="RouteManager"/> to get the <see cref="ICommandHandler{TAuthenticationToken,TCommand}"/> from.</param>
/// <param name="framework">The current framework.</param>
/// <returns>
/// True indicates the <paramref name="command"/> was successfully handled by a handler.
/// False indicates the <paramref name="command"/> wasn't handled, but didn't throw an error, so by convention, that means it was skipped.
/// Null indicates the command<paramref name="command"/> wasn't handled as it was already handled.
/// </returns>
public virtual
#if NETSTANDARD2_0 || NET48_OR_GREATER
async Task<bool?> DefaultReceiveCommandAsync
#else
bool? DefaultReceiveCommand
#endif
(ICommand<TAuthenticationToken> command, RouteManager routeManager, string framework)
{
Type commandType = command.GetType();
if (command.Frameworks != null && command.Frameworks.Contains(framework))
{
// if this is the only framework in the list, then it's fine to handle as it's just pre-stamped, if there is more than one framework, then exit.
if (command.Frameworks.Count() != 1)
{
Logger.LogInfo("The provided command has already been processed in Azure.", $"{GetType().FullName}\\DefaultReceiveCommand({commandType.FullName})");
#if NETSTANDARD2_0 || NET48_OR_GREATER
return await Task.FromResult<bool?>(null);
#else
return null;
#endif
}
}
CorrelationIdHelper.SetCorrelationId(command.CorrelationId);
AuthenticationTokenHelper.SetAuthenticationToken(command.AuthenticationToken);
bool isRequired = BusHelper.IsEventRequired(commandType);
RouteHandlerDelegate commandHandler = routeManager.GetSingleHandler(command, isRequired);
// This check doesn't require an isRequired check as there will be an exception raised above and handled below.
if (commandHandler == null)
{
Logger.LogDebug($"The command handler for '{commandType.FullName}' is not required.");
#if NETSTANDARD2_0 || NET48_OR_GREATER
return await Task.FromResult(false);
#else
return false;
#endif
}
#if NETSTANDARD2_0 || NET48_OR_GREATER
Func<IMessage, Task> handler = commandHandler.Delegate;
await handler(command);
return await Task.FromResult(true);
#else
Action<IMessage> handler = commandHandler.Delegate;
handler(command);
return true;
#endif
}
/// <summary>
/// Prepares an <see cref="IEvent{TAuthenticationToken}"/> to be sent specifying the framework it is sent via.
/// </summary>
/// <typeparam name="TEvent">The <see cref="Type"/> of<see cref="IEvent{TAuthenticationToken}"/> being sent.</typeparam>
/// <param name="event">The <see cref="IEvent{TAuthenticationToken}"/> to send.</param>
/// <param name="framework">The framework the <paramref name="event"/> is being sent from.</param>
public virtual
#if NETSTANDARD2_0 || NET48_OR_GREATER
async Task PrepareEventAsync
#else
void PrepareEvent
#endif
<TEvent>(TEvent @event, string framework)
where TEvent : IEvent<TAuthenticationToken>
{
if (@event.AuthenticationToken == null || @event.AuthenticationToken.Equals(default(TAuthenticationToken)))
@event.AuthenticationToken = AuthenticationTokenHelper.GetAuthenticationToken();
@event.CorrelationId = CorrelationIdHelper.GetCorrelationId();
@event.TimeStamp = DateTimeOffset.UtcNow;
if (string.IsNullOrWhiteSpace(@event.OriginatingFramework))
@event.OriginatingFramework = framework;
var frameworks = new List<string>();
if (@event.Frameworks != null)
frameworks.AddRange(@event.Frameworks);
frameworks.Add(framework);
@event.Frameworks = frameworks;
#if NETSTANDARD2_0 || NET48_OR_GREATER
await Task.CompletedTask;
#endif
}
/// <summary>
/// Prepares and validates an <see cref="IEvent{TAuthenticationToken}"/> to be sent specifying the framework it is sent via.
/// </summary>
/// <typeparam name="TEvent">The <see cref="Type"/> of<see cref="IEvent{TAuthenticationToken}"/> being sent.</typeparam>
/// <param name="event">The <see cref="IEvent{TAuthenticationToken}"/> to send.</param>
/// <param name="framework">The framework the <paramref name="event"/> is being sent from.</param>
public virtual
#if NETSTANDARD2_0 || NET48_OR_GREATER
async Task<bool> PrepareAndValidateEventAsync
#else
bool PrepareAndValidateEvent
#endif
<TEvent>(TEvent @event, string framework)
where TEvent : IEvent<TAuthenticationToken>
{
Type eventType = @event.GetType();
if (@event.Frameworks != null && @event.Frameworks.Contains(framework))
{
// if this is the only framework in the list, then it's fine to handle as it's just pre-stamped, if there is more than one framework, then exit.
if (@event.Frameworks.Count() != 1)
{
Logger.LogInfo("The provided event has already been processed in Azure.", $"{GetType().FullName}\\PrepareAndValidateEvent({eventType.FullName})");
#if NETSTANDARD2_0 || NET48_OR_GREATER
return await Task.FromResult(false);
#else
return false;
#endif
}
}
#if NETSTANDARD2_0 || NET48_OR_GREATER
await PrepareEventAsync
#else
PrepareEvent
#endif
(@event, framework);
#if NETSTANDARD2_0 || NET48_OR_GREATER
return await Task.FromResult(true);
#else
return true;
#endif
}
/// <summary>
/// Deserialises and processes the <paramref name="messageBody"/> received from the network through the provided <paramref name="receiveEventHandler"/>.
/// </summary>
/// <param name="serviceBusReceiver">The channel the message was received on.</param>
/// <param name="messageBody">A serialised <see cref="IMessage"/>.</param>
/// <param name="receiveEventHandler">The handler method that will process the <see cref="IEvent{TAuthenticationToken}"/>.</param>
/// <param name="messageId">The network id of the <see cref="IMessage"/>.</param>
/// <param name="signature">The signature of the <see cref="IMessage"/>.</param>
/// <param name="signingTokenConfigurationKey">The configuration key for the signing token as used by <see cref="IConfigurationManager"/>.</param>
/// <param name="skippedAction">The <see cref="Action"/> to call when the <see cref="IEvent{TAuthenticationToken}"/> is being skipped.</param>
/// <param name="lockRefreshAction">The <see cref="Action"/> to call to refresh the network lock.</param>
/// <returns>The <see cref="IEvent{TAuthenticationToken}"/> that was processed.</returns>
public virtual
#if NETSTANDARD2_0 || NET48_OR_GREATER
async Task<IEvent<TAuthenticationToken>> ReceiveEventAsync
#else
IEvent<TAuthenticationToken> ReceiveEvent
#endif
(IMessageReceiver serviceBusReceiver, string messageBody, Func<IEvent<TAuthenticationToken>,
#if NETSTANDARD2_0 || NET48_OR_GREATER
Task<bool?>
#else
bool?
#endif
> receiveEventHandler, string messageId, string signature, string signingTokenConfigurationKey,
#if NETSTANDARD2_0 || NET48_OR_GREATER
Func<Task>
#else
Action
#endif
skippedAction = null,
#if NETSTANDARD2_0 || NET48_OR_GREATER
Func<Task>
#else
Action
#endif
lockRefreshAction = null)
{
IEvent<TAuthenticationToken> @event;
try
{
@event = MessageSerialiser.DeserialiseEvent(messageBody);
}
catch (JsonSerializationException exception)
{
JsonSerializationException checkException = exception;
bool safeToExit = false;
do
{
if (checkException.Message.StartsWith("Could not load assembly"))
{
safeToExit = true;
break;
}
} while ((checkException = checkException.InnerException as JsonSerializationException) != null);
if (safeToExit)
{
const string pattern = @"(?<=^Error resolving type specified in JSON ').+?(?='\. Path '\$type')";
Match match = new Regex(pattern).Match(exception.Message);
if (match.Success)
{
string[] typeParts = match.Value.Split(',');
if (typeParts.Length == 2)
{
string classType = typeParts[0];
bool isRequired = BusHelper.IsEventRequired($"{classType}.IsRequired");
if (!isRequired)
{
if (skippedAction != null)
#if NETSTANDARD2_0 || NET48_OR_GREATER
await skippedAction();
return await Task.FromResult<IEvent<TAuthenticationToken>>(null);
#else
skippedAction();
return null;
#endif
}
}
}
}
throw;
}
string eventTypeName = @event.GetType().FullName;
CorrelationIdHelper.SetCorrelationId(@event.CorrelationId);
AuthenticationTokenHelper.SetAuthenticationToken(@event.AuthenticationToken);
object identifyMessage = null;
var identifiedEvent = @event as IEventWithIdentity<TAuthenticationToken>;
if (identifiedEvent != null)
identifyMessage = " for aggregate {identifiedEvent.Rsn}";
#if NETSTANDARD2_0 || NET48_OR_GREATER
string topicPath = serviceBusReceiver == null ? "UNKNOWN" : serviceBusReceiver.EntityPath;
#else
string topicPath = serviceBusReceiver == null ? "UNKNOWN" : serviceBusReceiver.TopicPath;
#endif
Logger.LogInfo($"An event message arrived from topic {topicPath} with the {messageId} was of type {eventTypeName}{identifyMessage}.");
#if NETSTANDARD2_0 || NET48_OR_GREATER
await VerifySignatureAsync
#else
VerifySignature
#endif
(signingTokenConfigurationKey, signature, "An event", messageId, eventTypeName, identifyMessage, messageBody);
bool canRefresh;
if (!ConfigurationManager.TryGetSetting($"{eventTypeName}.ShouldRefresh", out canRefresh))
if (!ConfigurationManager.TryGetSetting(DefaultMessagesShouldRefreshConfigurationKey, out canRefresh))
canRefresh = false;
if (canRefresh && serviceBusReceiver != null)
{
if (lockRefreshAction == null)
Logger.LogWarning($"An event message arrived with the {messageId} was of type {eventTypeName} and was destined to support lock renewal, but no action was provided.");
else
#if NETSTANDARD2_0 || NET48_OR_GREATER
await lockRefreshAction
#else
lockRefreshAction
#endif
();
}
// a false response means the action wasn't handled, but didn't throw an error, so we assume, by convention, that this means it was skipped.
bool? result =
#if NETSTANDARD2_0 || NET48_OR_GREATER
await receiveEventHandler
#else
receiveEventHandler
#endif
(@event);
if (result != null && !result.Value)
if (skippedAction != null)
#if NETSTANDARD2_0 || NET48_OR_GREATER
await skippedAction
#else
skippedAction
#endif
();
#if NETSTANDARD2_0 || NET48_OR_GREATER
return await Task.FromResult(@event);
#else
return @event;
#endif
}
/// <summary>
/// Refreshes the network lock.
/// </summary>
public virtual
#if NETSTANDARD2_0 || NET48_OR_GREATER
async Task RefreshLockAsync(IMessageReceiver client,
#else
void RefreshLock(
#endif
CancellationTokenSource brokeredMessageRenewCancellationTokenSource, BrokeredMessage message, string type = "message")
{
#if NETSTANDARD2_0 || NET48_OR_GREATER
if (client == null)
return;
#else
Task.Factory.StartNewSafely(() =>
#endif
{
// The capturing of ObjectDisposedException is because even the properties can throw it.
try
{
object value;
string typeName = null;
if (message.TryGetUserPropertyValue("Type", out value))
typeName = value.ToString();
long loop = long.MinValue;
while (!brokeredMessageRenewCancellationTokenSource.Token.IsCancellationRequested)
{
// Based on LockedUntilUtc property to determine if the lock expires soon
// We lock for 45 seconds to ensure any thread based issues are mitigated.
#if NETSTANDARD2_0 || NET48_OR_GREATER
if (DateTime.UtcNow > message.ExpiresAt.AddSeconds(-45))
#else
if (DateTime.UtcNow > message.LockedUntilUtc.AddSeconds(-45))
#endif
{
// If so, renew the lock
for (int i = 0; i < 10; i++)
{
try
{
if (brokeredMessageRenewCancellationTokenSource.Token.IsCancellationRequested)
return;
#if NETSTANDARD2_0 || NET48_OR_GREATER
await client.RenewMessageLockAsync(message);
#else
message.RenewLock();
#endif
string logMessage = $"Renewed the {typeName} lock on {type} '{message.MessageId}'.";
try
{
Logger.LogDebug(logMessage, "Cqrs.Azure.ServiceBus.AzureBusHelper.RefreshLock");
}
catch
{
Trace.TraceInformation(logMessage);
}
break;
}
catch (ObjectDisposedException)
{
return;
}
catch (
#if NETSTANDARD2_0 || NET48_OR_GREATER
ServiceBusException
#else
MessageLockLostException
#endif
exception)
{
string logMessage = $"Renewing the {typeName} lock on {type} '{message.MessageId}' failed as the message lock was lost.\r\n{exception.Message}";
try
{
Logger.LogWarning(logMessage, "Cqrs.Azure.ServiceBus.AzureBusHelper.RefreshLock", exception: exception);
}
catch
{
Trace.TraceWarning(logMessage);
}
return;
}
catch (Exception exception)
{
string logMessage = $"Renewing the {typeName} lock on {type} '{message.MessageId}' failed.\r\n{exception.Message}";
try
{
Logger.LogWarning(logMessage, "Cqrs.Azure.ServiceBus.AzureBusHelper.RefreshLock", exception: exception);
}
catch
{
Trace.TraceWarning(logMessage);
}
if (i == 9)
return;
}
}
}
if (loop++ % 5 == 0)
Thread.Yield();
else
Thread.Sleep(500);
if (loop == long.MaxValue)
loop = long.MinValue;
}
try
{
brokeredMessageRenewCancellationTokenSource.Dispose();
}
catch (ObjectDisposedException) { }
}
catch (ObjectDisposedException) { }
}
#if NETSTANDARD2_0 || NET48_OR_GREATER
await Task.CompletedTask;
#else
, brokeredMessageRenewCancellationTokenSource.Token);
#endif
}
/// <summary>
/// The default event handler that
/// check if the <see cref="IEvent{TAuthenticationToken}"/> has already been processed by this framework,
/// checks if the <see cref="IEvent{TAuthenticationToken}"/> is required,
/// finds the handler from the provided <paramref name="routeManager"/>.
/// </summary>
/// <param name="event">The <see cref="IEvent{TAuthenticationToken}"/> to process.</param>
/// <param name="routeManager">The <see cref="RouteManager"/> to get the <see cref="IEventHandler{TAuthenticationToken,TCommand}"/> from.</param>
/// <param name="framework">The current framework.</param>
/// <returns>
/// True indicates the <paramref name="event"/> was successfully handled by a handler.
/// False indicates the <paramref name="event"/> wasn't handled, but didn't throw an error, so by convention, that means it was skipped.
/// Null indicates the <paramref name="event"/> wasn't handled as it was already handled.
/// </returns>
public virtual
#if NETSTANDARD2_0 || NET48_OR_GREATER
async Task<bool?> DefaultReceiveEventAsync
#else
bool? DefaultReceiveEvent
#endif
(IEvent<TAuthenticationToken> @event, RouteManager routeManager, string framework)
{
Type eventType = @event.GetType();
if (@event.Frameworks != null && @event.Frameworks.Contains(framework))
{
// if this is the only framework in the list, then it's fine to handle as it's just pre-stamped, if there is more than one framework, then exit.
if (@event.Frameworks.Count() != 1)
{
Logger.LogInfo("The provided event has already been processed in Azure.", $"{GetType().FullName}\\DefaultReceiveEvent({eventType.FullName})");
#if NETSTANDARD2_0 || NET48_OR_GREATER
return await Task.FromResult<bool?>(null);
#else
return null;
#endif
}
}
CorrelationIdHelper.SetCorrelationId(@event.CorrelationId);
AuthenticationTokenHelper.SetAuthenticationToken(@event.AuthenticationToken);
bool isRequired = BusHelper.IsEventRequired(eventType);
IEnumerable<
#if NETSTANDARD2_0 || NET48_OR_GREATER
Func<IMessage, Task>
#else
Action<IMessage>
#endif
> handlers = routeManager.GetHandlers(@event, isRequired).Select(x => x.Delegate).ToList();
// This check doesn't require an isRequired check as there will be an exception raised above and handled below.
if (!handlers.Any())
{
Logger.LogDebug($"The event handler for '{eventType.FullName}' is not required.");
#if NETSTANDARD2_0 || NET48_OR_GREATER
return await Task.FromResult(false);
#else
return false;
#endif
}
foreach (var handler in handlers)
#if NETSTANDARD2_0 || NET48_OR_GREATER
await handler(@event);
return await Task.FromResult(true);
#else
handler(@event);
return true;
#endif
}
/// <summary>
/// Verifies that the signature is authorised.
/// </summary>
protected virtual
#if NETSTANDARD2_0 || NET48_OR_GREATER
async Task VerifySignatureAsync
#else
void VerifySignature
#endif
(string signingTokenConfigurationKey, string signature, string messagetype, string messageId, string typeName, object identifyMessage, string messageBody)
{
if (string.IsNullOrWhiteSpace(signature))
Logger.LogWarning($"{messagetype} message arrived with the {messageId} was of type {typeName}{identifyMessage} and had no signature.");
else
{
bool messageIsValid = false;
// see https://github.com/Chinchilla-Software-Com/CQRS/wiki/Inter-process-function-security</remarks>
string configurationKey = $"{typeName}.SigningToken";
string signingToken;
HashAlgorithm signer = Signer.Create();
if (ConfigurationManager.TryGetSetting(configurationKey, out signingToken) && !string.IsNullOrWhiteSpace(signingToken))
using (var hashStream = new MemoryStream(Encoding.UTF8.GetBytes($"{signingToken}{messageBody}")))
messageIsValid = signature == Convert.ToBase64String(signer.ComputeHash(hashStream));
if (!messageIsValid && ConfigurationManager.TryGetSetting(signingTokenConfigurationKey, out signingToken) && !string.IsNullOrWhiteSpace(signingToken))
using (var hashStream = new MemoryStream(Encoding.UTF8.GetBytes($"{signingToken}{messageBody}")))
messageIsValid = signature == Convert.ToBase64String(signer.ComputeHash(hashStream));
if (!messageIsValid)
using (var hashStream = new MemoryStream(Encoding.UTF8.GetBytes($"{Guid.Empty:N}{messageBody}")))
messageIsValid = signature == Convert.ToBase64String(signer.ComputeHash(hashStream));
if (!messageIsValid)
throw new UnAuthorisedMessageReceivedException(typeName, messageId, identifyMessage);
}
#if NETSTANDARD2_0 || NET48_OR_GREATER
await Task.CompletedTask;
#endif
}
/// <summary>
/// Manually registers the provided <paramref name="handler"/>
/// on the provided <paramref name="routeManger"/>
/// </summary>
/// <typeparam name="TMessage">The <see cref="Type"/> of <see cref="IMessage"/> the <paramref name="handler"/> can handle.</typeparam>
public virtual
#if NETSTANDARD2_0 || NET48_OR_GREATER
async Task RegisterHandlerAsync
#else
void RegisterHandler
#endif
<TMessage>(ITelemetryHelper telemetryHelper, RouteManager routeManger,
#if NETSTANDARD2_0 || NET48_OR_GREATER
Func<TMessage, Task>
#else
Action<TMessage>
#endif
handler, Type targetedType, bool holdMessageLock = true)
where TMessage : IMessage
{
#if NETSTANDARD2_0 || NET48_OR_GREATER
Func<TMessage, Task>
#else
Action<TMessage>
#endif
registerableHandler = BusHelper.BuildActionHandler(handler, holdMessageLock);
routeManger.RegisterHandler(registerableHandler, targetedType);
telemetryHelper.TrackEvent($"Cqrs/RegisterHandler/{typeof(TMessage).FullName}", new Dictionary<string, string> { { "Type", "Azure/Bus" } });
telemetryHelper.Flush();
#if NETSTANDARD2_0 || NET48_OR_GREATER
await Task.CompletedTask;
#endif
}
/// <summary>
/// Register an event handler that will listen and respond to all events.
/// </summary>
public virtual
#if NETSTANDARD2_0 || NET48_OR_GREATER
async Task RegisterGlobalEventHandlerAsync
#else
void RegisterGlobalEventHandler
#endif
<TMessage>(ITelemetryHelper telemetryHelper, RouteManager routeManger,
#if NETSTANDARD2_0 || NET48_OR_GREATER
Func<TMessage, Task>
#else
Action<TMessage>
#endif
handler, bool holdMessageLock = true)
where TMessage : IMessage
{
#if NETSTANDARD2_0 || NET48_OR_GREATER
Func<TMessage, Task>
#else
Action<TMessage>
#endif
registerableHandler = BusHelper.BuildActionHandler(handler, holdMessageLock);
routeManger.RegisterGlobalEventHandler(registerableHandler);
telemetryHelper.TrackEvent($"Cqrs/RegisterGlobalEventHandler/{typeof(TMessage).FullName}", new Dictionary<string, string> { { "Type", "Azure/Bus" } });
telemetryHelper.Flush();
#if NETSTANDARD2_0 || NET48_OR_GREATER
await Task.CompletedTask;
#endif
}
}
}