WebClient
connection limit
C# programming
.NET
network configuration

How can I programmatically remove the 2 connection limit in WebClient

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

In classic .NET Framework, WebClient often appears limited to two concurrent connections per host because of the underlying ServicePointManager defaults. If that is truly the bottleneck, you can raise the limit programmatically, but the exact fix depends on whether you want a global change or a host-specific one.

Where The Limit Comes From

WebClient is a higher-level API. The connection limit is enforced lower down by service point management, not by WebClient itself.

That is why code like this may not fan out as much as you expect:

csharp
1using System;
2using System.Net;
3using System.Threading.Tasks;
4
5class Program
6{
7    static async Task Main()
8    {
9        using var client = new WebClient();
10        var tasks = new Task<string>[5];
11
12        for (int i = 0; i < tasks.Length; i++)
13        {
14            tasks[i] = client.DownloadStringTaskAsync("https://example.com");
15        }
16
17        await Task.WhenAll(tasks);
18    }
19}

In older .NET Framework environments, the host connection limit may serialize more of those calls than you intended.

Raise The Default Limit Early

The usual programmatic fix is:

csharp
using System.Net;

ServicePointManager.DefaultConnectionLimit = 20;

Set that early during process startup, before the application has already created service points for the remote hosts you care about.

That "set it early" part matters because existing service points may keep their earlier settings.

Change The Limit For One Host Only

If you only want to raise the limit for one endpoint, use the service point for that URI:

csharp
1using System;
2using System.Net;
3
4var uri = new Uri("https://example.com");
5ServicePoint sp = ServicePointManager.FindServicePoint(uri);
6sp.ConnectionLimit = 20;

This is more targeted and avoids changing behavior for every outbound host the process might contact.

That can be useful in shared processes that talk to several external services with different traffic profiles.

Know The Runtime Context

This question is most relevant to classic .NET Framework behavior. In newer .NET implementations, connection management differs, and HttpClient is the preferred API for modern code.

So if you are maintaining old code, changing ServicePointManager may be the right short-term answer. If you are writing new code, it is often better to move to HttpClient and configure connection behavior there instead of investing more in WebClient.

A Better Long-Term Direction: HttpClient

For new development:

csharp
1using System;
2using System.Net.Http;
3using System.Threading.Tasks;
4
5class Program
6{
7    static async Task Main()
8    {
9        using var client = new HttpClient();
10        string html = await client.GetStringAsync("https://example.com");
11        Console.WriteLine(html.Length);
12    }
13}

This does not answer the old limit question directly, but it matters because WebClient is legacy API surface and is rarely the best strategic choice in modern .NET code.

If you do raise the limit, watch the result under load instead of assuming a bigger number is automatically safer. More connections can improve throughput, but they can also amplify server pressure, proxy contention, and client-side resource usage.

Common Pitfalls

One common mistake is setting DefaultConnectionLimit after network calls have already started, then assuming the change should retroactively affect existing service points.

Another issue is raising the limit without checking whether the server, DNS resolution, or remote rate limits are the actual bottlenecks.

A third problem is assuming the same behavior across classic .NET Framework and newer .NET runtimes without checking which runtime the application actually uses.

Finally, developers sometimes focus on the connection cap when the real improvement would come from moving off WebClient entirely.

Summary

  • The apparent two-connection limit comes from underlying service point behavior, not from WebClient alone.
  • In classic .NET Framework, ServicePointManager.DefaultConnectionLimit raises the global default.
  • 'ServicePoint.ConnectionLimit can target one specific host.'
  • Set the limit early in application startup.
  • For new code, prefer HttpClient instead of building more infrastructure around WebClient.

Related reading
Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.