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

标签 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'.

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

什么会导致运行时无法看到主函数DLL?

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

app files

我的项目包引用:

  <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>

来自部署中心的部署 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 }}

这是我的启动类(class):

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);

        }
    }
}

最佳答案

Microsoft.Azure.WebJobs.Script: Error building configuration in an external startup class. System.Private.CoreLib: Could not load file or assembly ', Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.

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

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

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

  3. 检查程序集是否与函数的运行时版本兼容,并检查函数应用是否配置为在 Azure 门户中使用正确的运行时版本。 enter image description here

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

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

DLL 文件: enter image description here

enter image description here

而且我还观察到您在应用程序中使用的包 Microsoft.Azure.WebJobs.Scriptdeprecated .

enter image description here

关于c# - 为什么 Azure Functions 运行时找不到我的 DLL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75945599/

相关文章:

c# - 系统.Web.HttpCompileException :The type '<>' is defined in an assembly that is not referenced

azure - 在 UML 中重新创建 Azure 可扩展 Web 应用程序图

c# - EF Core 已跟踪具有相同 id 的实体

c# - Outlook 365 Rest API - 受众声明值无效

c# - 如何使用 AWSSDK.AppSync .net Core NuGet 包执行突变和查询数据?

c# - 传递 id,但不要在实际 url 中显示它

c# - 如何使用 Knockout.js 通过 ("map"创建 "observables") 复杂类型?

c# - 如何在不使用其控件的情况下仅从报告服务获取数据?

asp.net-mvc - Azure Web 角色 - 混合 MVC 和 WCF - 最佳实践

azure - 如何通过 Azure API 管理层向微服务添加 HTTPS/SSL 支持,而应用程序服务器不必支持 SSL?