Azure Web App中.NET 6 API升级后启动异常求助
Azure App Service多虚拟应用.NET 6升级异常解决方案
核心问题分析
- Azure App Service的虚拟应用共享同一进程池,InProcess托管的.NET应用会直接占用进程池进程,多个InProcess应用必然触发500.35错误;
- 混合InProcess/OutOfProcess托管模型违反ANCM进程隔离规则,导致500.34错误;
- API1反复重启的根源是默认端口绑定冲突:Azure为OutOfProcess应用分配随机端口,但API1硬绑定
localhost:5000,导致端口占用或ANCM无法正确转发请求。
针对性解决步骤
一、统一所有.NET 6 API为OutOfProcess托管
- 确保两个API的
.csproj文件配置一致:<PropertyGroup> <AspNetCoreHostingModel>OutOfProcess</AspNetCoreHostingModel> <AspNetCoreModuleName>AspNetCoreModuleV2</AspNetCoreModuleName> </PropertyGroup> - 检查
web.config,确保ANCM配置读取Azure分配的环境变量端口:
禁止硬编码<aspNetCore processPath="dotnet" arguments=".\YourApi.dll" stdoutLogEnabled="true" stdoutLogFile=".\logs\stdout"> <environmentVariables> <environmentVariable name="ASPNETCORE_URLS" value="http://0.0.0.0:%PORT%" /> </environmentVariables> </aspNetCore>localhost:5000,必须使用%PORT%环境变量。
二、修复API1端口绑定问题
- 删除API1中所有显式绑定端口的代码,比如
Program.cs中不要包含:
保留默认逻辑,让应用自动读取builder.WebHost.UseUrls("http://localhost:5000");ASPNETCORE_URLS环境变量。 - 确保
launchSettings.json仅用于本地开发,发布时不部署到Azure:<ItemGroup> <Content Remove="Properties\launchSettings.json" /> <None Include="Properties\launchSettings.json" CopyToPublishDirectory="Never" /> </ItemGroup>
三、清理Azure环境缓存
- 进入Azure App Service的高级工具 > Kudu控制台,执行以下命令:
dotnet dev-certs https --clean rm -r D:\home\site\wwwroot\api1\logs - 重启App Service,彻底清除旧进程与缓存。
四、检查虚拟应用隔离配置
- 确认Azure门户中两个API的虚拟应用路径(如
/api1、/api2)无重叠; - 检查每个虚拟应用的应用设置,确保
ASPNETCORE_ENVIRONMENT等变量独立无冲突。
内容的提问来源于stack exchange,提问作者Edwin van der Thiel
相关产品推荐
相关产品推荐

