Skip to content

Conversation

alistairmatthews
Copy link
Collaborator

@alistairmatthews alistairmatthews commented Sep 18, 2025

Summary

The correct connectionName to pass to AddAzureServiceBusClient() depends on whether you subscribed to a topic in the AppHost.

  • If you did, it must match the name you passed to AddServiceBusSubscription()
  • Otherwise, it must match the name you passed to AddAzureServiceBus()

Fixes #4718


Internal previews

📄 File 🔗 Preview link
docs/messaging/azure-service-bus-integration.md .NET Aspire Azure Service Bus integration

@alistairmatthews
Copy link
Collaborator Author

@eerhardt you might want to check my wording? Thanks!

> The `connectionName` parameter must match the name used when adding the Service Bus resource in the AppHost project. In other words, when you call `AddAzureServiceBus` and provide a name of `messaging` that same name should be used when calling `AddAzureServiceBusClient`. For more information, see [Add Azure Service Bus resource](#add-azure-service-bus-resource).
> The correct `connectionName` parameter depends on your AppHost code:
>
> - If you subscribed to a topic in the AppHost, then the `connectionString` you pass to `AddAzureServiceBusClient()` must match the name you passed to `AddServiceBusSubscription()`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't correct. What matters is what object you passed into .WithReference to your project.

var serviceBus = builder.AddAzureServiceBus("messaging");
var topic = serviceBus.AddServiceBusTopic("myTopic");

builder.AddProject<Projects.WebApplication>("web")
       .WithReference(serviceBus);  // since `serviceBus` was passed, you use "messaging" in your app

// vs.

builder.AddProject<Projects.WebApplication>("web")
       .WithReference(topic);  // since `topic` was passed, you use "myTopic" in your app

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! I think that's clear, but my impression from Discord is that a pattern like this is confusing some devs:

var serviceBus = builder.AddAzureServiceBus("messaging").AddServiceBusTopic("myTopic");

builder.AddProject<Projects.WebApplication>("web")
       .WithReference(serviceBus); 

In other words, they pass serviceBus to the project, so they wrongly expect a connection string named "messaging" but they get one called "myTopic". Then the type of serviceBus might also surprise them!

I think I'll correct the wording in this PR, and then raise a separate issue to clear up this potential problem and discourage calling AddXXX methods on the same line unless fully aware of the consequences. It applies to all integrations so I'll probably put it in the integrations overview article.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

Copy link
Member

@eerhardt eerhardt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the clarification!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Azure Service Bus integration doesn't decribe the behavior of .AddServiceBusSubscription correctly
2 participants