Same nested container for Observer and consumer

43 views
Skip to first unread message

Pratik Vasani

unread,
Aug 23, 2016, 5:53:29 AM8/23/16
to masstransit-discuss
Hi 

I am looking at a ways to share the nested container between a Consumer observer and a consumer but I am not quite sure how to do it.

This is my consumer 
 public class ConsumerObserver :
    IConsumeObserver
    {
        Task IConsumeObserver.PreConsume<T>(ConsumeContext<T> context)
        {
            // called before the consumer's Consume method is called

            return Task.FromResult(0);
        }

       

    

        Task IConsumeObserver.ConsumeFault<T>(ConsumeContext<T> context, Exception exception)
        {
            // called if the consumer's Consume method throws an exception
            return Task.Run(() =>
            {
                var container = IoC.GetWorkflowContainer();
                var uow = container.GetInstanceWrapper<IUnitOfWork>();

            // get hold of the nested container used by conumer here and roll back my transaction

                uow.UseTransactions(true);
            });
            
        }
    }

Pratik Vasani

unread,
Aug 23, 2016, 5:57:53 AM8/23/16
to masstransit-discuss
Sorry hit the post button too early 

This is the consumer code 
This is my consumer 
 public class ConsumerObserver :
    IConsumeObserver
    {
        Task IConsumeObserver.PreConsume<T>(ConsumeContext<T> context)
        {
            // called before the consumer's Consume method is called

            return Task.FromResult(0);
        }

       

    

        Task IConsumeObserver.ConsumeFault<T>(ConsumeContext<T> context, Exception exception)
        {
            // called if the consumer's Consume method throws an exception
            return Task.Run(() =>
            {
                var container = IoC.GetWorkflowContainer();
                var uow = container.GetInstanceWrapper<IUnitOfWork>();

            // get hold of the nested container used by conumer here and roll back my transaction
           // right now i am not able to make sure how i get the same nested container being used by the consumer

                uow.UseTransactions(true);
            });
            
        }
    }


Configuration code.

   var container = IoC.GetContainer();
            var bus = ESBInitializer.CreateBus(sbc =>
           {


                sbc.ReceiveEndpoint(GetQueueName(typeof(NotificationEvent)),
                   e =>
                   {
                       e.Consumer<NotificationReceivedConsumer>(container); // pass the main container for mass transit to use.


                   });
         }

 var observer = new ConsumerObserver();
            var obsHandle = bus.ConnectConsumeObserver(observer); // connect obesever before starting the bus.
            var handle = bus.Start();

My understanding is that mass transit gets a nested container from the container object i provided each time a new message is received .  Is that correct ?

How can i force it to share the nested object with my consumerObserver as well ?
Reply all
Reply to author
Forward
0 new messages