Search code examples
c#amazon-sqsamazon-snsmasstransit

Filter breaks topic and subscription name prefix in MassTransit


I use masstransit 8 + amazon sqs/sns, the app is running in Windows Docker container.

I've noticed that adding custom filter to consumers leads to the issue - name of topic and subscription lose their scope prefix. Though the queue itself has always the correct name (with prefix).

Steps to re-produce the issue:

Filter:

public sealed class CustomConsumeFilter<T> : IFilter<ConsumeContext<T>>  where T: class
{
    public async Task Send(ConsumeContext<T> context, IPipe<ConsumeContext<T>> next)
    {
        await next.Send(context);
    }

    public void Probe(ProbeContext context)
    {
        
    }
}

Consumer:

public class TestModelEventsConsumer : IConsumer<TestModel>
{
    public Task Consume(ConsumeContext<TestModel> context)
    {
        return Task.CompletedTask;
    }
}

Masstransit initialization:

var builder = Host.CreateApplicationBuilder(args);

var configuration = builder.Configuration;

builder.Services
       .Configure<AmazonSqsTransportOptions>(configuration.GetSection("MassTransit:Transports:AmazonSqs"))
       .AddMassTransit(bus =>
       {
           bus.AddConsumer<TestModelEventsConsumer>();            

           bus.UsingAmazonSqs((context, sqs) =>
           {
               sqs.UseFilter(new CustomConsumeFilter<TestModel>()); // the issue is in this line

               var entityNameFormatter = new MessageNameFormatterEntityNameFormatter(new AmazonSqsMessageNameFormatter());

               var scope = "test-prefix-";
           
               sqs.MessageTopology.SetEntityNameFormatter(new PrefixEntityNameFormatter(entityNameFormatter, scope));

               sqs.ConfigureEndpoints(context, new KebabCaseEndpointNameFormatter(scope));
           });
       });

builder.Build().Run();

If I run the code with line "sqs.UseFilter" the following objects will be created:

  • Queue - test-prefix-test-model-events - correct (has prefix)
  • Topic - MyApplication_Services-TestModel - incorrect (no prefix)
  • Subscription ARN - arn:aws:sns:eu-south-1:111111111111:MyApplication_Services-TestModel:66904f2a-fc68-4652-b038-ef2d6157b7c9 - incorrect (no prefix)

If I remove the line "sqs.UseFilter" the proper names will be created for objects:

  • Queue - test-prefix-test-model-events - correct (has prefix)
  • Topic - test-prefix-MyApplication_Services-TestModel - correct (has prefix)
  • Subscription ARN - arn:aws:sns:eu-south-1:111111111111:test-prefix-MyApplication_Services-TestModel:b8eb1111-65ca-4833-9b3f-29df2b33a8c9 - correct (has prefix)

I didn't think that simple filter code affect tha name of objects. Setting the scope prefix "test-prefix-" I expect all objects (queue, topic, subscription) to have the prefix no matter if filter is using or not. Could you please advise if this a bug or not and how to resolve it ?

Thanks, Evgeny.


Solution

  • Order matters, configure the topology settings before you add any filters or configure any endpoints.