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

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

  1. 确保两个API的.csproj文件配置一致:
    <PropertyGroup>
      <AspNetCoreHostingModel>OutOfProcess</AspNetCoreHostingModel>
      <AspNetCoreModuleName>AspNetCoreModuleV2</AspNetCoreModuleName>
    </PropertyGroup>
    
  2. 检查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端口绑定问题

  1. 删除API1中所有显式绑定端口的代码,比如Program.cs中不要包含:
    builder.WebHost.UseUrls("http://localhost:5000");
    
    保留默认逻辑,让应用自动读取ASPNETCORE_URLS环境变量。
  2. 确保launchSettings.json仅用于本地开发,发布时不部署到Azure:
    <ItemGroup>
      <Content Remove="Properties\launchSettings.json" />
      <None Include="Properties\launchSettings.json" CopyToPublishDirectory="Never" />
    </ItemGroup>
    

三、清理Azure环境缓存

  1. 进入Azure App Service的高级工具 > Kudu控制台,执行以下命令:
    dotnet dev-certs https --clean
    rm -r D:\home\site\wwwroot\api1\logs
    
  2. 重启App Service,彻底清除旧进程与缓存。

四、检查虚拟应用隔离配置

  1. 确认Azure门户中两个API的虚拟应用路径(如/api1、/api2)无重叠;
  2. 检查每个虚拟应用的应用设置,确保ASPNETCORE_ENVIRONMENT等变量独立无冲突。

内容的提问来源于stack exchange,提问作者Edwin van der Thiel

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.02 16:22:52