简体   繁体   English

在ASP.NET Web窗体中使用.NET 4.5 HttpClient

[英]using .NET 4.5 HttpClient in ASP.NET Web form

I Want to use .NET 4.5 HttpClient class in ASP.NET Web Form. 我想在ASP.NET Web窗体中使用.NET 4.5 HttpClient类。 I read in the C# 5.0 in a NutShell Book (page 663): 我在NutShell Book中阅读了C#5.0 (第663页):

Unlike with WebClient, to get the best performance with HttpClient, you must reuse same instance (otherwise things such as DNS resolution may be unnecessarily repeated.) HttpClient permits concurrent operations, . 与WebClient不同,要使用HttpClient获得最佳性能,必须重用相同的实例(否则可能会不必要地重复DNS解析等事情。)HttpClient允许并发操作,。 . .

In our ASP.NET web Form website ,we need to connect to another website. 在我们的ASP.NET Web窗体网站中,我们需要连接到另一个网站。 I have created an instance of HttpClient in the Application property to use a single instance in all requests. 我在Application属性中创建了一个HttpClient实例,以便在所有请求中使用单个实例。 in Global.asax something like this is written: 在Global.asax中写的是这样的:

Application.Add("MateCatWorker", new HttpClient());

My question is that:Is it a good practice? 我的问题是:这是一个好习惯吗?

According to the HttpClient Class on MSDN these methods are thread-safe: 根据MSDN上的HttpClient类 ,这些方法是线程安全的:

  • CancelPendingRequests CancelPendingRequests
  • DeleteAsyn DeleteAsyn
  • GetAsync GetAsync
  • GetByteArrayAsync GetByteArrayAsync
  • GetStreamAsync GetStreamAsync
  • GetStringAsync GetStringAsync
  • PostAsync PostAsync
  • PutAsync PutAsync
  • SendAsync SendAsync

HttpClient is intended to be instantiated once and re-used throughout the life of an application . HttpClient 旨在实例化一次,并在 应用程序的 整个 生命周期中 重复使用 Instantiating an HttpClient class for every request will exhaust the number of sockets available under heavy loads. 为每个请求实例化一个HttpClient类将耗尽重负载下可用的套接字数量。 This will result in SocketException errors. 这将导致SocketException错误。 Below is an example using HttpClient correctly. 下面是一个正确使用HttpClient的示例。

And I recommend not to store the HttpClient into Application , I'm not a fan of singleton but create a singleton class for that is much more better. 我建议不要将HttpClient存储到Application ,我不是单身人士的粉丝,但为此创建一个单例类要好得多。

public class GoodHttpClient
{
    // OK
    private static readonly _httpClient HttpClient;

    static GoodHttpClient()
    {
        _httpClient = new HttpClient();
    }
}

A good practice: no, not in my opinion. 一个好的做法:不,不在我看来。 Note that the application context might be synced between instances in a web farm, so I wouldn't really use that to store such variable. 请注意,应用程序上下文可能在Web场中的实例之间同步,因此我不会真正使用它来存储此类变量。

Does it work? 它有用吗? Yes, if you use one of the async methods mentioned in the remarks section of this MSDN article , since those are safe to use in multiple threads: 是的,如果您使用本MSDN文章的备注部分中提到的异步方法之一,因为在多个线程中使用它们是安全的:

  • CancelPendingRequests
  • DeleteAsync
  • GetAsync
  • GetByteArrayAsync
  • GetStreamAsync
  • GetStringAsync
  • PostAsync
  • PutAsync
  • SendAsync

Honestly, I would not worry too much about it. 老实说,我不会太担心它。 If you run calls in a tight loop, reuse the same object. 如果在紧密循环中运行调用,请重用相同的对象。 If not, don't reuse it. 如果没有,请不要重复使用它。

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

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