forked from Particular/NServiceBus
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Correlation id is now set on outgoing messages to make it possible to…
… keep a full audit trail
- Loading branch information
1 parent
bfd193a
commit 5b1f3d3
Showing
7 changed files
with
131 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 44 additions & 42 deletions
86
src/unicast/NServiceBus.Unicast.Tests/Helpers/MessageHeaderManager.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,48 @@ | ||
namespace NServiceBus.Unicast.Tests.Helpers | ||
namespace NServiceBus.Unicast.Tests.Helpers | ||
{ | ||
using System; | ||
using System.Collections.Generic; | ||
using NServiceBus.MessageMutator; | ||
using NServiceBus.Unicast.Transport; | ||
|
||
public class MessageHeaderManager : IMutateOutgoingTransportMessages | ||
{ | ||
void IMutateOutgoingTransportMessages.MutateOutgoing(object[] messages, TransportMessage transportMessage) | ||
{ | ||
foreach (var staticHeader in staticHeaders.Keys) | ||
{ | ||
transportMessage.Headers.Add(staticHeader,staticHeaders[staticHeader]); | ||
} | ||
|
||
if (messageHeaders != null) | ||
if (messageHeaders.ContainsKey(messages[0])) | ||
foreach (var key in messageHeaders[messages[0]].Keys) | ||
transportMessage.Headers.Add(key, messageHeaders[messages[0]][key]); | ||
|
||
messageHeaders.Clear(); | ||
} | ||
|
||
|
||
public void SetHeader(object message, string key, string value) | ||
{ | ||
if (message == ExtensionMethods.CurrentMessageBeingHandled) | ||
throw new InvalidOperationException("Cannot change headers on the message being processed."); | ||
|
||
if (messageHeaders == null) | ||
messageHeaders = new Dictionary<object, IDictionary<string, string>>(); | ||
|
||
if (!messageHeaders.ContainsKey(message)) | ||
messageHeaders.Add(message, new Dictionary<string, string>()); | ||
|
||
if (!messageHeaders[message].ContainsKey(key)) | ||
messageHeaders[message].Add(key, value); | ||
else | ||
messageHeaders[message][key] = value; | ||
} | ||
|
||
public static IDictionary<string, string> staticHeaders = new Dictionary<string, string>(); | ||
static IDictionary<object, IDictionary<string, string>> messageHeaders; | ||
} | ||
using MessageMutator; | ||
using Transport; | ||
|
||
public class MessageHeaderManager : IMutateOutgoingTransportMessages | ||
{ | ||
void IMutateOutgoingTransportMessages.MutateOutgoing(object[] messages, TransportMessage transportMessage) | ||
{ | ||
foreach (var staticHeader in staticHeaders.Keys) | ||
{ | ||
transportMessage.Headers.Add(staticHeader, staticHeaders[staticHeader]); | ||
} | ||
|
||
if (messageHeaders == null) | ||
return; | ||
|
||
if (messageHeaders.ContainsKey(messages[0])) | ||
foreach (var key in messageHeaders[messages[0]].Keys) | ||
transportMessage.Headers.Add(key, messageHeaders[messages[0]][key]); | ||
|
||
messageHeaders.Clear(); | ||
} | ||
|
||
|
||
public void SetHeader(object message, string key, string value) | ||
{ | ||
if (message == ExtensionMethods.CurrentMessageBeingHandled) | ||
throw new InvalidOperationException("Cannot change headers on the message being processed."); | ||
|
||
if (messageHeaders == null) | ||
messageHeaders = new Dictionary<object, IDictionary<string, string>>(); | ||
|
||
if (!messageHeaders.ContainsKey(message)) | ||
messageHeaders.Add(message, new Dictionary<string, string>()); | ||
|
||
if (!messageHeaders[message].ContainsKey(key)) | ||
messageHeaders[message].Add(key, value); | ||
else | ||
messageHeaders[message][key] = value; | ||
} | ||
|
||
public static IDictionary<string, string> staticHeaders = new Dictionary<string, string>(); | ||
static IDictionary<object, IDictionary<string, string>> messageHeaders; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
namespace NServiceBus.Unicast | ||
{ | ||
using Config; | ||
using MessageMutator; | ||
using Transport; | ||
|
||
/// <summary> | ||
/// Mutator to set the correlation id | ||
/// </summary> | ||
public class CorrelationIdMutator : IMutateOutgoingTransportMessages, INeedInitialization | ||
{ | ||
/// <summary> | ||
/// The bus is needed to get access to the current message id | ||
/// </summary> | ||
public IBus Bus { get; set; } | ||
|
||
/// <summary> | ||
/// If no correlation id is set and the message is sent from a messagehandler the current message id | ||
/// will be used as correlation id to make auditing possible | ||
/// </summary> | ||
/// <param name="messages"></param> | ||
/// <param name="transportMessage"></param> | ||
public void MutateOutgoing(object[] messages, TransportMessage transportMessage) | ||
{ | ||
if (transportMessage.CorrelationId == null && Bus.CurrentMessageContext != null) | ||
transportMessage.CorrelationId = Bus.CurrentMessageContext.Id; | ||
} | ||
|
||
public void Init() | ||
{ | ||
Configure.Instance.Configurer.ConfigureComponent<CorrelationIdMutator>(DependencyLifecycle.InstancePerCall); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters