You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

.NET 8部署Azure App Service时出现Microsoft.AspNetCore.AzureAppServices.HostingStartup加载失败异常

.NET 8部署Azure App Service时出现Microsoft.AspNetCore.AzureAppServices.HostingStartup加载失败异常

我之前也碰到过几乎一模一样的问题,结合你的描述——本地运行正常、部署到App Service启动失败、已经更新了所有适配.NET 8的NuGet包,给你几个排查和解决的方向:

1. 优先确认Azure App Service的.NET运行时版本配置

你猜的没错,App Service的运行时版本不匹配是这类问题的高发原因:

  • 登录Azure Portal,找到你的目标App Service,进入「配置」->「常规设置」页面
  • 检查「Stack」选项:
    • 若为Windows环境,确认.NET版本选择的是.NET 8
    • 若为Linux环境,选择.NET 8 (STS)对应的选项
  • 修改后点击「保存」,务必重启App Service,再尝试启动服务

2. 检查项目的发布模式与配置

  • 打开项目的发布配置(比如Visual Studio的发布向导),确认部署模式:
    • 如果是框架依赖部署,必须保证App Service的运行时和项目的TargetFramework完全一致(即.NET 8)
    • 如果是独立部署,本地发布后检查输出文件夹,确认是否包含Microsoft.AspNetCore.AzureAppServices.HostingStartup.dll文件;如果缺失,尝试显式在项目中添加对应的NuGet包
  • 打开项目的.csproj文件,确认<TargetFramework>配置为:
    <TargetFramework>net8.0</TargetFramework>
    
  • 检查appsettings.json或App Service的环境变量中,是否存在ASPNETCORE_HOSTINGSTARTUPASSEMBLIES配置项,如果有非必要的启动集配置,建议暂时移除后测试

3. 清理残留的部署文件

旧版本的部署文件残留可能导致依赖冲突:

  • 通过Azure Portal的「高级工具」->「Kudu控制台」,进入网站的wwwroot目录,删除所有文件后重新部署
  • 本地端执行「生成」->「清理解决方案」,再重新生成项目后发布

4. 确认NuGet包的适配性

虽然你已经更新了包,但可以再做一次验证:

  • 检查Microsoft.AspNetCore.AzureAppServices.HostingStartup包的版本是否为8.x.x系列(适配.NET 8的版本)
  • 如果项目中没有显式引用这个包,但系统试图加载它,可能是其他依赖间接引入的,尝试显式添加该包到项目中,再重新发布

附你遇到的异常日志:

Microsoft.AspNetCore.Hosting.Diagnostics: Hosting startup assembly exception

System.InvalidOperationException: Startup assembly Microsoft.AspNetCore.AzureAppServices.HostingStartup failed to execute. See the inner exception for more details.

---> System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.AspNetCore.AzureAppServices.HostingStartup, Culture=neutral, PublicKeyToken=null'. The system cannot find the file specified.

File name: 'Microsoft.AspNetCore.AzureAppServices.HostingStartup, Culture=neutral, PublicKeyToken=null'

at System.Reflection.RuntimeAssembly.g____PInvoke|49_0(NativeAssemblyNameParts* __pAssemblyNameParts_native, ObjectHandleOnStack __requestingAssembly_native, StackCrawlMarkHandle __stackMark_native, Int32 __throwOnFileNotFound_native, ObjectHandleOnStack __assemblyLoadContext_native, ObjectHandleOnStack __retAssembly_native)

at System.Reflection.RuntimeAssembly.InternalLoad(NativeAssemblyNameParts* pAssemblyNameParts, ObjectHandleOnStack requestingAssembly, StackCrawlMarkHandle stackMark, Boolean throwOnFileNotFound, ObjectHandleOnStack assemblyLoadContext, ObjectHandleOnStack retAssembly)

at System.Reflection.RuntimeAssembly.InternalLoad(AssemblyName assemblyName, StackCrawlMark& stackMark, AssemblyLoadContext assemblyLoadContext, RuntimeAssembly requestingAssembly, Boolean throwOnFileNotFound)

at System.Reflection.Assembly.Load(AssemblyName assemblyRef)

at Microsoft.AspNetCore.Hosting.GenericWebHostBuilder.ExecuteHostingStartups()

备注:内容来源于stack exchange,提问作者jber Ibra

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.20 09:09:38