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;
}
}
}