skip to Main Content

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


  1. Reading the Azure Service Bus Component documentation, it seems these components use a slightly different structure in the appsettings.json. Instead of:

    {
       "ConnectionStrings": {
           "Messaging": ".... connection string ..."
       }
    }
    

    They use the format of Aspire:Azure:Messaging:ServiceBus or in Json:

    {
       "Aspire": {
          "Azure": {
             "Messaging": {
                "ServiceBus": {
                   "ConnectionString": "..."
                }  
             }
          }
       }
    }
    
    Login or Signup to reply.
  2. Just an update, I just added the same ConnectionStrings section to the appsettings.json file in the App Host project and the error disappeared. The question is why would I need to do this? The connection string should be picked up from its original location in the component-consuming project.

    In your AppHost project, you have the following code:

    var serviceBus = builder.AddConnectionString("Messaging");
    builder.AddProject<NotificationService>("notificationservice").WithReference(serviceBus);
    

    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 with azd, 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 its appSettings.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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search