microsoft / microsoft/aspire

Azure function rewriting environment problem

Open
#6,661 17 comments 0 reactions 1 assignee Assigned to @phenning View on GitHub
area-tooling azure azure-functions
Dominant language
C#
Stars
6.3k
Forks
991
Avg merge
2d 15h
Merged PRs (30d)
196

Description

### Is there an existing issue for this?

- [x] I have searched the existing issues

### Describe the bug

For Aspire - 9.1.0-preview.1.24561.5, I am experiencing a problem regarding poor loading of environments settings for Azure Function. I wrote about it [here](https://github.com/dotnet/aspire/issues/920) and was directed to create a new bug.

The problem occurred in previous versions as well.
I have in AzureFunctionsTest.Functions local.settings.json with empty values and in AzureFunctionsTest.AppHost added connection strings to appsettings with filled values.

I then register the connection strings in Aspire program.cs and then add them as with refrence to the Azure Function. However, if I use debug to look at the values loaded in configuration in program.cs in Azure Function, sometimes they are all filled, sometimes they are all empty, and most often it varies.

I have prepared a repository - https://github.com/petrkasnal/AspireLocalSettingsProblem

Aspire
```
var messaging = builder.AddConnectionString("Messaging");
var messaging2 = builder.AddConnectionString("Messaging2");
var messaging3 = builder.AddConnectionString("Messaging3");
var messaging4 = builder.AddConnectionString("Messaging4");
var messaging5 = builder.AddConnectionString("Messaging5");
var messaging6 = builder.AddConnectionString("Messaging6");

builder.AddAzureFunctionsProject("funcapp")
.WithReference(messaging)
.WithReference(messaging2)
.WithReference(messaging3)
.WithReference(messaging4)
.WithReference(messaging5)
.WithReference(messaging6);
```

Aspire - local.settings
```
{
"IsEncrypted": false,
"Values": {
"FUNCTIONS_WORKER_RUNTIME": "dotnet-isolated",
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
"ServiceBusConnection__fullyQualifiedNamespace": ""
},
"ConnectionStrings": {
"Messaging": "",
"Messaging2": "",
"Messaging3": "",
"Messaging4": "",
"Messaging5": "",
"Messaging6": ""
}
}

```
Function
```
var value = builder.Configuration.GetConnectionString("Messaging");
var value2 = builder.Configuration.GetConnectionString("Messaging2");
var value3 = builder.Configuration.GetConnectionString("Messaging3");
var value4 = builder.Configuration.GetConnectionString("Messaging4");
var value5 = builder.Configuration.GetConnectionString("Messaging5");
var value6 = builder.Configuration.GetConnectionString("Messaging6");
```

Function - appsettings.json
```
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning",
"Aspire.Hosting.Dcp": "Warning"
}
},
"ConnectionStrings": {
"Messaging": "test.servicebus.windows.net",
"Messaging2": "test.servicebus.windows.net",
"Messaging3": "test.servicebus.windows.net",
"Messaging4": "test.servicebus.windows.net",
"Messaging5": "test.servicebus.windows.net",
"Messaging6": "test.servicebus.windows.net"
}
}

```

Results - Its totally randoml:
![Image](https://github.com/user-attachments/assets/49bce0af-8c67-4b1d-a23a-c1f8c9fbe2ae)
![Image](https://github.com/user-attachments/assets/d50283f4-be4b-4269-9a49-e24dd305344c)
![Image](https://github.com/user-attachments/assets/0054c2fd-56ba-425a-b633-079b5ae0be1e)
![Image](https://github.com/user-attachments/assets/197e20fe-b26c-4b63-884a-392c35946f77)

### Expected Behavior

I would expect the same behavior as the API, where only the values set in Aspire are loaded.

### Steps To Reproduce

Open repository - https://github.com/petrkasnal/AspireLocalSettingsProblem
Start Aspire application
Set breakpoint to AzureFunctionsTest.Functions - Program.cs
Run it a few times to see that the values go differently

### Exceptions (if any)

_No response_

### .NET Version info

9.0.0-rc.2.24473.5

### Anything else?

_No response_

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.