Azure 函数在云中无法启动

问题描述 投票:0回答:1

我有一个 Azure 函数,它在本地运行良好,当我将其部署到 Azure 门户时,它抱怨遇到“加载函数时出错 - 主机运行时遇到错误 (InternalServerError)”,如您在下面的截图。

查看日志时,发生了很多事情,但有些错误似乎以固定模式发生(按时间降序排序):

> System.Threading.Tasks.TaskCanceledException at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess
> An exception occurred while receiving events for Event Hub: sessionmetadataupdated (Consumer Group: 'playbyplayfeedprocessor', Partition Id: '11'); Operation Id: '993c69e4-5b4d-4f71-80fd-61488d3bc08f'. Error Message: 'At least one receiver for the endpoint is created with epoch of '0', and so non-epoch receiver is not allowed. Either reconnect with a higher epoch, or make sure all epoch receivers are closed or disconnected. TrackingId:3b769308-3d08-49e2-bffe-0dc78c637f2a_B12, SystemTracker:metadata-v3-stadium-bb-trackman-dev:eventhub:sessionmetadataupdated~12287, Timestamp:2024-04-18T06:31:52 Reference:b4d803e0-0189-4e21-9401-5a98d268a1a2, TrackingId:cd151c95-2830-4e6d-8b38-0456b41bbbc5_B12, SystemTracker:metadata-v3-stadium-bb-trackman-dev:eventhub:sessionmetadataupdated~12287|playbyplayfeedprocessor, Timestamp:2024-04-18T06:31:52 TrackingId:5909ac77e7c046ea982e531736dbe67d_G0, SystemTracker:gateway5, Timestamp:2024-04-18T06:31:52 (sessionmetadataupdated).  For troubleshooting information, see https://aka.ms/azsdk/net/eventhubs/exceptions/troubleshoot'
> Failed to start a new language worker for runtime: dotnet-isolated.
> Exceeded language worker restart retry count for runtime:dotnet-isolated. Shutting down and proactively recycling the Functions Host to recover
> Unhandled exception. System.IO.FileNotFoundException: The configuration file 'appsettings.Release.json' was not found and is not optional. The expected physical path was '/azure-functions-host/appsettings.Release.json'.

这似乎表明它找不到

appsettings.Release.json
(在Azure Functions中使用
appsettings.json
是否是一个好主意,是另一个讨论,现在我必须处理它)。但我已经三次检查它是否在构建和发布时都被复制。我已经检查了 CI/CD 管道生成的工件,并且该文件确实存在于其中。

这是它在我的

Program.cs
文件中的加载方式:

using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using System;
using PlayByPlayDataFeed.Function.ServiceConfigurations;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Extensions.DependencyInjection;

string environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ??
    throw new InvalidOperationException("Could not find value for 'ASPNETCORE_ENVIRONMENT' in app settings.");

var host = new HostBuilder()
    .ConfigureFunctionsWebApplication()
    .ConfigureAppConfiguration((_, configBuilder) =>
    {
        configBuilder
            .AddJsonFile($"appsettings.json", optional: false, reloadOnChange: true)
            .AddJsonFile($"appsettings.{environment}.json",
                optional: false,
                reloadOnChange: true)
            .AddJsonFile("local.settings.json", true, true)
            .AddEnvironmentVariables();
    })
    .ConfigureServices((hostContext, services) =>
    {
        services.AddApplicationInsightsTelemetryWorkerService();
        services.ConfigureFunctionsApplicationInsights();
        ServiceConfigurations.ConfigureServices(services, hostContext.Configuration);
    })
    .Build();

host.Run();

根据Azure Functions 迁移指南,我非常确定我使用的是所有正确的包。这是我的

.csproj
文件中的样子:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net8.0</TargetFramework>
    <AzureFunctionsVersion>v4</AzureFunctionsVersion>
    <OutputType>Exe</OutputType>
    <ImplicitUsings>enabled</ImplicitUsings>
  </PropertyGroup>
  <ItemGroup>
    <FrameworkReference Include="Microsoft.AspNetCore.App" />
    <PackageReference Include="Microsoft.ApplicationInsights.WorkerService" Version="2.22.0" />
    <PackageReference Include="Microsoft.Azure.Functions.Extensions" Version="1.1.0" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker" Version="1.21.0" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker.ApplicationInsights" Version="1.2.0" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.EventHubs" Version="6.2.0" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http" Version="3.1.0" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http.AspNetCore" Version="1.2.1" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="1.15.1" />
  </ItemGroup>
  <ItemGroup>
      <Using Include="System.Threading.ExecutionContext" Alias="ExecutionContext"/>
  </ItemGroup>
  <ItemGroup>
    <ProjectReference Include="..\PlayByPlayDataFeed.Common\PlayByPlayDataFeed.Common.csproj" />
  </ItemGroup>
  <ItemGroup>
    <None Update="appsettings.Development.json">
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </None>
    <None Update="appsettings.json">
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </None>
    <None Update="appsettings.Production.json">
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </None>
    <None Update="appsettings.Release.json">
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </None>
    <None Update="host.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
    <None Update="local.settings.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
      <CopyToPublishDirectory>Never</CopyToPublishDirectory>
    </None>
  </ItemGroup>
  <ItemGroup>
    <Using Include="System.Threading.ExecutionContext" Alias="ExecutionContext" />
  </ItemGroup>
</Project>

我的设置(我删除了与函数运行时不直接相关的设置)是这样的:

"APPLICATIONINSIGHTS_CONNECTION_STRING": "InstrumentationKey=(...)",
"AzureWebJobsStorage": "DefaultEndpointsProtocol=(...)",
"FUNCTIONS_WORKER_RUNTIME": "dotnet-isolated",
"ASPNETCORE_ENVIRONMENT": "Release",
"FUNCTIONS_EXTENSION_VERSION": "~4",
"WEBSITE_CONTENTAZUREFILECONNECTIONSTRING": "DefaultEndpointsProtocol=https;AccountName=(...);EndpointSuffix=core.windows.net",
"WEBSITE_CONTENTSHARE": "(...)",
"WEBSITE_ENABLE_SYNC_UPDATE_SITE": "true",
"WEBSITE_RUN_FROM_PACKAGE": "1",
"WEBSITES_ENABLE_APP_SERVICE_STORAGE": "true"

我在 SO、Microsoft 和 Github 上读了很多帖子。但其中大多数是在本地运行时遇到问题的人。如果不是,那通常是因为他们错过了诸如

FUNCTIONS_WORKER_RUNTIME
之类的东西,而我却没有。还有,失踪的消息是怎么回事
appsettings.Release.json

我在这里缺少什么?

更新

这是构建工件的屏幕截图,显示所有可能的

appsettings.json
文件确实都包含在内:

c# azure .net-core azure-functions
1个回答
0
投票

它是appSettings.Production.json

您的环境名称错误。您使用了构建配置“Release”的名称,而不是环境名称。

© www.soinside.com 2019 - 2024. All rights reserved.