Add metadata when configuring an endpoint to be delivered in the Consumer

Viewed 75

So, when I'm configuring the endpoint and I set ep.Consumer<MyConsumer>(context) - am I able to add something to the Consumer that appears in the ConsumeContext on each invocation?

For example something like this:

        MyMetaData metadata = new MyMetaData() { foo: "Bar" }
        ep.Consumer<MyConsumer>(context, c => c.somehowincludemetadata(metadata));

And then in the Consumer:

        public Task Consume(ConsumeContext<Message> context)
        {
            var metadata = (MyMetaData)context.heresyourmetadata();
        }

Obviously that psudeo-code - but what I'm after is essentially a means by which I can add context (like a customer name, computer name) to the context for retrieve during processing.

It's completely on the consumer side, the publisher won't have any idea of the data that needs to be included.

Many thanks.

1 Answers

That metadata typically has to come from someplace, which can be injected as a dependency into the consumer. Another option, if it is based off data in the message, or headers, is to use middleware – such as a scoped filter that could add payload to the ConsumeContext. This option could either set properties on a shared context object scoped in the container, or as mentioned, add payload.

Yet another option is to somehow add an async method that is called before the consumer, such as shown below.

ep.ConfigureConsumer<MyConsumer>(context, 
    x => x.UseExecute(context => context.Consumer.Property = Value)));

Or, you can add payload as well:

ep.ConfigureConsumer<MyConsumer>(context, 
    x => x.UseExecute(context => 
        context.GetOrAddPayload(() => new MyPayload())));
Related