Reputation: 285
I have added my own headers into my messages and I need to process them to setup DB context for my consumer before the consumer is invoked (the headers allow me to setup my NHibernate session connected to the correct DB which I do not want to have to process in every consumer). I'm using Autofac and I have created an implementation of IInboundMessageInterceptor which processes the message headers. I register the interceptor with Autofac and it resolves fine for each message. But when I get to my consumer, the lifetime scope is different for each message that gets processed and the session I configure in the MessageInterceptor is no longer configured in the new lifetimescope.
a) Is there a way to process the MessageInterceptor in the same lifetimescope as the consumer? b) Is there another place, in the same scope as the consumer, which I can intercept all messages and process the headers? I looked at BeforeConsumingMessage but it has no context of the current message being consumed that I could see.
Thanks
Upvotes: 1
Views: 422
Reputation: 33278
If you are managing the lifetime scope, you really should intercept at the IConsumerFactory
level, instead of using a message interceptor.
Look at the source for the AutofacConsumerFactory<T>
, and you can create your own implementation that decorates it, or you can customize and just use your own to manage your transaction scope instead of nesting them creating complexity in that regard.
That way, any message the consumer receives is wrapped with the proper ISession
setup.
Upvotes: 2
Reputation: 10547
Autofac doesn't really have any knowledge of MassTransit. The resolution of objects is abstracted behind a Func<T>
, so it's not tied to any container. Related to those choices, there currently isn't a way to say "this is a session" and scope to that for when resolving the consumer.
I can't currently think of a way this would be possible to setup without a lot of tearing internals apart. I'd post an issue to https://github.com/masstransit/masstransit, and maybe we can come up with a reasonable way to approach this together.
Upvotes: 0