Web客户端在.net中不释放套接字资源 [英] WebClient in .Net not releasing socket resources

查看:123
本文介绍了Web客户端在.net中不释放套接字资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有问题,一些简单的测试code,做我们的RESTful服务的一个体下载。看来,低水平插座没有被释放的调用Dispose。这是基本的code;

I am having problems with some simple testing code that does bulk downloads from one of our restful services. It appears that the low level socket is not being released by the call to Dispose. Here is the basic code;

foreach(...)
{
  using(WebClient client = new WebClient())
  {
    string results = client.DownloadString("http://host/request");
    client.Dispose();
  }
}

这将导致255圈后异常。我想加入这一行(通过一些建议,形成另一个计算器后)。

This causes an exception after 255 loops. I tried adding this line (via some suggestions form another stackoverflow post).

System.Net.ServicePointManager.DefaultConnectionLimit = 500;

然后我500圈后,得到一个异常。所以,在我看来是不被释放水平低套接字连接。

Then I get an exception after 500 loops. So, it seems to me the low level socket connection is not being released.

有其他人看到这个问题?你知道周围的工作。

Has anyone else seen this issue? Do you know a work around.

感谢

推荐答案

Web客户端不会覆盖处置组件将继承

WebClient does not override the Dispose it inherits from Component.

不应有需要调用Dispose,我没有得到你提到的错误。请注明其他计算器职务。

There should not be a need to call the Dispose and I do not get the error you mentioned. Please mention the other stackoverflow post.

事实上,Web客户端不保留,​​将需要dsiposing是pretty的无状态的任何状态。看着在反射器实施足以表明它的外观reasources后在这里(方法 DownloadDataInternal DownloadString 被称为):

In fact, WebClient does not keep any state that would need dsiposing and is pretty stateless. Looking at the implementation in Reflector makes it obvious that it looks after the reasources in here (method DownloadDataInternal is called from DownloadString):

private byte[] DownloadDataInternal(Uri address, out WebRequest request)
{
    byte[] buffer2;
    if (Logging.On)
    {
        Logging.Enter(Logging.Web, this, "DownloadData", address);
    }
    request = null;
    try
    {
        request = this.m_WebRequest = this.GetWebRequest(this.GetUri(address));
        buffer2 = this.DownloadBits(request, null, null, null);
    }
    catch (Exception exception)
    {
        if (((exception is ThreadAbortException) || (exception is StackOverflowException)) || (exception is OutOfMemoryException))
        {
            throw;
        }
        if (!(exception is WebException) && !(exception is SecurityException))
        {
            exception = new WebException(SR.GetString("net_webclient"), exception);
        }
        AbortRequest(request);
        throw exception;
    }
    catch
    {
        Exception exception2 = new WebException(SR.GetString("net_webclient"), new Exception(SR.GetString("net_nonClsCompliantException")));
        AbortRequest(request);
        throw exception2;
    }
    return buffer2;
}

这篇关于Web客户端在.net中不释放套接字资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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