.net - 如何确定执行期间来自异步 httpclient 的并发请求数?

标签 .net visual-studio-2012 .net-4.5 async-await dotnet-httpclient

我有一个简单的控制台应用程序,如下所示:

    private static StringBuilder sb = new StringBuilder();
    private static HttpClient client = new HttpClient();

    private static async Task<HttpStatusCode> AccessTheWebAsync()
    {            
        HttpResponseMessage response = await client.GetAsync("http://www.google.com").ConfigureAwait(false);
        return response.StatusCode;
    }

    static void Main(string[] args)
    {
        Stopwatch sw = new Stopwatch();
        sw.Start();
        List<Task> tasks = new List<Task>();
        for (int i = 0; i < 10; i++)
            tasks.Add(AccessTheWebAsync());
        Task.WaitAll(tasks.ToArray());

        foreach (Task<HttpStatusCode> t in tasks)
            sb.Append((int)t.Result).Append(Environment.NewLine);
        Console.WriteLine(sb.ToString());            
        sw.Stop();
        Console.WriteLine("Run Completed, Time elapsed: {0}", sw.Elapsed);
        Console.ReadLine();
    }

这里我发起了 10 个异步 Web 请求,并在请求完成时收集响应代码并列出它们。

  1. 问题:是否可以使用 VS2012 来调试应用程序,以便我可以确定执行期间任何给定点发生的并发 Web 请求的数量?

原因是我发现您可以在 App.config 上更改某些内容:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>
  <system.net>
    <connectionManagement>
      <add address = "*" maxconnection = "10" />
    </connectionManagement>
  </system.net>
</configuration>

但是,我没有一个好的方法来确定这是否真的有效。

  1. 服务器是否设置此限制?

  2. 操作系统是否设置此限制?

  3. 此应用配置和/或 .NET 是否设置了此限制?

最佳答案

Is it possible to use VS2012 to debug the app in such a way where I can determine the number of concurrent web requests that are happening at any given point during execution?

是的,当然。使用Parallel TasksParallel Stacks window ,它们非常好。

  1. Do servers set this limit?

  2. Does the OS set this limit?

  3. Does this app config and/or .NET set this limit?

该设置指定 the maximum number of connections to a network host并且可以在机器或应用程序级别进行设置。服务器也可以为客户端设置连接限制,但这与 maxconnection 属性无关。

在您的示例中,实际最大连接数也可以通过线程池设置来限制。看看this KB entry了解更多详情。

关于.net - 如何确定执行期间来自异步 httpclient 的并发请求数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13014072/

相关文章:

asp.net - 连接到远程iis服务器2012以远程调试Web应用程序

c# - 我如何优化这个像素不透明度计算?

c# - 格式化字符串 : Value 20000 to $20k?

c# - 更改发布者 Visual Studio

c++ - VS2012 C++ 的 Delaunay 三角剖分?

c# - 无法在 Azure 开发存储上创建表

mef - 在谈论 MEF(托管扩展框架)和 .NET 4.5 时, "multiple scopes"是什么意思?

c# - 如何创建 ImmutableDictionary 的新实例?

c# - 长时间运行的进程暂停

c# - 如何在Winforms RichTextBox中实现基本的语法高亮?