Azure Functions运行时找不到已部署DLL的原因咨询
问题描述
部署Azure Function后遇到以下运行时错误:
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'.
部署已成功,且在App Files中能看到该DLL。环境为C#/.NET 6.0、Functions v4.0,通过Deployment Center生成的GitHub Workflow Action部署。
相关配置信息
项目包引用
<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 Action部署脚本
# 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 }}
Startup类代码
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); } } }
可能的原因及解决方法
构建输出目录结构问题:GitHub Action中使用
dotnet build输出到./output,但Azure Functions运行时期望DLL位于站点根目录或正确的子目录。build命令会保留嵌套目录结构(比如bin/Release/net6.0),导致运行时找不到主程序集。
解决:改用dotnet publish替代dotnet build,publish命令会将所有依赖和主程序集整理到指定目录的根层级:dotnet publish --configuration Release --output ./output程序集依赖缺失或版本不匹配:主DLL存在,但它依赖的其他DLL可能缺失、版本不对,或者被错误排除在部署包外。比如Cosmonaut相关包的间接依赖未被正确复制。
解决:- 本地执行
dotnet publish,检查输出目录是否包含所有必要的依赖DLL; - 检查项目文件中是否有
PrivateAssets设置导致依赖未被复制; - 升级
Microsoft.NET.Sdk.Functions到最新稳定版,确保SDK与Functions Runtime兼容。
- 本地执行
Azure Functions运行时版本不兼容:虽然指定了Functions v4.0,但Azure门户中Function App的运行时版本可能未正确设置为
.NET 6。
解决:- 登录Azure门户,进入Function App;
- 在配置 > 常规设置中,确认运行时版本设置为
.NET 6 (Isolated)或.NET 6 (In-process)(根据项目模式选择)。
Startup类加载问题:错误提示提到
Error building configuration in an external startup class,说明Startup类加载时出错,可能是引用的程序集缺失,或者命名空间、类名配置错误。
解决:- 确认
[assembly: FunctionsStartup(typeof(BrokenToys.Api.Startup))]中的命名空间和类名完全正确; - 检查Startup类中引用的
BrokenToys.Api.Models.Games等本地项目是否已被正确构建并包含在部署包中。
- 确认
部署包文件权限问题:少数情况下,部署后的DLL文件权限不足,导致运行时无法读取。
解决:在Azure门户的Function App中,进入高级工具 > Kudu,打开CMD或PowerShell,检查站点wwwroot目录下的DLL文件权限,确保IIS AppPool用户有读取权限。
内容的提问来源于stack exchange,提问作者Jeffrey Meyer

