How to Create MassTransit Message Interceptor (Observer) with Scoped Lifetime
See original GitHub issueWe 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. Also asked in stackoverflow
Issue Analytics
- State:
- Created 6 years ago
- Comments:10 (4 by maintainers)
Top Results From Across the Web
How to Create MassTransit Message Interceptor (Observer ...
I have tried with Middleware-Filter but no luck. It is giving exception that i cannot use Scoped lifetime object outside of context. It...
Read more >How to Create MassTransit Message Interceptor (Observer ...
We can setup Consume Observer using following way busControl.ConnectConsumeObserver(container.GetInstance<MessageConsumerInterceptor>());.
Read more >Observability
Lifetime Observers. MassTransit supports several message observers allowing received, consumed, sent, and published messages to be monitored. There is a bus ...
Read more >Messaging through a service bus in .NET using MassTransit ...
In this post we'll see how to intercept messages both in the publisher and receiver. The message observers. Messages can be intercepted and ......
Read more >Climate Change Draft Scoping Plan Appendices Contents
To be included with Draft Scoping Plan Supplemental Analysis ... greenhouse gas emission reporting programs and make reasonable efforts to.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

Please ignore my previous message I was able to solve issues with scoped instances myself. It was mistake from my side rely on issue with creation instances from different threads. So I have updated test and enahnce ASP.NET Core DI extension with scope method. Please review if you find that useful https://github.com/MassTransit/MassTransit/pull/1076.
I think using middleware is more appropriate to manipulation with contextual inforamtion which scoped per consumed message. For that purpose I use ASP.NET Core DI and you can find example by link https://github.com/TerraVenil/MassTransit/tree/ResolveScopedInstance. On this point I agree with @hasibul2363 that due to different threads for IFilter and IConsumer scoped information is loosing. Besides I don’t think that it’s good idea to pass
IServiceProviderto Filter/Specification to resolve scoped instances so suggested previousGetOrAddPayload/TryGetPayloadworks for my case BUT is there are way to prevent duplication ofTryGetPayloadto resolve instance and move that responsibility toIServiceProvider?