C
C#2y ago
Messiah

❔ CallWebApiForUserAsync

I'm doing
List<T>.ForEach(i => {
_downstreamWebApi.CallWebApiForUserAsync(
SERVICE,
options => {
options.RelativePath = i
List<T>.ForEach(i => {
_downstreamWebApi.CallWebApiForUserAsync(
SERVICE,
options => {
options.RelativePath = i
but for some reason it isn't calling the api not even once. How do you go about calling the api with a list?
11 Replies
Angius
Angius2y ago
You're not awaiting your async calls
Messiah
MessiahOP2y ago
Urls.ForEach(async i =>
{
var r = await _downstreamWebApi.CallWebApiForUserAsync(
SERVICE,
options =>
{
options.HttpMethod = HttpMethod.Post;
options.RelativePath = i;
});
var obj3 = await JsonSerializer.DeserializeAsync<RazorApp.Models.Entities.EntityList>(await r.Content.ReadAsStreamAsync(), options);
t3 += await r.Content.ReadAsStringAsync();
});

ViewData["t3"] = t3;
Urls.ForEach(async i =>
{
var r = await _downstreamWebApi.CallWebApiForUserAsync(
SERVICE,
options =>
{
options.HttpMethod = HttpMethod.Post;
options.RelativePath = i;
});
var obj3 = await JsonSerializer.DeserializeAsync<RazorApp.Models.Entities.EntityList>(await r.Content.ReadAsStreamAsync(), options);
t3 += await r.Content.ReadAsStringAsync();
});

ViewData["t3"] = t3;
Angius
Angius2y ago
I'd just use a normal foreach loop instead of trying to Javascript it with .ForEach()
Messiah
MessiahOP2y ago
the way I'm dumping the results into a string, then to the view model is ok?
Angius
Angius2y ago
You don't seem to have a viewmodel
Messiah
MessiahOP2y ago
it worked with the normal foreach! jesus christ I spent 26 hours (literally) on this problem this is the viewmodel, it's razor pages why was it not working with .foreach?
Angius
Angius2y ago
Ah, I saw ViewData and thought you're using that as some demented replacement for a viewmodel ¯\_(ツ)_/¯ Dunno, I never really use it, so don't know its quirks Probably because there's async code in the lambda you pass, but .ForEach() itself is a void method
Messiah
MessiahOP2y ago
I see, so the compiler just moves on because it knows it won't be returning anything anyways
Angius
Angius2y ago
yep
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.

Did you find this page helpful?