在ASP.NET 4.8 + IIS 10中限制多个请求的一种解决方法是使用MaxConcurrentRequestsPerCPU和MaxConcurrentThreadsPerCPU属性来控制并发请求的数量。以下是一个代码示例:
在Web.config文件中添加以下配置:
上面的配置将限制每个CPU核心同时处理的最大请求数为20。
另外,你还可以在Global.asax文件中使用Application_BeginRequest事件来进行请求限制。以下是一个示例:
void Application_BeginRequest(object sender, EventArgs e)
{
int maxConcurrentRequests = 20;
if (System.Threading.Interlocked.Increment(ref concurrentRequests) > maxConcurrentRequests)
{
// Reject the request
HttpContext.Current.Response.StatusCode = 503; // Service Unavailable
HttpContext.Current.Response.End();
}
}
void Application_EndRequest(object sender, EventArgs e)
{
System.Threading.Interlocked.Decrement(ref concurrentRequests);
}
上面的代码会在每个请求开始时进行计数,并在达到最大并发请求数后拒绝请求。
请注意,以上只是一种解决方法,具体的配置和代码可能需要根据你的应用程序的需求进行调整。
上一篇:ASP.NET 4.7.2 OWIN JWT Bearer Authentication Bearer prefix(ASP.NET 4.7.2 OWIN JWT承载身份验证承载前缀)
下一篇:Asp.Net 4.x 使用 JwtBearerAuthentication 的 events onTokenValidated