我正在将 IIS 上托管的 ASP.NET Framework 应用程序迁移到 Kestrel 上托管的 ASP.NET Core。
在 ASP.NET Framework 中,我有一个包含数十个自定义配置/优化的 web.config,例如:
<httpRuntime executionTimeout="120" maxUrlLength="8192" maxQueryStringLength="8192" />
问题是我找不到如何配置这些设置,例如使用 Kestrel 的 ASP.NET Core 中的 maxQueryStringLength
。
例如,在这个answer中,用户表示在 ASP.NET Core 中不可用,建议使用 IIS 或 NGINX。
如何在不使用 ASP.NET Core 中的 web.config
文件的情况下迁移我的 web.config
设置?我更愿意使用默认的 appsettings.json
文件。
最佳答案
您可以使用 ASP.NET Core Kestrel 限制来进行适当的配置
可以通过 Program.cs 中的配置来完成
#elif Limits
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
#region snippet_Limits
webBuilder.ConfigureKestrel(serverOptions =>
{
serverOptions.Limits.MaxConcurrentConnections = 100;
serverOptions.Limits.MaxConcurrentUpgradedConnections = 100;
serverOptions.Limits.MaxRequestBodySize = 10 * 1024;
serverOptions.Limits.MinRequestBodyDataRate =
new MinDataRate(bytesPerSecond: 100,
gracePeriod: TimeSpan.FromSeconds(10));
serverOptions.Limits.MinResponseDataRate =
new MinDataRate(bytesPerSecond: 100,
gracePeriod: TimeSpan.FromSeconds(10));
serverOptions.Listen(IPAddress.Loopback, 5000);
serverOptions.Listen(IPAddress.Loopback, 5001,
listenOptions =>
{
listenOptions.UseHttps("testCert.pfx",
"testPassword");
});
serverOptions.Limits.KeepAliveTimeout =
TimeSpan.FromMinutes(2);
serverOptions.Limits.RequestHeadersTimeout =
TimeSpan.FromMinutes(1);
})
#endregion
.UseStartup<Startup>();
});
#elif Port0
Kestrel 选项也可以使用配置提供程序进行设置。例如,文件配置提供程序 ( https://learn.microsoft.com/en-us/aspnet/core/fundamentals/configuration/?view=aspnetcore-3.1#file-configuration-provider ) 可以从 appsettings.json 或 appsettings.{Environment}.json 文件加载 Kestrel 配置:
{
"Kestrel": {
"Limits": {
"MaxConcurrentConnections": 100,
"MaxConcurrentUpgradedConnections": 100
},
"DisableStringReuse": true
}
}
此外,如果您需要通过代码管理这些事情,您可以尝试通过中间件处理它
public class RequestFilterMiddleware
{
private readonly RequestDelegate _next;
private const long _allowedRequestQueryStringMaxLength = 100;
private const long _allowedUrlStringMaxLength = 100;
public RequestFilterMiddleware(RequestDelegate next)
{
_next = next;
}
public async Task Invoke(HttpContext httpContext)
{
var queryStringLength = httpContext.Request.QueryString.Value?.Length;
if (queryStringLength > _allowedRequestQueryStringMaxLength)
{
//throw exception or send some appropriate http status code
}
var urlRequestLength = httpContext.Request.GetDisplayUrl().Length;
if (urlRequestLength > _allowedUrlStringMaxLength)
{
//throw exception or send some appropriate http status code
}
using (var TokenSource = System.Threading.CancellationTokenSource.CreateLinkedTokenSource(httpContext.RequestAborted))
{
httpContext.RequestAborted = TokenSource.Token;
var DelayTask = Task.Delay(500, TokenSource.Token);
var NextTask = _next.Invoke(httpContext);
if (await Task.WhenAny(DelayTask, NextTask).ConfigureAwait(false) == DelayTask)
{
//LogStuff for long running requests
}
TokenSource.Cancel();
}
await _next(httpContext);
}
}
不幸的是,Kestrel 目前不提供超时选项,要获取更多信息,您可以阅读此主题 https://github.com/dotnet/aspnetcore/issues/10079
但是,至少可以在生产中记录长时间运行的请求,以供以后优化之用。
关于c# - 如何将 ASP.NET Framework 配置迁移到 ASP.NET Core?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60675202/