.NET Framework 4.8 API部署Azure App Service后程序集加载异常求助
我有一个基于.NET Framework 4.8的API,本地运行正常,但部署到Azure App Service后出现问题。GitHub Actions部署流程执行成功,文件已复制到正确的wwwroot目录,但访问端点或App Service URL时出现程序集加载错误,疑似包映射问题。请问如何修复该问题?是否需要修改global.asax或web.config文件?
我的GitHub Action YML配置:
# Azure Web Apps Deploy 动作文档:https://github.com/Azure/webapps-deploy # 更多Azure GitHub Actions:https://github.com/Azure/actions name: Build and deploy ASP app to Azure Web App - MyAppApi on: push: branches: - my-branch workflow_dispatch: jobs: build: runs-on: windows-latest steps: - uses: actions/checkout@v4 - name: 设置MSBuild路径 uses: microsoft/setup-msbuild@v1.0.2 - name: 设置NuGet uses: NuGet/setup-nuget@v1.0.5 - name: 还原NuGet包 run: nuget restore - name: 发布到文件夹 run: | msbuild "MyProject.Api\My.Api.csproj" /nologo /verbosity:m /t:Build /t:pipelinePreDeployCopyAllFilesToOneFolder /p:_PackageTempDir="\published\" dir \published - name: 上传部署工件 uses: actions/upload-artifact@v3 with: name: ASP-app path: '/published/**' deploy: runs-on: windows-latest needs: build environment: name: 'Production' url: ${{ steps.deploy-to-webapp.outputs.webapp-url }} steps: - name: 下载构建工件 uses: actions/download-artifact@v3 with: name: ASP-app - name: 部署到Azure Web App id: deploy-to-webapp uses: azure/webapps-deploy@v2 with: app-name: 'MyAppApi' slot-name: 'Production' package: . publish-profile: ${{ secrets.AZUREAPPSERVICE_PUBLISHPROFILE_--- }}
访问App Service URL时的错误信息:
[FileLoadException: 无法加载文件或程序集“System.Web.Http.WebHost, Version=5.2.4.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35”或它的某一个依赖项。找到的程序集清单定义与程序集引用不匹配。 (异常来自 HRESULT:0x80131040)] CriticalWebApi.Convert.Areas.HelpPage.HelpPageAreaRegistration.RegisterArea(AreaRegistrationContext context) 在 C:\Work\CriticalPath\CriticalWebApi.Convert\Areas\HelpPage\HelpPageAreaRegistration.cs:24 System.Web.Mvc.AreaRegistration.CreateContextAndRegister(RouteCollection routes, Object state) +104 System.Web.Mvc.AreaRegistration.RegisterAllAreas(RouteCollection routes, IBuildManager buildManager, Object state) +190 System.Web.Mvc.AreaRegistration.RegisterAllAreas(Object state) +35 System.Web.Mvc.AreaRegistration.RegisterAllAreas() +7 CriticalPath.Zametek.Api.WebApiApplication.Application_Start() 在 D:\a\critical-path-api\critical-path-api\CriticalPath.Zametek.Api\Global.asax.cs:16 [HttpException (0x80004005): 无法加载文件或程序集“System.Web.Http.WebHost, Version=5.2.4.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35”或它的某一个依赖项。找到的程序集清单定义与程序集引用不匹配。 (异常来自 HRESULT:0x80131040)] System.Web.HttpApplicationFactory.EnsureAppStartCalledForIntegratedMode(HttpContext context, HttpApplication app) +475 System.Web.HttpApplication.RegisterEventSubscriptionsWithIIS(IntPtr appContext, HttpContext context, MethodInfo[] handlers) +118 System.Web.HttpApplication.InitSpecial(HttpApplicationState state, MethodInfo[] handlers, IntPtr appContext, HttpContext context) +176 System.Web.HttpApplicationFactory.GetSpecialApplicationInstance(IntPtr appContext, HttpContext context) +220 System.Web.Hosting.PipelineRuntime.InitializeApplication(IntPtr appContext) +303 [HttpException (0x80004005): 无法加载文件或程序集“System.Web.Http.WebHost, Version=5.2.4.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35”或它的某一个依赖项。找到的程序集清单定义与程序集引用不匹配。 (异常来自 HRESULT:0x80131040)] System.Web.HttpRuntime.FirstRequestInit(HttpContext context) +659 System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context) +89 System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr, HttpContext context) +189
这个错误是典型的程序集版本不匹配问题——代码引用的System.Web.Http.WebHost版本是5.2.4.0,但部署到Azure后实际找到的程序集版本和引用不一致。以下是具体修复步骤:
1. 配置web.config的程序集绑定重定向
不需要修改global.asax,优先在web.config中添加或修正绑定重定向,强制所有版本的目标程序集指向项目实际使用的版本:
<configuration> <runtime> <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> <!-- System.Web.Http.WebHost 绑定重定向 --> <dependentAssembly> <assemblyIdentity name="System.Web.Http.WebHost" publicKeyToken="31bf3856ad364e35" culture="neutral" /> <bindingRedirect oldVersion="0.0.0.0-5.2.4.0" newVersion="5.2.4.0" /> </dependentAssembly> <!-- 关联Web API程序集同步重定向 --> <dependentAssembly> <assemblyIdentity name="System.Web.Http" publicKeyToken="31bf3856ad364e35" culture="neutral" /> <bindingRedirect oldVersion="0.0.0.0-5.2.4.0" newVersion="5.2.4.0" /> </dependentAssembly> <dependentAssembly> <assemblyIdentity name="System.Net.Http.Formatting" publicKeyToken="31bf3856ad364e35" culture="neutral" /> <bindingRedirect oldVersion="0.0.0.0-5.2.4.0" newVersion="5.2.4.0" /> </dependentAssembly> </assemblyBinding> </runtime> </configuration>
如果已有相关配置,检查newVersion是否和NuGet包管理器中Microsoft.AspNet.WebApi.WebHost的实际版本一致。
2. 确认程序集的复制本地设置
在项目引用中找到System.Web.Http.WebHost,右键打开属性面板,将复制本地设置为True。确保构建时该程序集会被复制到输出目录,避免Azure端找不到正确的依赖文件。
3. 优化GitHub Actions的构建命令
当前使用的pipelinePreDeployCopyAllFilesToOneFolder目标可能无法完整收集所有依赖,建议改用标准的Publish目标:
msbuild "MyProject.Api\My.Api.csproj" /nologo /verbosity:m /t:Publish /p:PublishDir="\published\" /p:Configuration=Release /p:Platform="Any CPU"
Publish目标会自动处理依赖复制、配置转换等逻辑,确保部署包包含所有必要文件。
4. 清理Azure端旧文件残留
旧部署的残留程序集可能导致版本冲突,可通过以下方式清理:
- 在GitHub Actions中添加部署前清理步骤
- 登录Azure门户,手动删除App Service的wwwroot目录下所有旧文件后重新部署
5. 统一NuGet包版本
检查项目中所有Web API相关包(如Microsoft.AspNet.WebApi.Core、Microsoft.AspNet.WebApi.Client等)的版本,确保全部使用5.2.4版本,避免跨版本依赖引发的冲突。
核心修复点在web.config的绑定重定向和构建流程的标准化,无需修改global.asax。优先调整绑定配置,再优化MSBuild的Publish命令,确保依赖程序集被正确复制到部署包中。
内容的提问来源于stack exchange,提问作者afriedman111

