HttpClient async requests not completing for large batch sent out in a loop

前端 未结 3 1101
再見小時候
再見小時候 2021-01-12 15:59

I think I\'ve managed to make a test that shows this problem repeatably, at least on my system. This question relates to HttpClient being used for a bad endpoint (no

3条回答
  •  鱼传尺愫
    2021-01-12 16:47

    Your exception information is being lost in the WhenAll task. Instead of using that, try this:

    Task aggregateTask = Task.Factory.ContinueWhenAll(
        Batch,
        TaskExtrasExtensions.PropagateExceptions,
        TaskContinuationOptions.ExecuteSynchronously);
    
    aggregateTask.Wait();
    

    This uses the PropagateExceptions extension method from the Parallel Extensions Extras sample code to ensure that exception information from the tasks in the batch operation are not lost:

    /// Propagates any exceptions that occurred on the specified tasks.
    /// The Task instances whose exceptions are to be propagated.
    public static void PropagateExceptions(this Task [] tasks)
    {
        if (tasks == null) throw new ArgumentNullException("tasks");
        if (tasks.Any(t => t == null)) throw new ArgumentException("tasks");
        if (tasks.Any(t => !t.IsCompleted)) throw new InvalidOperationException("A task has not completed.");
        Task.WaitAll(tasks);
    }
    

提交回复
热议问题