为什么我的Azure函数不会在startup.cs中正确加载我的环境变量?
我们在Windows App Service上的.NET 6.0上有一个C#预编译V4 Azure函数。我们在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
}
并且我们在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>
这在VS 2022中在本地运行良好,并从local.settings.json.json
中加载,但在null Connection上失败字符串检查,将InvalidOperationException丢弃
显示在云到云时,尽管环境变量正确设置:
我们在这里缺少什么?
We have a C# precompiled v4 Azure Function on .NET 6.0 on Windows App Service. We have the following code in Startup.cs:
public override void Configure(IFunctionsHostBuilder builder)
{
IConfiguration config = new ConfigurationBuilder()
.AddJsonFile(Path.Combine(builder.GetContext().ApplicationRootPath, quot;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:
<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:
What are we missing here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我们的修复程序是覆盖
configureAppConfiguration
以初始化配置,并且(因为我们需要在启动中的其他地方访问它)以将其设置为startup.cs类中的变量。由于
addsingleton
传递了一个操作,并且该操作直到需要服务才能调用,然后在我们的原始代码中config
,大概是不范围的。我对为什么在部署它时不起作用并不感到困惑,但是我 am 对为什么在本地工作感到困惑。无论哪种方式,上面的修复程序都可以很好地工作,因此解决了问题。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.Since
AddSingleton
is passed an action, and that action isn't called until the service is required, thenconfig
in our original code is presumably out of scope. 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.