I’ve been trying to set up Aspire in a .NET Core solution that already uses Azure Service Bus for messaging. I followed the steps on this page but got the following error in the component-consuming project:
Failed to apply configuration value 'ConnectionStrings__Messaging'. A dependency may have failed to start.
In the App Host project, I got the following error as well:
Connection string parameter resource could not be used because connection string 'Messaging' is missing.
I can see the errors in the Logs section of the dashboard that Aspire provides when I run the App Host project.
In the startup of the component-consuming project I have the following lines:
builder.AddAzureServiceBusClient("Messaging");
In the appsettings.json, I have this setting:
"ConnectionStrings": { "Messaging": "Endpoint=sb://somenamespace.servicebus.windows.net/;SharedAccessKeyName=Manage;SharedAccessKey=someaccesskey;EntityPath=topic1"},
And lastly, I got the following in the startup of the App Host project:
var serviceBus = builder.AddConnectionString("Messaging");
builder.AddProject<NotificationService>("notificationservice").WithReference(serviceBus);
What am I missing? Why are the projects unable to read the configured connection string?
2
Answers
Reading the Azure Service Bus Component documentation, it seems these components use a slightly different structure in the
appsettings.json
. Instead of:They use the format of
Aspire:Azure:Messaging:ServiceBus
or in Json:In your AppHost project, you have the following code:
This code is adding a "connection string" to your DistributedApplication (what your AppHost project is building up). This allows for multiple web app projects to consume the same connection information – you would call
.AddReference(serviceBus)
on multiple projects. It also allows you to switch this connection information out during deployment withazd
, as it will prompt for a value.The idea is that the AppHost is the coordinator of how the separate applications in your larger "distributed application" discover and connect to the other parts of the distributed application.
If you just want to configure your single web app project to talk directly to this Service Bus, you can remove the
.AddConnectionString
call in your AppHost, and configure the single project directly through itsappSettings.json
, or user secrets, like a regular ASP.NET Core app.But adding the connection string to the AppHost (both in code, and setting a value in the AppHost configuration), is the more ".NET Aspire" way to do it.