[英]Why would my Azure Function not load my environment variables correctly in startup.cs?
We have a C# precompiled v4 Azure Function on .NET 6.0 on Windows App Service.我们在 Windows 应用服务上的 .NET 6.0 上有一个 C# 预编译的 v4 Azure 函数。 We have the following code in Startup.cs:我们在 Startup.cs 中有以下代码:
public override void Configure(IFunctionsHostBuilder builder)
{
IConfiguration config = new ConfigurationBuilder()
.AddJsonFile(Path.Combine(builder.GetContext().ApplicationRootPath, $"local.settings.json"), optional: true, reloadOnChange: true)
.AddEnvironmentVariables()
.Build();
builder.Services.AddSingleton(config);
builder.Services.AddSingleton(s =>
{
var connectionString = config["Azure:CosmosDB:ConnectionString"];
if (string.IsNullOrEmpty(connectionString)) throw new InvalidOperationException(
"Please specify a valid CosmosDBConnection in the local.settings.json file or your Azure Functions Settings.");
return new CosmosClientBuilder(connectionString)
.Build();
});
// Additional services configuration
}
And we have the necessary package references in csproj:我们在 csproj 中有必要的包引用:
<ItemGroup>
<PackageReference Include="Microsoft.ApplicationInsights.WorkerService" Version="2.20.0" />
<PackageReference Include="Microsoft.Azure.Functions.Extensions" Version="1.1.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="6.0.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="6.0.0" />
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="4.1.0" />
</ItemGroup>
This works well running locally in VS 2022 and loading from local.settings.json
, but fails on the null connection string check, throwing the InvalidOperationException
shown, when deployed to the cloud, despite the environment variable being correctly set:这在 VS 2022 中本地运行并从local.settings.json
加载时运行良好,但在空连接字符串检查时失败,在部署到云时抛出显示的InvalidOperationException
,尽管环境变量已正确设置:
What are we missing here?我们在这里缺少什么?
The fix for us was to override ConfigureAppConfiguration
to initialize Config, and (since we need to access it elsewhere in startup) to set it as a variable on the Startup.cs class.我们的解决方法是重写ConfigureAppConfiguration
来初始化 Config,并且(因为我们需要在启动的其他地方访问它)将其设置为 Startup.cs 类的变量。
public IConfiguration Config { get; private set; }
public override void Configure(IFunctionsHostBuilder builder)
{
builder.Services.AddSingleton(s =>
{
var connectionString = Config["Azure:CosmosDB:ConnectionString"];
if (string.IsNullOrEmpty(connectionString)) throw new InvalidOperationException(
"Please specify a valid CosmosDBConnection in the local.settings.json file or your Azure Functions Settings.");
return new CosmosClientBuilder(connectionString)
.Build();
});
// Etc.
}
public override void ConfigureAppConfiguration(IFunctionsConfigurationBuilder builder)
{
builder.ConfigurationBuilder
.AddJsonFile(Path.Combine(builder.GetContext().ApplicationRootPath, $"local.settings.json"), optional: true, reloadOnChange: true)
.AddEnvironmentVariables();
Config = builder.ConfigurationBuilder.Build();
}
Since AddSingleton
is passed an action, and that action isn't called until the service is required, then config
in our original code is presumably out of scope.由于AddSingleton
传递了一个操作,并且在需要服务之前不会调用该操作,因此我们原始代码中的config
可能超出了范围。 I'm not confused as to why this didn't work when we deployed it, but I am confused as to why it worked locally.我并不困惑为什么在我们部署它时它不起作用,但我对它为什么在本地工作感到困惑。 Either way, the fix above works well in both, so the issue is resolved.无论哪种方式,上述修复都适用于两者,因此问题已解决。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.