How to get access to the Autofac LifetimeScope that wraps the consumer.

385 views
Skip to first unread message

Gert Jansen van Rensburg

unread,
Aug 17, 2016, 2:25:55 AM8/17/16
to masstransit-discuss
I am trying to configure something verify similar to Tranaction,

trying to create middleware except I dont have access to the lifetimescope that is wrapped around the consumer.

public class UnitOfWorkFilter<T> : IFilter<T>
        where T : class, PipeContext
    {
        public void Probe(ProbeContext context)
        {
            context.CreateFilterScope("unit-of-work");
        }

        public async Task Send(T context, IPipe<T> next)
        {
            var scope = context.GetPayload<ILifetimeScope>();
            var uow = scope.Resolve<IUnitOfWork>();

            try
            {
                await next.Send(context);
                await uow.Complete();
            }
            catch
            {
                await uow.Abandon();
                throw;
            }
        }
    } 

At this point looks like I would have to fork MassTransit.AutofacIntegration and alter the AutofacConsumerFactory 

public async Task Send<TMessage>(ConsumeContext<TMessage> context, IPipe<ConsumerConsumeContext<TConsumer, TMessage>> next)
            where TMessage : class
        {
            using (ILifetimeScope innerScope = context.GetOrAddPayload(() => _scope.BeginLifetimeScope(_name)))
            {
                var uow = innerScope.Resolve<IUnitOfWork>();
    
                var consumer = innerScope.Resolve<TConsumer>();
                if (consumer == null)
                {
                    throw new ConsumerException($"Unable to resolve consumer type '{TypeMetadataCache<TConsumer>.ShortName}'.");
                }
                try
                {
                    await next.Send(context.PushConsumer(consumer)).ConfigureAwait(false);
                    await uow.Complete().ConfigureAwait(false);
                }
                catch
                {
                    await uow.Abandon().ConfigureAwait(false);
                    throw;
                }
            }
        }

Chris Patterson

unread,
Aug 17, 2016, 8:33:51 AM8/17/16
to masstrans...@googlegroups.com
Pretty sure the current lifetime scope is always available in Autofac but just adding ILifetimeScope to the constructor. It's always ambient within the current container lifetime scope.

--
You received this message because you are subscribed to the Google Groups "masstransit-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to masstransit-discuss+unsub...@googlegroups.com.
To post to this group, send email to masstransit-discuss@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/masstransit-discuss/f2253f50-6d1b-4891-a1e4-e4deae6ac465%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Chris Patterson

unread,
Aug 17, 2016, 8:34:20 AM8/17/16
to masstrans...@googlegroups.com
Oh, right, you want the filter to be able to access it. Yeah, that's a bit more complicated.

To unsubscribe from this group and stop receiving emails from it, send an email to masstransit-discuss+unsubscribe...@googlegroups.com.

Daniel Little

unread,
Sep 25, 2016, 8:43:04 PM9/25/16
to masstransit-discuss
I'm interested in this too, it appears there's no good way to carry the LifetimeScope with the context
Reply all
Reply to author
Forward
0 new messages