试图将环境变量用作配置源时,容器化的Azure函数(V4,dotnet is-olated)断开

发布于 2025-01-30 14:22:03 字数 1657 浏览 2 评论 0 原文

从a /a>。我正在尝试使用功能运行时的.NET 6和V4在隔离过程中构建Azure函数,以作为Linux容器图像运行。

我遵循 Microsoft Guide 要使用HTTP触发器进行工作,运行示例功能。然后,我开始将代码添加到应用程序并立即被阻止的过程。这是我 program main 方法:

public static void Main()
{
    var host = new HostBuilder()
        .ConfigureFunctionsWorkerDefaults()
        .ConfigureAppConfiguration((context, builder) =>
        {
            builder.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
            builder.AddEnvironmentVariables();
        })
        .ConfigureServices((context, collection) => ConfigureServices(collection, context.Configuration))
        .Build();

    host.Run();
}

我发现该函数正常工作,直到我添加行 builder.addenvironmentvariables(); 。只有此更改,该功能突然在启动时突然崩溃,例外:

system.invalidoperationException in microsoft.azure.webjobs.script.workers.rpc.rpc.rpcfunctioninvocationdispatcherloadbalancer.getLanguageWormageWorkerWorkerChannel

中隐藏着一条消息,那里隐藏着一条消息,说“我没有找到任何初始化的语言工人”我不会告诉你什么或为什么”。

在我的特殊用例中,我取决于能够将环境变量用作配置源的功能,因为我的管道将将其传递给ARM模板作为站点配置。所以我的问题是,如何在孤立模式下进行?

This follows on from a previous question. I'm trying to build an Azure function to run as a Linux container image in an isolated process using .NET 6 and v4 of the functions runtime.

I have followed the Microsoft guide to get a working, running sample function with HTTP trigger. I have then begun the process of adding my code to the app and immediately become blocked. This is the Main method of my Program:

public static void Main()
{
    var host = new HostBuilder()
        .ConfigureFunctionsWorkerDefaults()
        .ConfigureAppConfiguration((context, builder) =>
        {
            builder.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
            builder.AddEnvironmentVariables();
        })
        .ConfigureServices((context, collection) => ConfigureServices(collection, context.Configuration))
        .Build();

    host.Run();
}

What I've discovered is that the function works fine until I add the line builder.AddEnvironmentVariables();. With this change only, the function suddenly craps out on startup with the exception:

System.InvalidOperationException at Microsoft.Azure.WebJobs.Script.Workers.Rpc.RpcFunctionInvocationDispatcherLoadBalancer.GetLanguageWorkerChannel

There's a message hidden in there saying "Did not find any initialized language workers" which I understand is Azure Function language for "something bad happened during startup but I'm not going to tell you what or why".

In my particular use case I'm depending on the function being able to use environment variables as a config source because my pipeline will be passing these to the ARM template as site config. So my question is, how is this possible in isolated mode?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

晨曦慕雪 2025-02-06 14:22:03

好吧,事实证明,环境变量是通过调用 configurefunctionsworkerDefaults 自动添加的。您可以在。我不知道为什么在传统的dotnet核心方式中添加环境变量作为配置源会导致整个事情爆炸,但至少我没有被封锁。希望这对别人有帮助。

还指出,这将影响您使用 configureAppConfiguration configurefunctionsworkerDefaults Extensions的顺序。由于后者将控制台输入和环境变量作为配置源,因此,如果您还使用appSettings.json。 em> configureAppConfiguration 。因此,我的程序看起来像这样:

public class Program
{
    public static async Task Main()
    {
        var host = new HostBuilder()
            .ConfigureAppConfiguration((context, configurationBuilder) =>
            {
                configurationBuilder.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);

                if (context.HostingEnvironment.IsDevelopment())
                {
                    configurationBuilder.AddJsonFile("local.settings.json", optional: true, reloadOnChange: true);
                }
                else
                {
                    configurationBuilder.AddJsonFile($"appsettings.{context.HostingEnvironment.EnvironmentName}.json", optional: true, reloadOnChange: true);
                }
            })
            .ConfigureFunctionsWorkerDefaults()
            .ConfigureServices((context, collection) => ConfigureServices(collection, context.Configuration))
            .Build();

        await host.RunAsync();
    }

    private static void ConfigureServices(IServiceCollection services, IConfiguration configuration)
    {
        services
            .AddLogging()
            .AddDomainLayer(configuration)
            .AddHttpLayer(configuration)
            .AddKeyVaultLayer(configuration);
    }
}

Well it turns out that environment variables are added automatically by the call to ConfigureFunctionsWorkerDefaults. You can see this here in WorkerHostBuilderExtensions. I have no idea why attempting to add environment variables as a config source in the traditional dotnet core way causes the whole thing to blow up, but at least I'm unblocked. Hope this helps someone else.

It's woth also pointing out that this will affect the order in which you use the ConfigureAppConfiguration and ConfigureFunctionsWorkerDefaults extensions. Since the latter adds console input and environment variables as config sources, if you also use an appsettings.json on disk it's likely you'll want environment variables to override its config so ConfigureFunctionsWorkerDefaults would need to be called after ConfigureAppConfiguration. Therefore my Program looks like this:

public class Program
{
    public static async Task Main()
    {
        var host = new HostBuilder()
            .ConfigureAppConfiguration((context, configurationBuilder) =>
            {
                configurationBuilder.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);

                if (context.HostingEnvironment.IsDevelopment())
                {
                    configurationBuilder.AddJsonFile("local.settings.json", optional: true, reloadOnChange: true);
                }
                else
                {
                    configurationBuilder.AddJsonFile(
quot;appsettings.{context.HostingEnvironment.EnvironmentName}.json", optional: true, reloadOnChange: true);
                }
            })
            .ConfigureFunctionsWorkerDefaults()
            .ConfigureServices((context, collection) => ConfigureServices(collection, context.Configuration))
            .Build();

        await host.RunAsync();
    }

    private static void ConfigureServices(IServiceCollection services, IConfiguration configuration)
    {
        services
            .AddLogging()
            .AddDomainLayer(configuration)
            .AddHttpLayer(configuration)
            .AddKeyVaultLayer(configuration);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文