HttpClient中的using语句 [英] HttpClient in using statement

查看:153
本文介绍了HttpClient中的using语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我阅读了这篇文章您使用的HttpClient错误,并且不稳定您的软件,文章建议使用这2

hi i read this article You're using HttpClient wrong and it is destabilizing your software the article is suggesting these 2

  1. 使您的HttpClient保持静态
  2. 除非您明确地正在寻找特定的行为(例如导致服务失败),否则请不要将HttpClient丢弃或包装在使用中.

现在像我这样使用c#的新手会像在文章上发布的代码一样跟随它,这里是他说的原始代码,他说这会使应用程序失败

now a newbie on c# like me would just follow it like the code posted on the article here is the original code the he said would make the application fail

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");
        }
    }
}

要解决此问题,他给出了以下代码:

and to fix it he gave this code:

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循环的任何新手一样,会感到好奇吗?

now being curious like any newbie i thought of the for loop inside the using statement will the effect be the same as the latter?

谢谢

推荐答案

区别在于,在顶部循环中,您将创建10个总共HttpClient对象,每个对象使用一次,然后在底部时处理每个对象,您只创建一个HttpClient并重新使用它.

The difference is that in the top loop, you're creating 10 total HttpClient objects, using each once, and then disposing of each, while in the bottom, you're creating just one HttpClient and reusing it.

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

The point of the article is that it's quite inefficient - and wholly unnecessary - to make a new HttpClient object every time you want to make a web service call. Since HttpClient is not only reusable but thread-safe, the preferred method is to make a single HttpClient and reuse it until your program is done making http connections.

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

It sounds like you were asking about why not this:

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设置静态属性是实现此目标的最合理方法.

In this specific case there's no difference. The important thing is that the HttpClient is reused until every request is done. In most realistic scenarios, having a static property for an HttpClient makes the most sense to accomplish this goal.

他们说不使用"的原因是是using意味着您的HttpClient是方法中的局部变量,并且在大多数情况下不是您想要的.在这种特定情况下,程序中的每个http请求都在一个方法中发生,该方法仅被调用一次,因此该方法本地的变量就可以了-您最终得到一个HttpClient,该HttpClient可以重复使用,直到发生所有请求,然后处置.

The reason they say "don't use using" is that using implies your HttpClient is a local variable within a method, and in most cases that isn't what you want. In this specific case, every http request from the program happens within one method which is called only once, so a variable that is local to that method is fine - you end up with one HttpClient that is reused until all requests have occurred and then is disposed.

这篇关于HttpClient中的using语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆