c# - HttpClient 在 using 语句中

标签 c# httpclient

嗨,我读了这篇文章 You're using HttpClient wrong and it is destabilizing your software文章建议这 2

  1. 让你的 HttpClient 静态化
  2. 不要在 using 中处理或包装你的 HttpClient,除非你明确地在寻找特定的行为(例如导致你的服务失败)

现在像我这样的 c# 新手会像本文中发布的代码是他所说的会使应用程序失败的原始代码一样遵循它

using System;
using System.Net.Http;
namespace ConsoleApplication
{
    public class Program
    {
        public static void Main(string[] args)
        {
            Console.WriteLine("Starting connections");
            for(int i = 0; i<10; i++)
            {
                using(var client = new HttpClient())
                {
                    var result = client.GetAsync("http://aspnetmonsters.com").Result;
                    Console.WriteLine(result.StatusCode);
                }
            }
            Console.WriteLine("Connections done");
        }
    }
}

为了解决这个问题,他给出了这段代码:

using System;
using System.Net.Http;
namespace ConsoleApplication
{
    public class Program
    {
        private static HttpClient Client = new HttpClient();
        public static void Main(string[] args)
        {
            Console.WriteLine("Starting connections");
            for(int i = 0; i<10; i++)
            {
                var result = Client.GetAsync("http://aspnetmonsters.com").Result;
                Console.WriteLine(result.StatusCode);
            }
            Console.WriteLine("Connections done");
            Console.ReadLine();
        }
    }
}

现在像任何新手一样好奇我想到了using语句中的for循环,效果会和后者一样吗?

谢谢

最佳答案

不同之处在于,在顶部循环中,您总共创建了 10 个 HttpClient 对象,每个对象使用一次,然后处理每个对象,而在底部循环中,您只创建一个 HttpClient 并重新使用它。

文章的要点是,每次您想要进行 Web 服务调用时都创建一个新的 HttpClient 对象是非常低效的——而且完全没有必要。由于 HttpClient 不仅可重用而且是线程安全的,因此首选方法是制作一个 HttpClient 并重用它,直到您的程序完成建立 http 连接。

编辑

听起来你在问为什么不这样做:

using System;
using System.Net.Http;
namespace ConsoleApplication
{
    public class Program
    {

        public static void Main(string[] args)
        {
            Console.WriteLine("Starting connections");
            using (var client = new HttpClient())
            {
                for(int i = 0; i<10; i++)
                {
                    var result = Client.GetAsync("http://aspnetmonsters.com").Result;
                    Console.WriteLine(result.StatusCode);
                }
            }
            Console.WriteLine("Connections done");
            Console.ReadLine();
        }
    }
}

在这种特定情况下,没有区别。重要的是 HttpClient 被重用,直到每个请求都完成。在大多数现实情况下,为 HttpClient 提供静态属性最适合实现此目标。

他们说“不要使用 using”的原因是 using 暗示您的 HttpClient 是方法中的局部变量,而在大多数情况下这不是您想要的。在这种特定情况下,来自程序的每个 http 请求都发生在一个只调用一次的方法中,因此该方法的本地变量很好 - 您最终得到一个 HttpClient,它被重用,直到所有请求都发生,然后是处置。

关于c# - HttpClient 在 using 语句中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40187153/

相关文章:

java - 使用 Yahoo Finance API 检索股票行情

c# - .NET 应用程序最稳定的时间戳源是什么?

c# - 不明白为什么 ForEach 命令不起作用

java - 在新线程中使用 Apache HttpClient,我不知道它是否运行完毕

c# - 在 WinRT HttpClient 上设置请求内容类型

java - 来自 Windows 操作系统的 Apache HttpPut 请求非常慢

c# - 在我想要等待的方法上获得 Cannot await void

c# - 测试网络/互联网连接问题

c# - 不使用数组查找第二大数

java - 如何确保我的 HttpClient 4.1 不泄漏套接字?