Hasibul
Hasibul

Reputation: 589

How to Create MassTransit Message Interceptor (Observer) with Scoped Lifetime

We can setup Consume Observer using following way

busControl.ConnectConsumeObserver(container.GetInstance<MessageConsumerInterceptor>());

I want to apply Scope lifestyle to MessageConsumerInterceptor so that I can fill/ initialize some object before consuming or handling the message. And I can access that filled object through the message handler or consumer. Now how I can achieve this using MassTransit?

I am expecting your help or possible suggestion.

Thanks in advance.

Upvotes: 0

Views: 1837

Answers (1)

Alexey Zimarev
Alexey Zimarev

Reputation: 19610

Observer is a not good candidate for this. Observers only observe, they should not change any state or have effect on message processing.

What you are looking at is a middleware.

Check this documentation page on how to create and apply custom middleware.

You will get the ConsumeContext as the Send method parameter in your filter and there you can use methods GetOrAddPayload<T> and TryGetPayload to add something to the context, so you can fetch this data later inside your consumer.

Upvotes: 2

Related Questions