WCF部署IIS后上传26MB PDF报413 Request Entity Too Large错误
WCF部署IIS后上传文件触发413 Request Entity Too Large解决方案
远程服务器返回了意外响应: (413) Request Entity Too Large。
你已完成WCF和前端应用的基础大小配置但部署后仍报错,可按以下顺序排查解决:
- 确认WCF服务自定义绑定规则已关联到服务端点
你当前的WCF配置中basicHttpBinding下的binding节点未指定name属性,也没有将服务端点关联到该自定义配置,会导致WCF默认使用64KB的消息大小限制。修改配置如下:
<!-- WCF服务web.config新增binding的name属性 --> <bindings> <basicHttpBinding> <binding name="LargeFileUploadBinding" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647" maxBufferSize="2147483647" transferMode="Buffered" closeTimeout="00:10:00" openTimeout="00:10:00" receiveTimeout="00:10:00" sendTimeout="00:10:00"> <readerQuotas maxDepth="2000000000" maxStringContentLength="2000000000" maxArrayLength="2000000000" maxBytesPerRead="2000000000" maxNameTableCharCount="2000000000" /> </binding> </basicHttpBinding> </bindings> <!-- 给WCF服务端点指定bindingConfiguration关联自定义配置 --> <services> <service name="你的服务实现类全限定名"> <endpoint address="" binding="basicHttpBinding" bindingConfiguration="LargeFileUploadBinding" contract="你的服务接口全限定名" /> </service> </services>
- 补充ASP.NET层面的上传大小限制配置
你当前缺失system.web节点下的httpRuntime配置,该配置默认最大请求大小为4MB,需要新增配置:
<system.web> <!-- maxRequestLength单位为KB,2097152对应2GB --> <httpRuntime maxRequestLength="2097152" executionTimeout="3600" /> </system.web>
- 检查IIS站点级别的上传限制是否覆盖了web.config配置
打开IIS管理器,定位到你部署的站点:
- 双击「请求筛选」功能,右侧操作栏点击「编辑功能设置」
- 确认「允许的最大内容长度(字节)」值为
2147483647,和web.config配置一致
- 若站点使用HTTPS,修改uploadReadAheadSize限制
IIS默认SSL场景下uploadReadAheadSize值为49152字节(约48KB),超过大小的请求会被直接拦截。可通过以下命令修改:
appcmd.exe set config "你的站点名称" -section:system.webServer/serverRuntime /uploadReadAheadSize:"2147483647" /commit:apphost
也可以在IIS站点的「配置编辑器」中定位到system.webServer/serverRuntime节点,手动修改uploadReadAheadSize值。
- 所有配置修改完成后,重启对应站点的应用程序池和站点,确保配置生效。
内容的提问来源于stack exchange,提问作者Ibrahim shaikh
相关产品推荐
相关产品推荐

