为什么 Azure Functions 运行时找不到我的 DLL?

Jef*_*yer 7 c# azure .net-core

我正在尝试运行 Azure 函数,但在部署时遇到以下错误 --

Microsoft.Azure.WebJobs.Script: Error building configuration in an external startup class. 
System.Private.CoreLib: Could not load file or assembly '<MyAssembly>, Version=1.0.0.0, 
Culture=neutral, PublicKeyToken=null'.
Run Code Online (Sandbox Code Playgroud)

但是我的部署成功了,我可以在App Files中看到DLL。我在 Functions v4.0 上运行 C# / .net 6.0 并使用部署中心生成的 GitHub 工作流操作进行部署。

什么会导致运行时看不到main函数DLL?

添加应用程序文件的图像:

应用程序文件

我的项目包参考:

  <ItemGroup>
    <PackageReference Include="Azure.Identity" Version="1.8.2" />
    <PackageReference Include="Azure.Security.KeyVault.Secrets" Version="4.5.0" />
    <PackageReference Include="Azure.Storage.Blobs" Version="12.15.1" />
    <PackageReference Include="Cosmonaut" Version="2.11.3" />
    <PackageReference Include="Cosmonaut.ApplicationInsights" Version="2.1.1" />
    <PackageReference Include="Cosmonaut.Extensions.Microsoft.DependencyInjection" Version="2.3.0" />
    <PackageReference Include="Cosmonaut.NestedQueries" Version="2.11.4" />
    <PackageReference Include="Microsoft.Azure.AppConfiguration.AspNetCore" Version="6.0.0" />
    <PackageReference Include="Microsoft.Azure.Functions.Extensions" Version="1.1.0" />
    <PackageReference Include="Microsoft.Azure.Services.AppAuthentication" Version="1.6.2" />
    <PackageReference Include="Microsoft.Azure.WebJobs.Extensions.SendGrid" Version="3.0.3" />
    <PackageReference Include="Microsoft.Azure.WebJobs.Extensions.Storage" Version="5.1.1" />
    <PackageReference Include="Microsoft.CodeAnalysis.NetAnalyzers" Version="7.0.1">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
    <PackageReference Include="Microsoft.Extensions.Azure" Version="1.6.3" />
    <PackageReference Include="Microsoft.Extensions.Hosting" Version="7.0.1" />
    <PackageReference Include="Microsoft.NET.Sdk.Functions" Version="4.1.3" />
  </ItemGroup>
Run Code Online (Sandbox Code Playgroud)

来自部署中心的部署 GitHub 操作:

# Docs for the Azure Web Apps Deploy action: https://github.com/azure/functions-action
# More GitHub Actions for Azure: https://github.com/Azure/actions

name: Build and deploy dotnet core app to Azure Function App - brokentoysapi3

on:
  push:
    branches:
      - master
  workflow_dispatch:

env:
  AZURE_FUNCTIONAPP_PACKAGE_PATH: './api/_new/BrokenToys.Api/' # set this to the path to your web app project, defaults to the repository root
  DOTNET_VERSION: '6.0.x' # set this to the dotnet version to use

jobs:
  build-and-deploy:
    runs-on: windows-latest
    steps:
      - name: 'Checkout GitHub Action'
        uses: actions/checkout@v2

      - name: Setup DotNet ${{ env.DOTNET_VERSION }} Environment
        uses: actions/setup-dotnet@v1
        with:
          dotnet-version: ${{ env.DOTNET_VERSION }}

      - name: 'Resolve Project Dependencies Using Dotnet'
        shell: pwsh
        run: |
          pushd './${{ env.AZURE_FUNCTIONAPP_PACKAGE_PATH }}'
          dotnet build --configuration Release --output ./output
          popd

      - name: 'Run Azure Functions Action'
        uses: Azure/functions-action@v1
        id: fa
        with:
          app-name: 'brokentoysapi3'
          slot-name: 'Production'
          package: '${{ env.AZURE_FUNCTIONAPP_PACKAGE_PATH }}/output'
          publish-profile: ${{ secrets.AZUREAPPSERVICE_PUBLISHPROFILE_E20EFE8EF7D84C4AA5B5C75BE0B66D0F }}
Run Code Online (Sandbox Code Playgroud)

这是我的启动课程:

using BrokenToys.Api.Models.Games;
using Cosmonaut;
using Cosmonaut.Extensions.Microsoft.DependencyInjection;
using Microsoft.Azure.Functions.Extensions.DependencyInjection;
using Microsoft.Extensions.Configuration;

[assembly: FunctionsStartup(typeof(BrokenToys.Api.Startup))]

namespace BrokenToys.Api
{
    public class Startup : FunctionsStartup
    {
        public static IConfiguration Configuration { set; get; }


        public override void Configure(IFunctionsHostBuilder builder)
        {
 
            var cosmosSettings = 
                    new CosmosStoreSettings
                    ("BrokenToys", "https://<mycosmosdb>.documents.azure.com:443/", "<mykey>");

            builder.Services.AddCosmosStore<Score>(cosmosSettings);
            builder.Services.AddCosmosStore<Player>(cosmosSettings);

        }
    }
}

Run Code Online (Sandbox Code Playgroud)

Pra*_* KV 6

Microsoft.Azure.WebJobs.Script:在外部启动类中构建配置时出错。System.Private.CoreLib:无法加载文件或程序集“,版本= 1.0.0.0,文化=中性,PublicKeyToken = null”。

此错误表明运行时无法加载您的函数的程序集。这可能是由于多种原因造成的,例如文件路径不正确或函数名称不正确。检查以下步骤是否有助于解决问题:

  1. 验证错误消息中的程序集名称和版本是否与“App Files”文件夹中程序集的名称和版本相匹配。如果它们不匹配,您可能需要重建项目并重新部署它。

  2. 检查程序集是否位于“App Files”文件夹中的正确位置。它应该位于 bin 文件夹中,该文件夹位于函数应用的根目录中。

  3. 检查程序集是否与函数的运行时版本兼容,并检查函数应用是否配置为在 Azure 门户中使用正确的运行时版本。 在此输入图像描述

  4. 检查您的 GitHub 工作流操作是否已正确配置以部署您的函数应用。确保正在部署所有文件都已部署到正确的位置。

我在我的环境中重现了相同的内容,并且它运行正常,没有任何问题,如下所示:

DLL 文件: 在此输入图像描述

在此输入图像描述

而且我还观察到您在应用程序中使用的软件包Microsoft.Azure.WebJobs.Script被弃用

在此输入图像描述

  • 所有这些包都内部链接到 Function SDK。`Microsoft.Azure.WebJobs.Script` 具有此 [NuGet 站点](https://i.imgur.com/aKWyVVm.png) 中所示的依赖项,因此应根据您的函数 SDK (v3或 v4) 和 .NET 版本。请参阅 SO 问题 [67338863](/sf/answers/4713720441/) (3认同)
  • 嗯,我不确定该脚本包来自哪里。我将发布项目中我的包的图片,但我没有参考它...... (2认同)