I was faced with one diffucult, I havent idea how to add IConsumeMessageObserver
entity in DI context, using AddMediator()
extension method. In that section (https://masstransit-project.com/advanced/observers.html#received-messages), only shows, how do it manually.
I tried to resolve IMediator
after AddMediator()
call, like this:
private static void BindInMemoryBroker ( IServiceCollection services )
{
services.AddMediator ( configuration =>
{
AddInMemoryAddOnBroker ( configuration );
} );
var serviceProvider = services.BuildServiceProvider ();
var mediator = serviceProvider.GetService<IMediator> ();
var observer1 = serviceProvider.GetService<MailingObserver<RetrieveAddOnDownloadLinkContract>> ();
var observer2 = serviceProvider.GetService<IncrementAddOnDownloadCounterObserver<RetrieveAddOnDownloadLinkContract>> ();
mediator.ConnectConsumeMessageObserver ( observer1 );
mediator.ConnectConsumeMessageObserver ( observer2 );
}
private static void AddInMemoryAddOnBroker ( IServiceCollectionMediatorConfigurator configuration )
{
AddInMemoryConsumers ( configuration );
AddInMemoryContracts ( configuration );
}
private static void AddInMemoryConsumers ( IServiceCollectionMediatorConfigurator configuration )
{
configuration.AddConsumer<RetrieveAddOnDownloadLinkConsumer> ();
}
private static void AddInMemoryContracts ( IServiceCollectionMediatorConfigurator configuration )
{
configuration.AddRequestClient<RetrieveAddOnDownloadLinkContract> ();
}
But it didn't work, pls help me
There are two types of consume observers, one is delivered all message types (IConsumeObserver
) and the other is only delivered a single message type (IConsumeMessageObserver
). The latter can only be connected to an IBus
or IMediator
, therefore it cannot be configuring within either the AddMediator
or the AddMassTransit
configuration.
You can add an IConsumeObserver
and use pattern matching to select only the message types you need as an alternative.