I am developing a .NET4-based application that must request third-party servers to get information from them. I am using HttpClient to execute these HTTP requests.
I need to create one hundred or one thousand requests in a short period of time. I would like to limit the creation of this request to the limit (defined by a constant or something else) so that other servers do not receive many requests.
I checked this link , which shows how to reduce the number of tasks created at any time.
Here is my non-working approach:
// create the factory var factory = new TaskFactory(new LimitedConcurrencyLevelTaskScheduler(level)); // use the factory to create a new task that will create the request to the third-party server var task = factory.StartNew(() => { return new HttpClient().GetAsync(url); }).Unwrap();
Of course, the problem is that even this one task is being created at that time, many requests will be created and processed at the same time, since they are launched in another scheduler. I could not find a way to change the scheduler to HttpClient.
How should I deal with this situation? I would like to limit the number of requests created to a certain limit, but not to block waiting for this request to complete.
Is it possible? Any ideas?
source share