1

I am using the code below to setup MassTransit to use ServiceBus

private static ServiceProvider SetupServiceCollection()
{
    var connectionString = ConfigurationManager.AppSettings["AzureServiceBusConnectionString"];
    var services = new ServiceCollection()
        .AddMassTransit(x =>
        {
            x.UsingAzureServiceBus((context, cfg) =>
            {
                cfg.Host(connectionString);
                cfg.ConfigureEndpoints(context);
                cfg.Message<MyMessage>(x =>
                {
                    x.SetEntityName("my-topic");
                });
            });
        });

    return services.BuildServiceProvider(); 
}

I use the following code to send a message

var message = new MyMessage()
{
    MessageIdentifier = Guid.NewGuid().ToString(),
};

await _busControl.Publish(message);

I want my message to be sent to my-topic only

However, MassTransit is creating topic, the names seem to be getting generated using the type names. How do I totally stop this?

I am setting up the receiver as below

public static void SetupMassTransit(this ServiceCollection services, string connectionString)
{
    services.AddMassTransit(x =>
    {
        x.UsingAzureServiceBus((context, cfg) =>
        {
            cfg.Host(connectionString);
            cfg.ConfigureEndpoints(context);
            x.UsingAzureServiceBus((context, cfg) =>
            {
                cfg.Host(connectionString);
                cfg.SubscriptionEndpoint<MyMessage>("low", e =>
                {
                    e.Consumer<MyMessageConsumer>(context);
                    e.PrefetchCount = 100;
                    e.MaxConcurrentCalls = 100;
                    e.LockDuration = TimeSpan.FromMinutes(5);
                    e.MaxAutoRenewDuration = TimeSpan.FromMinutes(30);
                    e.UseMessageRetry(r => r.Intervals(100, 200, 500, 800, 1000));
                    e.UseInMemoryOutbox();
                    e.ConfigureConsumeTopology = false;
                });
        });
    });
}

I can see that the message is being sent correctly, as its shown inside the subscription in Service Bus Explorer. However, the receiver is not picking it up? There are no errors or anything to go on? Really frustrating

Paul

Paul
  • 2,773
  • 7
  • 41
  • 96

1 Answers1

1

You're calling ConfigureEndpoints, which will by default create receive endpoints for the consumers, sagas, etc. that have been added. However, your code sample doesn't show any .AddConsumer methods. If you don't have any consumers, don't call ConfigureEndpoints.

For your receiver, you should use:

public static void SetupMassTransit(this ServiceCollection services, string connectionString)
{
    services.AddMassTransit(x =>
    {
        x.AddConsumer<MyMessageConsumer>();
        
        x.UsingAzureServiceBus((context, cfg) =>
        {
            cfg.Host(connectionString);

            cfg.SubscriptionEndpoint("your-topic-name", "your-subscription-name", e =>
            {
                e.PrefetchCount = 100;
                e.MaxConcurrentCalls = 100;
                e.LockDuration = TimeSpan.FromMinutes(5);
                e.MaxAutoRenewDuration = TimeSpan.FromMinutes(30);

                e.UseMessageRetry(r => r.Intervals(100, 200, 500, 800, 1000));
                e.UseInMemoryOutbox();

                e.ConfigureConsumer<MyMessageConsumer>(context);
            });
        });
    });
}

For your producer, you can simply use:

private static ServiceProvider SetupServiceCollection()
{
    var connectionString = ConfigurationManager.AppSettings["AzureServiceBusConnectionString"];
    var services = new ServiceCollection()
        .AddMassTransit(x =>
        {
            x.UsingAzureServiceBus((context, cfg) =>
            {
                cfg.Host(connectionString);
            });
        });

    return services.BuildServiceProvider(); 
}

Then, to publish, using your IServiceProvider that was created above:

var bus = serviceProvider.GetRequiredService<IBus>();
var endpoint = await bus.GetSendEndpoint(new Uri("topic:your-topic-name"));
await endpoint.Send(new MyMessage());

That should do the absolute minimum required that you need.

Chris Patterson
  • 28,659
  • 3
  • 47
  • 59
  • I have added more information and the queues and topic are still being created – Paul Oct 06 '20 at 19:22
  • If you don't want to use queues, use [SubscriptionEndpoint](https://masstransit-project.com/advanced/topology/servicebus.html#subscriptions). – Chris Patterson Oct 06 '20 at 19:40
  • The documentation for MassTransit is really frustrating, too many partial samples, like for this one, they use a variable called provider with nothing about where it comes from – Paul Oct 06 '20 at 19:47
  • I removed ConfigureEndpoints and the topic is still being created – Paul Oct 06 '20 at 21:22
  • The receiver is also not picking up the message – Paul Oct 06 '20 at 21:24
  • https://stackoverflow.com/questions/49434228/how-to-specify-which-azure-service-bus-topic-to-use-with-masstransit I cant use this because I dont know where provider is defined – Paul Oct 06 '20 at 21:30
  • It seems like you're missing some of the basics. Your configuration order is totally wrong as well, you need to configure the topology before other components of the bus that use the topology. I'd start by looking through the configuration section. Also _provider_ in that example is the MS DI service provider, like the one you built in your example above. – Chris Patterson Oct 06 '20 at 23:10