Quantcast
Channel: Active questions tagged blazor - Stack Overflow
Viewing all articles
Browse latest Browse all 4839

Issue with API Response in Product Method, Project Freezes on Waiting for Response

$
0
0

I'm facing an issue with my API response in the GetProductsAsync method. The GetUsersAsync method works fine, but when I try to get products, the project freezes and the response is never received. I can see that the method is being called, but when the response is awaited, it seems like the program just hangs without progressing.If I get the JSON data, I will do the mapping process, it is not important, but I cannot get a response.

Here is the relevant code:

public interface IUserService{    Task<List<User>> GetUsersAsync();    Task<List<Product>> GetProductsAsync();}public class UserService : IUserService{    private readonly HttpClient _httpClient;    public UserService(HttpClient httpClient)    {        _httpClient = httpClient;    }    public async Task<List<User>> GetUsersAsync()    {        var response = await _httpClient.GetAsync("https://jsonplaceholder.typicode.com/users");        response.EnsureSuccessStatusCode();         var content = await response.Content.ReadAsStringAsync();        return JsonSerializer.Deserialize<List<User>>(content);    }    public async Task<ApiResponse> GetProductsAsync()    {        // I'm getting an error in a subdata, it's not progressing.        var response = await _httpClient.GetAsync("https://localhost:7024/api/Product");        //The project does not reach this stage and is stalled.        response.EnsureSuccessStatusCode();         var content = await response.Content.ReadAsStringAsync();        return JsonSerializer.Deserialize<ApiResponse>(content);    }}// User.cspublic class User{    public int Id { get; set; }    public string Name { get; set; }    public string Username { get; set; }    public string Email { get; set; }}public class Product{    public string Name { get; set; }    public decimal Price { get; set; }    public int CategoryId { get; set; }    public int Id { get; set; }    public DateTime CreatedDate { get; set; }}public class ApiResponse{    public List<Product> Data { get; set; }    public object Errors { get; set; }}

https://localhost:7024/api/ProductResponse body:

{"data": [    {"name": "Polo Yaka","price": 30,"categoryId": 1,"id": 1,"createdDate": "2024-11-11T17:39:52.1000973"    },    {"name": "V yaka","price": 30,"categoryId": 1,"id": 2,"createdDate": "2024-11-11T17:47:14.3504982"    }  ],"errors": null}

Viewing all articles
Browse latest Browse all 4839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>