I'd like to add headers to every outgoing message I send. These headers will contain information about the AWS environment from which the messages originate. In this case, I want to add headers with the EC2 instance ID and AWS account ID.
I found this SO answer which explains how to customize headers for outgoing messages. However, seeing as how this answer is 7 years old and there's a lack of documentation & examples, I'd like to revisit the solution with a more object-oriented focus. So far, I deduce I need to do something like this:
var busControl = Bus.Factory.CreateUsingInMemory(c =>
c.ConfigurePublish(x => x.UseExecute(ctx =>
{
ctx.Headers.Set("awsAccountId", "abc");
});
I'd like to keep this logic out of my composition root, which is where it currently lives so that the bus can be registered as an instance in my DI container.
Is there a more object-oriented solution to modifying these headers? Best solution I can come up with, is stuffing the bus creation logic in a factory method that lazily-initializes it. At that point, I can add logic in the factory itself to pass ctx.Headers
to injected objects which have logic to modify headers, etc. However, this doesn't seem like a good approach as it splits up and complicates the configuration of MassTransit itself, and makes it more difficult to follow.
Another concern is that it seems like I need to repeat these lambdas once per publisher (say if I use both in memory and SQS), which isn't sustainable. Again, I wasn't able to find anything in the docs about this, and I'm not able to find any recent discussion about solving this.