Skip to content

Instantly share code, notes, and snippets.

@ramonsmits
Created September 27, 2019 08:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ramonsmits/fefd68758e928575cf2df3c418641be7 to your computer and use it in GitHub Desktop.
Save ramonsmits/fefd68758e928575cf2df3c418641be7 to your computer and use it in GitHub Desktop.
NServiceBus - Sample that shows the flow of context bag extension data from the incoming to outgoing context
// LogManager.Use<DefaultFactory>().Level(LogLevel.Debug);
// var pipeline = endpointConfiguration.Pipeline;
// pipeline.Register(new IN(), nameof(IN));
// pipeline.Register(new OUT(), nameof(OUT));
class IN : Behavior<IIncomingLogicalMessageContext>
{
public const string Key = "count";
static long count;
static readonly ILog Log = LogManager.GetLogger<IN>();
public override Task Invoke(IIncomingLogicalMessageContext context, Func<Task> next)
{
var value = Interlocked.Increment(ref count);
Log.FatalFormat("count: {0}", value);
context.Extensions.Set(Key, value);
return next();
}
}
class OUT : Behavior<IOutgoingLogicalMessageContext>
{
static readonly ILog Log = LogManager.GetLogger<OUT>();
public override Task Invoke(IOutgoingLogicalMessageContext context, Func<Task> next)
{
var set = context.Extensions.TryGet(IN.Key, out long value);
Log.FatalFormat("count: {0} {1}", set, value);
return next();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment