system.net.httpclient和system.net.httpclienthandler在。net Framework 4.5中实现了IDisposable(通过System.Net.Http.HttpMessageInvoker)。

using语句文档说:

作为规则,当使用IDisposable对象时,应该声明和 在using语句中实例化它。

这个答案使用了这个模式:

var baseAddress = new Uri("http://example.com");
var cookieContainer = new CookieContainer();
using (var handler = new HttpClientHandler() { CookieContainer = cookieContainer })
using (var client = new HttpClient(handler) { BaseAddress = baseAddress })
{
    var content = new FormUrlEncodedContent(new[]
    {
        new KeyValuePair<string, string>("foo", "bar"),
        new KeyValuePair<string, string>("baz", "bazinga"),
    });
    cookieContainer.Add(baseAddress, new Cookie("CookieName", "cookie_value"));
    var result = client.PostAsync("/test", content).Result;
    result.EnsureSuccessStatusCode();
}

但是来自Microsoft的最明显的例子既没有显式调用也没有隐式调用Dispose()。例如:

最初的博客文章宣布HttpClient的发布。 HttpClient的实际MSDN文档。 BingTranslateSample GoogleMapsSample WorldBankSample

在公告的评论中,有人问微软员工:

在检查了你的样品后,我发现你没有进行处理 对HttpClient实例的操作。我已经使用了HttpClient的所有实例 在我的应用程序上使用声明,我认为这是正确的方式 因为HttpClient实现了IDisposable接口。我是在 正确的道路?

他的回答是:

一般来说,这是正确的,尽管你必须小心 "using"和async,因为它们在。net 4和。net 4.5中不能真正混合 可以在“using”语句中使用“await”。 顺便说一句,你可以重复使用同一个HttpClient,次数不限 通常情况下,您不会一直创建/处理它们。

第二段对这个问题来说是多余的,它不关心您可以使用HttpClient实例多少次,而是关心在您不再需要它之后是否有必要处理它。

(更新:事实上,第二段是答案的关键,如下文由@DPeden提供。)

所以我的问题是:

Is it necessary, given the current implementation (.NET Framework 4.5), to call Dispose() on HttpClient and HttpClientHandler instances? Clarification: by "necessary" I mean if there are any negative consequences for not disposing, such as resource leakage or data corruption risks. If it's not necessary, would it be a "good practice" anyway, since they implement IDisposable? If it's necessary (or recommended), is this code mentioned above implementing it safely (for .NET Framework 4.5)? If these classes don't require calling Dispose(), why were they implemented as IDisposable? If they require, or if it's a recommended practice, are the Microsoft examples misleading or unsafe?


当前回答

如果你想要释放HttpClient,你可以把它设置为一个资源池。在应用程序的末尾,分配资源池。

代码:

// Notice that IDisposable is not implemented here!
public interface HttpClientHandle
{
    HttpRequestHeaders DefaultRequestHeaders { get; }
    Uri BaseAddress { get; set; }
    // ...
    // All the other methods from peeking at HttpClient
}

public class HttpClientHander : HttpClient, HttpClientHandle, IDisposable
{
    public static ConditionalWeakTable<Uri, HttpClientHander> _httpClientsPool;
    public static HashSet<Uri> _uris;

    static HttpClientHander()
    {
        _httpClientsPool = new ConditionalWeakTable<Uri, HttpClientHander>();
        _uris = new HashSet<Uri>();
        SetupGlobalPoolFinalizer();
    }

    private DateTime _delayFinalization = DateTime.MinValue;
    private bool _isDisposed = false;

    public static HttpClientHandle GetHttpClientHandle(Uri baseUrl)
    {
        HttpClientHander httpClient = _httpClientsPool.GetOrCreateValue(baseUrl);
        _uris.Add(baseUrl);
        httpClient._delayFinalization = DateTime.MinValue;
        httpClient.BaseAddress = baseUrl;

        return httpClient;
    }

    void IDisposable.Dispose()
    {
        _isDisposed = true;
        GC.SuppressFinalize(this);

        base.Dispose();
    }

    ~HttpClientHander()
    {
        if (_delayFinalization == DateTime.MinValue)
            _delayFinalization = DateTime.UtcNow;
        if (DateTime.UtcNow.Subtract(_delayFinalization) < base.Timeout)
            GC.ReRegisterForFinalize(this);
    }

    private static void SetupGlobalPoolFinalizer()
    {
        AppDomain.CurrentDomain.ProcessExit +=
            (sender, eventArgs) => { FinalizeGlobalPool(); };
    }

    private static void FinalizeGlobalPool()
    {
        foreach (var key in _uris)
        {
            HttpClientHander value = null;
            if (_httpClientsPool.TryGetValue(key, out value))
                try { value.Dispose(); } catch { }
        }

        _uris.Clear();
        _httpClientsPool = null;
    }
}

var handler = HttpClientHander.GetHttpClientHandle(new Uri(“base url”)).

HttpClient作为一个接口,不能调用Dispose()。 Dispose()将被垃圾回收器以延迟的方式调用。 或者当程序通过析构函数清理对象时。 使用弱引用+延迟清理逻辑,因此只要它被频繁重用,它就会一直被使用。 它只为传递给它的每个基本URL分配一个新的HttpClient。原因由Ohad Schneider解释,答案如下。更改基本url时的不良行为。 HttpClientHandle允许在测试中进行mock

其他回答

因为似乎还没有人在这里提到它,在。net Core >=2.1和。net 5.0+中管理HttpClient和HttpClientHandler的最佳新方法是使用HttpClientFactory。

它以一种干净和易于使用的方式解决了上述大多数问题和陷阱。摘自Steve Gordon的博客文章:

将以下包添加到.Net Core(2.1.1或更高版本)项目中:

Microsoft.AspNetCore.All
Microsoft.Extensions.Http

将此添加到Startup.cs:

services.AddHttpClient();

注射使用:

[Route("api/[controller]")]
public class ValuesController : Controller
{
    private readonly IHttpClientFactory _httpClientFactory;

    public ValuesController(IHttpClientFactory httpClientFactory)
    {
        _httpClientFactory = httpClientFactory;
    }

    [HttpGet]
    public async Task<ActionResult> Get()
    {
        var client = _httpClientFactory.CreateClient();
        var result = await client.GetStringAsync("http://www.google.com");
        return Ok(result);
    }
}

探索Steve博客中的系列文章,了解更多功能。

我认为应该使用单例模式来避免创建HttpClient实例并一直关闭它。如果你使用的是。net 4.0,你可以使用下面的示例代码。有关单例模式检查的更多信息,请点击这里。

class HttpClientSingletonWrapper : HttpClient
{
    private static readonly Lazy<HttpClientSingletonWrapper> Lazy= new Lazy<HttpClientSingletonWrapper>(()=>new HttpClientSingletonWrapper()); 

    public static HttpClientSingletonWrapper Instance {get { return Lazy.Value; }}

    private HttpClientSingletonWrapper()
    {
    }
}

使用下面的代码。

var client = HttpClientSingletonWrapper.Instance;

在我的理解中,只有当Dispose()锁定稍后需要的资源(如特定连接)时才有必要调用Dispose()。总是建议释放你不再使用的资源,即使你不再需要它们,只是因为你通常不应该持有你不使用的资源。

微软的例子不一定是错误的。当应用程序退出时,将释放所使用的所有资源。在那个例子中,它几乎是在HttpClient被使用完之后立即发生的。在类似的情况下,显式调用Dispose()有点多余。

但是,一般来说,当一个类实现IDisposable时,只要您完全准备好并能够执行,您就应该Dispose()它的实例。我认为在像HttpClient这样的情况下尤其如此,其中没有明确地记录资源或连接是否被保持/打开。在连接将[很快]再次被重用的情况下,您将希望放弃它的dispose()处理——在这种情况下,您还没有“完全准备好”。

参见: IDisposable。Dispose方法和何时调用Dispose

目前的答案有点令人困惑和误导,它们遗漏了一些重要的DNS含义。我会试着把情况总结清楚。

Generally speaking most IDisposable objects should ideally be disposed when you are done with them, especially those that own Named/shared OS resources. HttpClient is no exception, since as Darrel Miller points out it allocates cancellation tokens, and request/response bodies can be unmanaged streams. However, the best practice for HttpClient says you should create one instance and reuse it as much as possible (using its thread-safe members in multi-threaded scenarios). Therefore, in most scenarios you'll never dispose of it simply because you will be needing it all the time. The problem with re-using the same HttpClient "forever" is that the underlying HTTP connection might remain open against the originally DNS-resolved IP, regardless of DNS changes. This can be an issue in scenarios like blue/green deployment and DNS-based failover. There are various approaches for dealing with this issue, the most reliable one involving the server sending out a Connection:close header after DNS changes take place. Another possibility involves recycling the HttpClient on the client side, either periodically or via some mechanism that learns about the DNS change. See https://github.com/dotnet/corefx/issues/11224 for more information (I suggest reading it carefully before blindly using the code suggested in the linked blog post).

在典型使用中(响应<2GB),没有必要Dispose HttpResponseMessages。

如果HttpClient方法的流内容没有被完全读取,那么它们的返回类型应该被丢弃。否则,CLR没有办法知道这些流可以被关闭,直到它们被垃圾回收。

如果你正在将数据读入字节[](例如GetByteArrayAsync)或字符串,所有的数据都被读取,所以不需要处理。 其他重载将默认读取流到2GB (HttpCompletionOption是ResponseContentRead, HttpClient。MaxResponseContentBufferSize默认为2GB)

如果你设置HttpCompletionOption为ResponseHeadersRead或者响应大于2GB,你应该清理。这可以通过在HttpResponseMessage上调用Dispose或在从HttpResonseMessage内容中获得的流上调用Dispose/Close来完成,也可以通过完全读取内容来完成。

是否在HttpClient上调用Dispose取决于是否想要取消挂起的请求。