简体   繁体   English

IIS上的请求处理之前,HttpClient POST延迟较长,多线程

[英]HttpClient POST long delay before request processing on IIS, multithreading

I have a simple console application which sends HTTP POST from multiple threads: 我有一个简单的控制台应用程序,它从多个线程发送HTTP POST:

List<Task> tasks = new List<Task>();
for (int i = 0; i < 100; i++)
{
    tasks.Add(Task.Factory.StartNew(() => SendQuery(url1, query1)));
    tasks.Add(Task.Factory.StartNew(() => SendQuery(url2, query2)));
}

Task.WaitAll(tasks.ToArray());

SendQuery(string uri, string requestString) looks like this: SendQuery(string uri, string requestString)如下所示:

Uri url = new Uri(uri);

try
{
   using (HttpClient client = new HttpClient { Timeout = new TimeSpan(0, 0, 10, 0) })
   {
      StringContent content = new StringContent(requestString);
      content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
      HttpResponseMessage response = client.PostAsync(url, content).Result;
      response.EnsureSuccessStatusCode();
   }
}
catch (Exception ex)
{
   Console.WriteLine(ex);
}

The program works without any errors, all the queries are processed finally, but after filling tasks list each thread hangs on client.PostAsync(url, content).Result , after several minutes IIS starts to process queries. 该程序正常运行,所有查询最终都得到处理,但是在填充tasks列表之后,每个线程都挂在client.PostAsync(url, content).Result几分钟后 IIS开始处理查询。 Why does this delay occur? 为什么会出现这种延迟? What's happening during this time? 在这段时间内发生了什么事? I am using IIS 7.5 running on Windows Server 2008 R2 to host web-services which provide url1 and url2 . 我正在使用Windows Server 2008 R2上运行的IIS 7.5来托管提供url1url2 Web服务。

Set this value to 500 or 1000 at the start of your program and let us know the effects. 在程序开始时将此值设置为500或1000,然后让我们知道效果。 Your requests maybe getting throttled at the default value of 2. (depending on the .net version) 您的请求可能被限制为默认值2。(取决于.net版本)

ServicePointManager.DefaultConnectionLimit = 500;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM