Help with configure ServiceBus and MassTransit #9886
Replies: 7 comments
-
After calling I'm not 100% familiar with MassTransit, so this may not work. But what I would try is to let the .NET Aspire client integration inject the ServiceBusClient into DI and get the ServiceBusClient out of the DI container to give to MassTransit. Maybe you could do something like this: var builder = Host.CreateApplicationBuilder(args);
builder.AddAzureServiceBusClient("servicebus");
builder.Services.AddMassTransit(x =>
{
x.UsingAzureServiceBus((context,cfg) =>
{
ServiceBusClient client = context.GetRequiredService<ServiceBusClient>();
// pass "client" to MassTransit
});
}); |
Beta Was this translation helpful? Give feedback.
-
Here is a problem.
|
Beta Was this translation helpful? Give feedback.
-
.NET Aspire can work with both secret-containing connection strings, like you are used to, and with Microsoft Entra ID (aka Azure Active Directory) - which is the preferred/default approach. This approach doesn't use passwords or secrets. So if you want to use the default behavior (Microsoft Entra ID), your application needs to be able to authenticate to Azure with it. It looks like you found MassTransit/MassTransit#4780 (comment), which is an approach that will work. My only recommenation would be to use the same logic as how the Aspire client integration works to detect whether it is a secret-based connection string or not: Another option, if you want to use a secret based connection string (which isn't recommended for security reasons) would be to change your AppHost code to enable key based auth on the ServiceBus account and build a connection string with a SharedAccessKey in it to pass to your application. |
Beta Was this translation helpful? Give feedback.
-
Aspire doesn't natively support using shared access keys with service bus. We only support the managed identity option. Quite frankly, any library that supports these azure services in 2025 should also support managed identity OOTB. The emulator unfortunately doesn't support this yet so we have to support both formats. |
Beta Was this translation helpful? Give feedback.
-
I was fighting 🤺 with this for almost to 2 day and it works ! Ofc you are right @davidfowl. Thx for explaining now is more clear for me what is going on. |
Beta Was this translation helpful? Give feedback.
-
@huberttrueselftrueme would you be able to post a a solution that you've got? |
Beta Was this translation helpful? Give feedback.
-
@kyurkchyan we fixed the issue by parsing the uri like so var hostConnectionString = builder.Configuration.GetConnectionString("service-bus");
if (!Uri.TryCreate(hostConnectionString, UriKind.Absolute, out var uri))
{
throw new UriFormatException(
$"The connection string '{hostConnectionString}' is not a valid URI."
);
}
var hostUri = uri.Host;
builder.Services.AddMassTransit(x =>
{
x.UsingAzureServiceBus(
(context, config) =>
{
config.Host($"sb://{hostUri}");
// Rest of configuration
}
);
}); |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hello.
In my Aspire project i have
var serviceBus = builder.ExecutionContext.IsPublishMode ? builder.AddAzureServiceBus(OrchestrationResources.ServiceBus) : builder.AddConnectionString(OrchestrationResources.ServiceBus);
For my local envirement it aspire appsetting.json i have
And then

Everything works.
But when i deploy to azure connection string changes to format https://~.servicebus.windows.net:443/ and application crash.
I am reading documentation i am lost how to do this. there is information that i should add builder.AddAzureServiceBusClient(ApplicationConstants.ConnectionStrings.ServiceBus); to get ServiceBusClient
but it give me nothing because there is no Host that accept this clinet.
The closed is :
Can any body help me with this ?
Beta Was this translation helpful? Give feedback.
All reactions