开发者

WebClient construction overhead

I have a client which makes a limited number of concurrent web requests. I use WebClient for this purpose. I currently have a pool of WebClient-s which I create once and use whichever one is idle.

This approach is becoming a little cumbersome though, and I'm wondering if there is any benefit to having a collectio开发者_StackOverflown of pre-constructed WebClient instances, or if creating them on the fly wouldn't be too much trouble?


Why on earth would you have a pool of WebClients in the first place? They are tiny, cheap objects. Did you determine by measuring that this is needed and beneficial? I assume not?

Object instantiation is almost always cheap. HTTP connections are not expensive, either. A WebClient pool is premature optimization. There is no need for it - feel free to create as many as you want.


According to Reflector all that the constructor of WebClient does is this:

public WebClient()
{
    this.m_Encoding = Encoding.Default;
    this.m_ContentLength = -1L;
}

So no you have not much benefit of having a pool.


If you are using .NET 4.0 you can parallelize the Web Requests. Check this out.

But to the real question, I wouldn't store the instances of the WebClient in an Array, if there is no need to re-use that instance on other places. Depending on the purpose and the kind of usage you could aswell have a Request Pool with a String Dictionary.

And then just re-use a WebClient instead of having multiple instances.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜