Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WebClient construction overhead

Tags:

c#

webclient

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 collection of pre-constructed WebClient instances, or if creating them on the fly wouldn't be too much trouble?

like image 990
Barguast Avatar asked Mar 22 '10 12:03

Barguast


2 Answers

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.

like image 91
Sander Avatar answered Sep 20 '22 04:09

Sander


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.

like image 43
Darin Dimitrov Avatar answered Sep 19 '22 04:09

Darin Dimitrov