C
C#3w ago
OTDL

C# api response infinitly loading and stops debuging with working api and blazor

I am building an admin dashboard for my webshop with blazor and an api. I know my api is working correctly because i already have working crud pages for products, categories and subcategories. I have a method in categories controller that returns how many products there are in a category GetProductsByCategory(Guid id). When hitting this line var response = await _httpClient.GetAsync($"{ApplicationConstants.ApiUrl}Categories/{id}/products"); in the debugger it doesnt go to the next line instead it stops debugging and on my categories page it loads infinitly so i cant debug what the var response is because it stops. So i made a second method that doesnt return a json array but just an int but its the same thing. Its only on these 2 endpoints this happens Btw my page fully works if i remove the count functionality from this page. Ive tested the endpoints in my api and with postman and both get a 200 with either an empty array or 0 depending on what method public async Task<IQueryable<JsonProduct>> GetProductsByCategory(Guid id) { await AddAuthorizationHeaderAsync(); var response = await _httpClient.GetAsync($"{ApplicationConstants.ApiUrl}Categories/{id}/products"); response.EnsureSuccessStatusCode(); var json = await response.Content.ReadAsStringAsync(); var productCollection = JsonSerializer.Deserialize<List<JsonProduct>>(json); return productCollection.AsQueryable(); } public async Task<int> GetProductAmountByCategory(Guid id) { await AddAuthorizationHeaderAsync(); var response = await _httpClient.GetAsync($"{ApplicationConstants.ApiUrl}Categories/{id}/productamount"); response.EnsureSuccessStatusCode(); var json = await response.Content.ReadAsStringAsync(); var productCount = JsonSerializer.Deserialize<int>(json); return productCount; }
4 Replies
OTDL
OTDLOP3w ago
// Blazor code method that gets the categories and applies pagination and such private async Task GetCategories() { ViewModel.IsLoading = true; var categories = await CategoryService.GetCategories(null); ViewModel.TotalItems = categories.Count(); ViewModel.TotalPages = ViewModel.TotalItems / ViewModel.PageSize; if (ViewModel.TotalItems % ViewModel.PageSize != 0) { ViewModel.TotalPages++; } int startIndex = (ViewModel.CurrentPage - 1) * ViewModel.PageSize; int endIndex = Math.Min(startIndex + ViewModel.PageSize, ViewModel.TotalItems); var categoriesResponse = categories.Skip(startIndex).Take(ViewModel.PageSize).ToList(); ViewModel.CategoriesList = categoriesResponse.Select(async x => { var count = await GetItemsInCategory(x.Id); return new CategoryViewModel { Id = x.Id, Name = x.Name, Count = count }; }).Select(t => t.Result).ToList(); ViewModel.IsLoading = false; } // Blazor code method that gets the amount in category, its currently using the second api method code would be different with the first one private async Task<int> GetItemsInCategory(Guid id) { var products = await CategoryService.GetProductAmountByCategory(id); return products; } Please help me if i need to add any other code or explanation say so.
Sehra
Sehra3w ago
my suspect would be t => t.Result. put a breakpoint on that and see if the task is completed successfully
lycian
lycian3w ago
It's already in an async method so no need to try to do things synchronously. If you want to run them all just do
var categoryTasks = categoriesResponse.Select(async x =>
{
var count = await GetItemsInCategory(x.Id);
return new CategoryViewModel
{
Id = x.Id,
Name = x.Name,
Count = count
};
});

var results = await Task.WhenAll(categoryTasks);
ViewModel.CategoriesList = results.ToList();
var categoryTasks = categoriesResponse.Select(async x =>
{
var count = await GetItemsInCategory(x.Id);
return new CategoryViewModel
{
Id = x.Id,
Name = x.Name,
Count = count
};
});

var results = await Task.WhenAll(categoryTasks);
ViewModel.CategoriesList = results.ToList();
Also in the future follow $code
MODiX
MODiX3w ago
To post C# code type the following: ```cs // code here ``` Get an example by typing $codegif in chat For longer snippets, use: https://paste.mod.gg/

Did you find this page helpful?