2

I'am new to Blazor wasm. Actually I have HttpClient added as singleton to the services in program.cs. Then I use a generic utility class (HttpService) where HttpClient is injected to do http calls (GetAsync, PostAsync,...). Everywhere I use async await pattern. First case: in some page I should do simultaneous get webapi calls and then when all are done do some calculations on results. Second case: in other page I have the opposed need, do a call, wait for result and then use it for a second call.

I try and I notice that my code with calls like:

HttpService:

    public async Task<dataDTO> GetData(int id)
    {
        var httpResponse = await httpService.Get<DataDTO>($"{baseURL}/{id}");

        if (!httpResponse.Success)
        {
            var msg = await httpResponse.GetBody();
            throw new ApplicationException(msg);
        }
        return httpResponse.Response;
    }

And calls in page component:

var result1 = await GetData(5000);
var result2 = await GetData(2000);
var result3 = await GetData(500);

Suppose that the parameter are milliseconds that server use to delay the response. These calls are always syncronous. The next wait the end of previous to be executed. (second case).

How to implement the first case (simultaneous calls)?

Thank you very much.

3
  • 1
    That can work for small number of paralleled requests but Blazor WASM is not really multithread, with a big number of request you gonna loose some. So don't try to send requests in parallels with Blazor WASM. Wait for .Net 6. Commented Nov 23, 2020 at 12:54
  • Ok thank you. I tried and in this case with 3 request it works fine. Commented Nov 23, 2020 at 14:07
  • Sequentialy it works, but if you use Task.WhenAll or Task.WaitAll some request can be lost. Commented Nov 23, 2020 at 14:20

3 Answers 3

3

You could use Task.WhenAll to allow them to run simultaneously:

dataDTO[] results = await Task.WhenAll(GetData(5000), GetData(2000), GetData(500));

This creates a new Task that completes when all the provided tasks have completed, and amalgamates the results into an array.

Sign up to request clarification or add additional context in comments.

1 Comment

how can i observe task results in blazor like we do in angular ? i mean instead of task.whenall we subscribe http calls and when the response is ready it run subscribed code or callbsck.
3
var task1 =  GetData(5000);
var task2 =  GetData(2000);
var task3 =  GetData(500);

await Task.WhenAll(task1, task2, task3);

var result1 = task1.Result;
var result2 = task2.Result;
var result3 = task3.Result;

2 Comments

While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value.
how can i observe task results in blazor like we do in angular ? i mean instead of task.whenall we subscribe http calls and when the response is ready it run subscribed code or callbsck.
3

You can fire them all at once then wait for them to complete using Task.WhenAll.

So your code would be something like this:

//Notice that we don't use await here.
var task1 = GetData(5000);
var task2 = GetData(2000);
var task3 = GetData(500);

Task ResultTask = await Task.WhenAll(new List<Task<dataDTO>> { task1 ,task2, task3 });

 if (ResultTask.Status == TaskStatus.RanToCompletion)
         Console.WriteLine("All tasks completed successfully!");
      else if (ResultTask.Status == TaskStatus.Faulted)
         Console.WriteLine("Operation failed!");  

Also, you can access their result via ResultTask.Result:

foreach (var result in ResultTask.Result) 
{
  //Do stuff
}

4 Comments

how can i observe task results in blazor like we do in angular ? i mean instead of task.whenall we subscribe http calls and when the response is ready it run subscribed code or callbsck.
@MaasoudAsadi You don't need callbacks when you have async await، just await your http call or in this case await the WhenAll task. and your next code will be executed when the task finishes.
but in subscribe the code executes and not awaited. i think ContinueWith() is the nearest answer. maybe i misunderstood subscribe. do they run concurrent ?
@MaasoudAsadi I don't work with angular, but await can completely replace ContinueWith as they're similar, and await is much simpler as the latter adds more complexity, especially with error handling.

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.