I have a .NET 8 Blazor WebApp project with a separate WebAssembly project for the front end. When I call the api and return a list of objects, it shows a count of 0, yet actually contains elements. It shows both in Rider’s debugging value watches and when I am looking at List.Count value in code. In postman the json that the api is returning is formatted correctly, so I think it’s something to do with the deserialization on the WebAssembly side. But have no idea what I’m doing wrong.
public async Task<ApiResponse<List<TreatmentDto>>> GetTreatmentsAsync()
{
var response = await httpClient.GetAsync("api/treatments");
return await response.HandleResponse<List<TreatmentDto>>();
}
public static async Task<ApiResponse<T>> HandleResponse<T>(this HttpResponseMessage response)
{
if (response.IsSuccessStatusCode)
{
return await response.Content.ReadFromJsonAsync<ApiResponse<T>>() ?? new ApiResponse<T> { Success = false };
}
return new ApiResponse<T> { Success = false, Message = "API call failed", Errors = new List<string> { response.ReasonPhrase } };
}
public class ApiResponse<T>
{
public bool Success { get; set; }
public T Data { get; set; }
public string Message { get; set; }
public List<string> Errors { get; set; } = new List<string>();
}
Postman response
{
"success": true,
"data": [
{
"id": "2f312b5f-6d6f-4615-8bf1-b7b88bc40365",
"type": 0,
"name": "HYDROCORTISONE 10 MG ORAL TABLET [CORTEF]",
"description": "asdfasf",
"isMuted": false,
"customerNotes": "",
"medicationRxcui": 208712,
"medicationRxstring": "HYDROCORTISONE 10 MG ORAL TABLET [CORTEF]",
"tzid": "",
"expectedUnits": [
{
"id": "dab0842c-226d-4c49-8b8c-8f1ba0766086",
"treatmentId": "2f312b5f-6d6f-4615-8bf1-b7b88bc40365",
"numberOfUnits": 2,
"unitType": "Pills",
"order": 0
}
],
"reminders": [
{
"id": "84699631-db01-4e09-b292-6c26f5c4236d",
"rRule": "FREQ=WEEKLY",
"startDate": "2024-09-08T10:00:00-04:00",
"endDate": null,
"untilDate": null,
"tzid": "",
"treatmentId": "2f312b5f-6d6f-4615-8bf1-b7b88bc40365"
},
{
"id": "a77dfbe2-5290-4453-99af-9c7ac3dbe5b7",
"rRule": "FREQ=WEEKLY",
"startDate": "2024-09-08T09:00:00-04:00",
"endDate": null,
"untilDate": null,
"tzid": "",
"treatmentId": "2f312b5f-6d6f-4615-8bf1-b7b88bc40365"
}
]
}
],
"message": null,
"errors": []
}
Relevent Client Program.cs services
builder.Services.AddScoped(sp => new HttpClient
{ BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });
builder.Services.AddScoped<ITreatmentService, TreatmentService>();
2