© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•2y ago
honk

MassTransit: UseConsumeFilter throws nullreference exception on startup if I have batch consumers

MassTransit config
services.AddMassTransit(config =>
{
    config.AddConsumers(typeof(MyNonBatchConsumer).Assembly);
    
    config.UsingAmazonSqs((context, cfg) =>
    {
        cfg.UsePublishFilter(typeof(LoggingPublishFilter<>), provider);
        cfg.UseConsumeFilter(typeof(LoggingConsumeFilter<>), provider);     

        cfg.ReceiveEndpoint($"{typeof(Startup).Namespace?.Replace(".", "_")}_{environment.EnvironmentName}", receiveEndpointConfig =>
        {
            //tried registering the filter here as well, didn't work
            receiveEndpointConfig.ConfigureConsumer<MyNonBatchConsumer>(context);
            //Batch consumer
            receiveEndpointConfig.ConfigureConsumer<MyBatchConsumer>(context);
        });
    }));
});

services.AddScoped(typeof(LoggingPublishFilter<>));
services.AddScoped(typeof(LoggingConsumeFilter<>));
services.AddMassTransit(config =>
{
    config.AddConsumers(typeof(MyNonBatchConsumer).Assembly);
    
    config.UsingAmazonSqs((context, cfg) =>
    {
        cfg.UsePublishFilter(typeof(LoggingPublishFilter<>), provider);
        cfg.UseConsumeFilter(typeof(LoggingConsumeFilter<>), provider);     

        cfg.ReceiveEndpoint($"{typeof(Startup).Namespace?.Replace(".", "_")}_{environment.EnvironmentName}", receiveEndpointConfig =>
        {
            //tried registering the filter here as well, didn't work
            receiveEndpointConfig.ConfigureConsumer<MyNonBatchConsumer>(context);
            //Batch consumer
            receiveEndpointConfig.ConfigureConsumer<MyBatchConsumer>(context);
        });
    }));
});

services.AddScoped(typeof(LoggingPublishFilter<>));
services.AddScoped(typeof(LoggingConsumeFilter<>));


Batch Consumer:
csharp 
public class MyBatchConsumerHandler : 
    IIntegrationEventBatchHandler<MyBatchConsumer>
{
    private readonly IMediator _mediator;

    public MyBatchConsumerHandler(IMediator mediator)
    {
        _mediator = mediator;
    }

    public async Task Consume(ConsumeContext<Batch<MyBatchConsumer>> context)
    {
        //removed for brevity
    }
}
csharp 
public class MyBatchConsumerHandler : 
    IIntegrationEventBatchHandler<MyBatchConsumer>
{
    private readonly IMediator _mediator;

    public MyBatchConsumerHandler(IMediator mediator)
    {
        _mediator = mediator;
    }

    public async Task Consume(ConsumeContext<Batch<MyBatchConsumer>> context)
    {
        //removed for brevity
    }
}


Program runs when I comment out the
receiveEndpointConfig.ConfigureConsumer<MyBatchConsumer>(context);
receiveEndpointConfig.ConfigureConsumer<MyBatchConsumer>(context);
. I need filters for logging purposes. Has anyone else ran into this issue?
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

NullReference Exception
C#CC# / help
3y ago
MassTransit: Consumers not getting called
C#CC# / help
3y ago
✅ MassTransit with RabbitMQ Exception
C#CC# / help
3y ago
✅ Absolutely clueless about this nullreference exception
C#CC# / help
3y ago