4

I have a list of selected contentIds and for each content id I need to call an api, get the response and then save the received response for each content in DB.

At a time a user can select any number of content ranging from 1-1000 and can pass on this to update the content db details after getting the response from api.

In this situation I end up creating multiple requests for each content.

I thought to go ahead with asp.net async Task operation and then wrote this following method.

The code I wrote currently creates one one task for each contentId and atlast I am waiting from all task to get the response.

Task.WaitAll(allTasks);

public static Task<KeyValuePair<int, string>> GetXXXApiResponse(string url, int contentId)
{
    var client = new HttpClient();

    return client.GetAsync(url).ContinueWith(task =>
    {
        var response = task.Result;
        var strTask = response.Content.ReadAsStringAsync();
        strTask.Wait();
        var strResponse = strTask.Result;

        return new KeyValuePair<int, string>(contentId, strResponse);
    });
}

I am now thinking for each task I create it will create one thread and in turn with the limited no of worker thread this approach will end up taking all the threads, which I don't want to happen.

Can any one help/guide me how to handle this situation effectively i.e handling multiple api requests or kind of batch processing using async tasks etc?

FYI: I'm using .NET framework 4.5

2
  • Which version of C# and .Net do you use? Commented Aug 13, 2014 at 14:24
  • 2
    Is there a reason you're not using await? Commented Aug 13, 2014 at 14:31

1 Answer 1

7

A task is just a representation of an asynchronous operation that can be waited on and cancelled. Creating new tasks doesn't necessarily create new threads (it mostly doesn't). If you use async-await correctly you don't even have a thread during most of the asynchronous operation.

But, making many requests concurrently can still be problematic (e.g. burdening the content server too much). So you may still want to limit the amount of concurrent calls using SemaphoreSlim or TPL Dataflow:

private static readonly SemaphoreSlim _semaphore = new SemaphoreSlim(100);

public static async Task<KeyValuePair<int, string>> GetXXXApiResponse(string url, int contentId)
{
    await _semaphore.WaitAsync();
    try
    {
        var client = new HttpClient();
        var response = await client.GetAsync(url);
        var strResponse = await response.Content.ReadAsStringAsync();
        return new KeyValuePair<int, string>(contentId, strResponse);
    }
    finally
    {
        _semaphore.Release();
    }
}

To wait for these tasks to complete you should use Task.WhenAll to asynchronously wait instead of Task.WaitAll which blocks the calling thread:

await Task.WhenAll(apiResponses);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.