ASP.NET Core 3.1部署Azure App Service接收1.6MB JSON抛EPIPE错误求助
问题排查与解决步骤
1. 确认部署后的web.config未被CI/CD流水线覆盖
你本地修改的web.config大概率在Azure DevOps发布过程中被自动生成的默认配置覆盖了,按以下步骤验证:
- 进入Azure门户对应App Service页面,左侧导航选择「高级工具 (Kudu)」,点击「转到」进入Kudu控制台
- 选择顶部菜单「Debug console」->「CMD」,进入
site/wwwroot目录,查看web.config的实际内容,确认你添加的maxRequestLength、maxAllowedContentLength配置是否存在
如果配置丢失,用以下任意一种方案解决: - 方案1:在项目
.csproj文件的根<PropertyGroup>节点中添加配置,禁止发布时自动生成web.config:
<IsWebConfigTransformDisabled>true</IsWebConfigTransformDisabled>
- 方案2:在项目根目录添加
web.release.config转换文件,发布时自动注入配置:
<?xml version="1.0" encoding="utf-8"?> <configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform"> <location path="." inheritInChildApplications="false"> <system.web xdt:Transform="InsertIfMissing"> <httpRuntime maxRequestLength="204800" xdt:Transform="InsertIfMissing" /> </system.web> <system.webServer> <security xdt:Transform="InsertIfMissing"> <requestFiltering xdt:Transform="InsertIfMissing"> <requestLimits maxAllowedContentLength="419430400" xdt:Transform="InsertIfMissing" /> </requestFiltering> </security> <aspNetCore requestTimeout="00:05:00" xdt:Transform="SetAttributes(requestTimeout)" /> </system.webServer> </location> </configuration>
2. 延长IIS InProcess模式请求超时
你使用的是inprocess托管模式,默认请求超时为2分钟,大文件base64传输如果网络波动很容易触发超时断开,在web.config的<aspNetCore>节点添加requestTimeout属性,延长超时时间到5分钟:
<aspNetCore processPath="dotnet" arguments=".\MyApp.API.dll" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" hostingModel="inprocess" requestTimeout="00:05:00" />
3. 显式配置System.Text.Json序列化限制
.NET Core 3.1默认使用System.Text.Json处理JSON请求,即使请求体大小符合限制,超长字符串也可能触发序列化拦截,在Startup.cs的ConfigureServices方法中添加配置:
services.AddControllers() .AddJsonOptions(options => { // 允许最大字符串长度为2GB options.JsonSerializerOptions.MaxStringLength = int.MaxValue; // 调整JSON最大深度,避免复杂结构被拦截 options.JsonSerializerOptions.MaxDepth = 128; });
4. 检查WAF(Web应用防火墙)拦截
如果你的App Service开启了WAF功能,或者绑定了Azure应用网关/CDN,这些前置服务默认有请求体大小限制和攻击拦截规则,会直接中断大请求:
- 进入Azure门户App Service页面,左侧导航选择「Web应用防火墙」,如果已开启,将「请求正文限制」调整到大于你需要的大小(比如20MB)
- 查看WAF日志,确认对应请求是否被规则拦截,必要时添加自定义放行规则
5. 验证请求是否到达应用层
开启应用日志排查请求是否进入应用:
- 将web.config中的
stdoutLogEnabled设置为true - 在
site/wwwroot目录下新建logs文件夹,给予读写权限 - 调用报错接口后查看logs目录下的日志,如果没有对应请求记录,说明请求在IIS/Azure前置服务层面就被拦截,重点排查前4个步骤;如果有报错日志,按照日志提示修复对应问题即可
内容的提问来源于stack exchange,提问作者hungariandude
相关产品推荐
相关产品推荐

