如何在Azure Functions中从本地配置读取应用配置存储的端点与环境
Got it, let's fix this. The key here is that Azure Functions automatically loads values from local.settings.json (when running locally) into the configuration system—so we just need to access that existing config before setting up Azure App Configuration.
Here's what you need to do step by step:
1. Update your local.settings.json
First, add the two values you need to the Values section of your local.settings.json file:
{ "IsEncrypted": false, "Values": { "AzureWebJobsStorage": "UseDevelopmentStorage=true", "FUNCTIONS_WORKER_RUNTIME": "dotnet", "ConfigurationStorePrimaryEndpoint": "https://your-config-store-name.azconfig.io", "AppConfigTargetLabel": "DEVELOPMENT" } }
(I used AppConfigTargetLabel instead of just DEVELOPMENT for clarity, but you can use any key name you prefer—just make sure it matches what you use in code.)
2. Modify your Startup.cs code
Instead of pulling values directly from environment variables, we'll first build the current configuration (which includes local.settings.json when running locally) and extract the values from there. This approach also works seamlessly when deployed to Azure (you'll just add these values as App Service application settings instead of local.settings.json):
public override void ConfigureAppConfiguration(IFunctionsConfigurationBuilder builder) { if (builder == null) throw new ArgumentNullException(nameof(builder)); // Keep your existing environment variables setup builder.ConfigurationBuilder.AddEnvironmentVariables(); // Build the current config to access local.settings.json values (or app settings in Azure) var currentConfig = builder.ConfigurationBuilder.Build(); // Pull the values from the config system instead of directly from env vars var configStoreEndpoint = currentConfig["ConfigurationStorePrimaryEndpoint"]; var appConfigLabel = currentConfig["AppConfigTargetLabel"]; var credentials = new DefaultAzureCredential(); builder.ConfigurationBuilder.AddAzureAppConfiguration(options => { options.Connect(new Uri(configStoreEndpoint), credentials) .Select(KeyFilter.Any, LabelFilter.Null) .Select(KeyFilter.Any, appConfigLabel); }); }
Why this works
When running locally, Azure Functions automatically injects all values from local.settings.json's Values section into the configuration provider chain. By calling builder.ConfigurationBuilder.Build() before setting up Azure App Configuration, we can access those values just like we would any other config source.
When you deploy your function to Azure, you won't need to change this code—just add ConfigurationStorePrimaryEndpoint and AppConfigTargetLabel as application settings in your Function App, and the config system will pick them up automatically.
Bonus Tip
If you need to encrypt sensitive values in local.settings.json (like if your config store endpoint has sensitive info), set "IsEncrypted": true and use the Azure Functions Core Tools command func settings encrypt to encrypt the file. The runtime will automatically decrypt it when running locally.
内容的提问来源于stack exchange,提问作者Matt Douhan

