如何为OutOfProcess部署模式设置等效于IISServerOptions的MaxRequestBodySize?
在IIS OutOfProcess模式下配置MaxRequestBodySize的等效方案
当ASP.NET Core以OutOfProcess模式部署到IIS时,IISServerOptions不再生效——因为此时应用通过Kestrel独立运行,IIS仅作为反向代理。需要分别配置Kestrel和IIS反向代理的相关限制:
1. 配置Kestrel的请求体大小限制
在Program.cs中直接配置Kestrel选项,替代原IISServerOptions的配置:
var builder = WebApplication.CreateBuilder(args); // 配置Kestrel最大请求体大小(单位:字节) builder.WebHost.ConfigureKestrel(options => { options.Limits.MaxRequestBodySize = 12000; }); // 其他服务配置逻辑... var app = builder.Build(); // 中间件配置逻辑... app.Run();
2. 配置IIS反向代理的请求长度限制
还需修改web.config,调整IIS作为反向代理时的请求长度限制,避免IIS在请求到达Kestrel前截断大请求:
<?xml version="1.0" encoding="utf-8"?> <configuration> <system.webServer> <security> <requestFiltering> <!-- 单位:字节,值需≥Kestrel配置的大小 --> <requestLimits maxAllowedContentLength="12000" /> </requestFiltering> </security> <handlers> <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" /> </handlers> <aspNetCore processPath="dotnet" arguments=".\YourApp.dll" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" hostingModel="OutOfProcess" /> </system.webServer> </configuration>
注意事项
maxAllowedContentLength必须设置为大于或等于Kestrel中MaxRequestBodySize的值,防止IIS提前拦截请求。- 若需取消大小限制,可将
MaxRequestBodySize设为null,同时把maxAllowedContentLength设为4294967295(即4GB上限)。
内容的提问来源于stack exchange,提问作者user13663655
相关产品推荐
相关产品推荐

