0

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.

void.pointer
  • 24,859
  • 31
  • 132
  • 243
  • You should build some [middleware](https://masstransit.io/documentation/configuration/middleware/scoped). There is even [a sample](https://github.com/MassTransit/Sample-ScopedFilters) showing how it all works. – Chris Patterson May 26 '23 at 02:54
  • @ChrisPatterson How do you make scoped filters work without `IServiceCollection`? I'm using `Bus.Factory` and Unity Container in my .NET 4.8 project. I don't have a context object to pass to `UseSendFilter()`. – void.pointer May 26 '23 at 14:45
  • [Non-scoped](https://masstransit.io/documentation/configuration/middleware) is also a thing that would work for you. – Chris Patterson May 26 '23 at 16:55
  • @ChrisPatterson I don't see where that page explains the difference in how you initialize a non-scoped vs scoped. Scoped seems to use `UsePublishFilter()`, which requires an `IServiceProvider`. How do you add a non-scoped filter, which presumably doesn't require `IServiceProvider`? And can that be done using an open-generic, like `UsePublishFilter()`, so that the headers I add are applied to multiple different message types? The response to these questions might be a good format for an SO answer. – void.pointer May 30 '23 at 17:02
  • The non-scoped page shows how to use `AddFilter`, how to create observers to add filters for all message types, etc. – Chris Patterson May 30 '23 at 18:45
  • The solution I landed on was to register `IFilter` explicitly in Unity, and feed those into the factory I use to initialize MT. Then from there use `ConfigurePublish()` to invoke `UseFilter()`. How is creating the complicated pipe / observer objects a better approach? Honestly I skimmed over those because they didn't seem relevant, and the docs didn't make it clear how everything ties together. – void.pointer May 30 '23 at 20:27

0 Answers0