docs-aspire icon indicating copy to clipboard operation
docs-aspire copied to clipboard

Aspire.Hosting.Azure.Functions does not use the typical connection string naming convention

Open matthewhartz-qmerit opened this issue 5 months ago • 2 comments

Is there an existing issue for this?

  • [x] I have searched the existing issues

Describe the bug

When configuring azure functions projects and passing in a reference to blob storage I noticed that the way we access connection strings in the functions application does not work the same was as an API service.

For example, I have this code: builder.AddProject<Projects.ApiService>("apiservice") .WithExternalHttpEndpoints() .WithReference(rabbitMQ) .WithReference(cosmos) .WithReference(blobs) .WaitFor(rabbitMQ) .WaitFor(blobs);

builder.AddAzureFunctionsProject<Projects.Functions>("functions") .WithExternalHttpEndpoints() .WithHostStorage(storage) .WithReference(blobs) .WaitFor(blobs) .WaitFor(storage);

but when access the connection string in the function I get null. var blobConnectionString = context.Configuration.GetConnectionString("Blobs")!;

My work around was to change the with reference to this: builder.AddAzureFunctionsProject<Projects.PanelInsights_Functions>("functions") .WithExternalHttpEndpoints() .WithHostStorage(storage) .WithReference(blobs, "ConnectionStrings:Blobs") .WaitFor(blobs) .WaitFor(storage);

Expected Behavior

.WithReference(blobs)

paired with: var blobConnectionString = context.Configuration.GetConnectionString("Blobs")!;

should get the value of the connection string

Steps To Reproduce

  • Set up an API project and a Functions project
  • Connect them using code provided above
  • Verify how you access the connection string in both api project and functions project
  • Notice that in the function project it returns null

Exceptions (if any)

No response

.NET Version info

9.0.7

Anything else?

No response

matthewhartz-qmerit avatar Jul 25 '25 15:07 matthewhartz-qmerit

That's right, it's because functions projects are a bit strange with respect to configuration. Using the typical naming pattern breaks functons bindings so we use the alternative configuration pattern when applying configuration to functions.

davidfowl avatar Jul 28 '25 03:07 davidfowl

@davidfowl could you please elaborate/point to some docs on what the "alternative configuration pattern" is/means?

biltongza avatar Sep 02 '25 14:09 biltongza