8

I am calling a webservice in multi threaded environment. Lot of my calls fail due to operation time out or bad request but none of the calls fail if I do it in linear fashion which means there is problem with invoking webservice with multiple threads. After lot of analysis, I found out that there is limit of concurrent connection which is causing these exception so I fixed it by adding below code.

 ServicePointManager.DefaultConnectionLimit = 2 * _ThreadCount;

What I dont know is the possible disadvantage of increasing this limit. By default, Connection limit is 2. If anyone knows any disadvantages, please do let me know.

Viru
  • 2,228
  • 2
  • 17
  • 28

3 Answers3

4

The MSDN says:

Changing the DefaultConnectionLimit property has no effect on existing ServicePoint objects; it affects only ServicePoint objects that are initialized after the change. If the value of this property has not been set either directly or through configuration, the value defaults to the constant DefaultPersistentConnectionLimit.

and

Note

Any changes to the DefaultConnectionLimit property affect both HTTP 1.0 and HTTP 1.1 connections. It is not possible to separately alter the connection limit for HTTP 1.0 and HTTP 1.1 protocols. When used in the server environment (ASP.NET) DefaultConnectionLimit defaults to higher number of connections, which is 10.

Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
  • MSDN also states that default value is MaxValue but when I debug I see default value as 2 – Viru Jan 21 '16 at 11:46
  • @Viru:- Yes it is by default set to MaxValue. Are you sure it was not changed previously? – Rahul Tripathi Jan 21 '16 at 11:47
  • No where in my application or config, I am setting this value but if I undertstand it correctly, it is system level configuration so there might be configuration in machine.config which I did not check....anyway, I am more interested in finding out any possible disadvantages – Viru Jan 21 '16 at 11:49
  • @Viru:- As far as I know there is no such disadvantage to it. – Rahul Tripathi Jan 21 '16 at 11:50
3

No, there should not be any disadvantages other than that your AppDomain will consume more resources. But in your case it's trivial.

In fact, it can actually help you use less resources (memory) since pending requests are queued up internally in the ServicePoint. Read here for more information: Big size of ServicePoint object after several hours sending HTTP request in parallel

let me give you the picture....I have around 46K tasks,these task are ran in batch of 100 (each task will call webservice) so I have 100 threads calling webserivce simultaneously. is it still trivial? or will it have some impact in my case?

It will of course have an impact. But the impact depends on many factors. A service point is per host.

If your tasks are mostly against the same host, increase DefaultConnectionLimit to a larger value (expected number of tasks in the current execution batch).

If you are mostly doing requests against different hosts, the limit in your question works fine.

Regarding the usage of resources, it's probably fine as long as your server is not very busy by other applications.

Joakim M. H.
  • 424
  • 4
  • 14
jgauffin
  • 99,844
  • 45
  • 235
  • 372
  • let me give you the picture....I have around 46K tasks,these task are ran in batch of 100 (each task will call webservice) so I have 100 threads calling webserivce simultaneously. is it still trivial? or will it have some impact in my case? – Viru Jan 21 '16 at 12:17
  • do you mean to say same host in either of this case?? if your tasks are mostly against different hosts, increase DefaultConnectionLimit to a larger value (expected number of tasks in the current execution batch). If you are mostly doing requests against different hosts, the limit in your question works fine. – Viru Jan 21 '16 at 12:51
0

You should also realize, that it may hurt you at the other end. You may get throttled or black listed, if you put a big load on the webservice you are calling. See for example this blog.

Yahoo Serious
  • 3,728
  • 1
  • 33
  • 37