Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/256.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何限制与异步httpc客户端的连接_C#_Http_Connection_Dotnet Httpclient_Servicepointmanager - Fatal编程技术网

C# 如何限制与异步httpc客户端的连接

C# 如何限制与异步httpc客户端的连接,c#,http,connection,dotnet-httpclient,servicepointmanager,C#,Http,Connection,Dotnet Httpclient,Servicepointmanager,我试图限制到端点的最大连接数,我通过设置ServicePointManager.DefaultConnectionLimit来实现这一点,但它不起作用。为了测试这一点,我设置了一个端点,该端点休眠5秒,然后返回 这是我的密码: class Program { private static Uri uri = new Uri("http://myservice.com?sleepForMillisecond=5000")); static void Main(string[] a

我试图限制到端点的最大连接数,我通过设置ServicePointManager.DefaultConnectionLimit来实现这一点,但它不起作用。为了测试这一点,我设置了一个端点,该端点休眠5秒,然后返回

这是我的密码:

class Program
{
    private static Uri uri = new Uri("http://myservice.com?sleepForMillisecond=5000"));

    static void Main(string[] args)
    {
        ServicePointManager.DefaultConnectionLimit = 1;
        MainAsync().Wait();
    }

    static async Task MainAsync()
    {
        var watch = Stopwatch.StartNew();
        var tasks = new List<Task>()
            {
                Task.Run(async () => await MakeCallWithHttpClient()),
                Task.Run(async () => await MakeCallWithHttpClient()),
                Task.Run(async () => await MakeCallWithHttpClient())
            };

        await Task.WhenAll(tasks);
        watch.Stop();
        Console.WriteLine("Elapsed Time For HttpClient: " + watch.Elapsed);

        watch.Restart();
        tasks = new List<Task>()
            {
                Task.Run(async () => await MakeCallWithWebClient()),
                Task.Run(async () => await MakeCallWithWebClient()),
                Task.Run(async () => await MakeCallWithWebClient())
            };

        await Task.WhenAll(tasks);
        watch.Stop();
        Console.WriteLine("Elapsed Time For WebClient: " + watch.Elapsed);
        Console.WriteLine("Press Enter To Exit.");
        Console.ReadLine();
    }

    private static async Task MakeCallWithHttpClient()
    {
        var client = new HttpClient();
        await client.GetStringAsync(uri);
        Console.WriteLine("Done");
    }

    private static async Task MakeCallWithWebClient()
    {
        var client = new WebClient();
        await client.DownloadStringTaskAsync(uri);
        Console.WriteLine("Done");
    }
} 
如您所见,异步HttpClient不受最大连接数1的限制,因为并行调用3次需要5秒,而异步WebClient受1连接的限制,因为并行调用3次需要15秒

我的问题是…如何使用异步HttpClient限制最大连接数


谢谢

对所有请求使用相同的HttpClient实例,连接限制将按预期工作

为了开悟,请阅读这篇有趣的文章。。。异步还是多线程?我使用的是同一个httpclient实例,但是我看不出它有任何效果。有什么提示吗?@TomaszJaskuλa当您发出多个请求时,打开了多少个连接?您的DefaultConnectionLimit设置为什么?我正在测试同一HttpClient实例上的1000个异步调用。将DefaultConnectionLimit设置为5或100不会改变任何内容。我打开了大约300个TCP连接。然而,这对我来说是有效的new-HttpClientnew-HttpClientHandler{MaxConnectionsPerServer=5}。这起作用了,我只有5个连接打开了1000个电话。
Done
Done
Done
Elapsed Time For HttpClient: 00:00:05.0953943
Done
Done
Done
Elapsed Time For WebClient: 00:00:15.0450096