gpt4 book ai didi

c# - 无法让 Linux 容器在隔离进程中作为 Azure 函数运行

转载 作者:行者123 更新时间:2023-12-03 01:19:29 25 4
gpt4 key购买 nike

函数应用在本地运行,但部署为 Linux 容器。已部署的函数未在门户中报告任何问题,我可以看到“函数”边栏选项卡中列出的三个函数。然而,这些都不起作用(一个是简单的 HTTP ping,它返回 502 Bad Gateway 响应)。

因此,门户中没有明显的问题,但检查日志会发现此重复出现的异常:

System.Threading.Tasks.TaskCanceledException at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess

格式化的消息是:

Failed to start a new language worker for runtime: dotnet-isolated

我怀疑这与站点设置 linuxFxVersion 的值有关。请注意,我尝试过使用 DOTNET|6.0DOTNET-ISOLATED|6.0。在这两种情况下,它似乎没有什么区别,无论如何,当我导出函数应用程序的 ARM 模板时,它已将 linuxFxVersion 设置为以 DOCKER| 为前缀的容器镜像的 URI >.

这似乎与 this specific advice from Microsoft 有关关于在 Linux 上固定主机版本。但我仍然不清楚我应该使用哪个值,无论如何,在另一个地方 another Microsoft document 的建议状态:

To support zip deployment and running from the deployment package on Linux, you also need to update the linuxFxVersion site config setting to DOTNET-ISOLATED|6.0.

无论如何,这是我的配置的详细信息。我遵循了 Microsoft 的所有指导,因此我希望有人能够发现我错过的内容...

项目文件的前两部分:

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<AzureFunctionsVersion>V4</AzureFunctionsVersion>
<OutputType>Exe</OutputType>
<ImplicitUsings>true</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.CosmosDB" Version="3.0.9" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http" Version="3.0.13" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="1.3.0" OutputItemType="Analyzer" />
<PackageReference Include="Microsoft.Azure.Functions.Worker" Version="1.5.2" />
</ItemGroup>

主机.json:

{
"version": "2.0",
"logging": {
"applicationInsights": {
"samplingSettings": {
"isEnabled": true,
"excludedTypes": "Request"
}
}
}
}
Program.cs 中的

Main 方法:

public static async Task Main()
{
var host = new HostBuilder()
.ConfigureFunctionsWorkerDefaults()
.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);
}

configurationBuilder.AddEnvironmentVariables();
})
.ConfigureServices((context, collection) => ConfigureServices(collection, context.Configuration, context.HostingEnvironment))
.Build()
.RunAsync();
}

功能配置管道设置以下应用设置:

FUNCTIONS_EXTENSION_VERSION: '~4'
FUNCTIONS_WORKER_RUNTIME: 'dotnet-isolated'

如上所述,在配置期间定义的我的站点配置的最新版本包括:

linuxFxVersion: 'DOTNET-ISOLATED|6.0'

Docker 镜像使用 mcr.microsoft.com/azure-functions/dotnet-isolated:4 作为已发布应用程序的基础镜像,并且 mcr.microsoft.com/dotnet/sdk:6.0 来构建它。

请告诉我我明显遗漏了一些东西。我们目前有两个功能应用程序,但都不能部署为点网隔离。这让我发疯!

最佳答案

您看到的异常不是直接来自您的应用程序,而是一个一般异常,没有来自 Azure Function 运行时进程的详细信息 - 因此它是无用的。

原因可以是任何原因,例如:忘记等待任务。

public static async Task Main()
{
var host = new HostBuilder()
.ConfigureFunctionsWorkerDefaults()
.ConfigureAppConfiguration(builder =>
{
builder
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT")}.json", optional: true)
.Build();
})
.ConfigureServices((context, collection) => ConfigureServices(collection, context.Configuration, context.HostingEnvironment))
.Build();

host.RunAsync(); // await host.RunAsync();
}

或者损坏的appsettings.json

{
"Logging": {
"LogLevel": {
"Default": "Warning",
"Microsoft": "Warning",
"Host": "Warning",
"Function": "Information",
"Host.Aggregator": "Information"
}

}

或者启动期间缺少配置调用。

public static async Task Main()
{
var host = new HostBuilder()
// .ConfigureFunctionsWorkerDefaults()
.ConfigureAppConfiguration(builder =>
{
builder
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT")}.json", optional: true)
.Build();
}))
.ConfigureServices((context, collection) => ConfigureServices(collection, context.Configuration, context.HostingEnvironment))
.Build();

await host.RunAsync();
}

启动应用程序的 Microsoft 运行时进程会捕获所有控制台日志,并在启动过程中出现异常时将其丢弃。

顺便说一句,您还应该启用代码分析,该功能默认在 .net 6 中未启用。

enter image description here

关于c# - 无法让 Linux 容器在隔离进程中作为 Azure 函数运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72273025/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com