简体   繁体   English

为什么我的 Azure Function 无法在 startup.cs 中正确加载我的环境变量?

[英]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.

相关问题 为什么启动我的Azure Function时没有使用startup.cs文件? - Why is the startup.cs file not being used when I start my Azure Function? 我的 Blazor 默认目录中的 Startup.cs 文件在哪里? - Where is my Startup.cs file in my Blazor default directory? Startup.cs 返回错误的环境 - Startup.cs returns wrong environment how to read azure app config values in C# startup.cs class in azure function - how to read azure app config values in C# startup.cs class in azure function 在 Azure Function 中引用类库 .dll 文件,是否需要在 Startup.cs 中设置服务? - Referencing class library .dll file in Azure Function, do I need to setup the services in Startup.cs? 连接到 .NET 3.1 中的 Entity Framework Core - Connect to Entity Framework Core in .NET 3.1 Azure Function Project Startup.cs 通过 Azure 门户中的“身份验证”刀片与 Startup.cs 中的手动保护 Function 应用程序 - Securing Function App via 'Authentication' blade in Azure Portal vs. manually in Startup.cs 是否可以访问 Startup.cs 中的任何 Azure Function 触发器详细信息/上下文? - Is it possible to access any Azure Function trigger details/context in Startup.cs? 寻找一种更有效的方式将我的存储库添加到 startup.cs 和 controller - Looking for a more efficient way to add my repositories in startup.cs and controller 在我的.Net框架应用程序中,“startup.cs”(.Net Core)的等价物是什么? - what is the equivalent of “startup.cs”(.Net Core) in my .Net framework application?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM