Behaviour of PreConsume for ConsumeObserver

198 views
Skip to first unread message

Wyatt F

unread,
Oct 5, 2017, 12:45:12 AM10/5/17
to masstransit-discuss
My ConsumeObserver which inherits IConsumeObserver does not behave as expected based on the docs. I want an observer that will be called before each consumer's Consume method is called (like the description given in the docs for a ConsumeObserver). More specifically I have two consumers that consume the same message and I want the PreConsume to be called for them both. Is this not supported?

The following code will only hit the PreConsume method once even though both consume methods are hit:
using System;
using System.Threading.Tasks;
using MassTransit;

namespace ObserverTest
{
    class Program
    {
        private static void Main(string[] args)
        {
            var bus = Bus.Factory.CreateUsingRabbitMq(sbc =>
            {
                var host = sbc.Host(new Uri("rabbitmq://localhost/dev"), c =>
                {
                    c.Username("guest");
                    c.Password("guest");
                });
                sbc.ReceiveEndpoint(host, "my_queue", c =>
                {
                    c.Consumer<TestConsumer>();
                    c.Consumer<OtherTestConsumer>();
                });
            });
            bus.ConnectConsumeObserver(new ConsumeObserver());
            bus.Start();
            bus.Publish(new TestMessage());
        }
    }

    public class ConsumeObserver : IConsumeObserver
    {
        public Task PreConsume<T>(ConsumeContext<T> context) where T : class
        {
            // called before the consumer's Consume method is called
            Console.WriteLine("PreConsume");
            await context.CompleteTask;
        }

        public async Task PostConsume<T>(ConsumeContext<T> context) where T : class
        {
            // called after the consumer's Consume method is called
            Console.WriteLine("PostConsume");
            await context.CompleteTask;
        }

        public async Task ConsumeFault<T>(ConsumeContext<T> context, Exception exception) where T : class
        {
            await context.CompleteTask;
        }
    }
}



Chris Patterson

unread,
Oct 5, 2017, 4:21:30 PM10/5/17
to masstrans...@googlegroups.com
It will only be called once per message type, it isn't observing the consumer, it's observing the message.

--
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/c0583202-a47d-4221-a072-bb77b3958fe6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Wyatt F

unread,
Oct 6, 2017, 10:28:15 AM10/6/17
to masstransit-discuss
Alright, thank you for your help!


On Thursday, October 5, 2017 at 2:21:30 PM UTC-6, Chris Patterson wrote:
It will only be called once per message type, it isn't observing the consumer, it's observing the message.
To post to this group, send email to masstrans...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages