0

I am working on a Blazor WASM web app that gets ETAs for customer orders, but my GetFromJsonAsync() has the correct number of objects (customers) but they are all empty objects.

The data comes from an excel spreadsheet and then is mapped into objects (Customer, Order, Item) with the relations being customers have a list of orders that have a list of items. Each customer is added to a static list of Customers in the Customer class. Everything is working up to this point (I created a program just to test this and all the data is going where it belongs).

From there I create a repository of customers, and run through the static list of customers, pushing each customer to the repository.

    public static void AddCustomerRepository(this IServiceCollection services)
    {
        Workbook workbook = new Workbook();
        workbook.InitWorkbook();
        workbook.MapOrdersFromWorkbook();

        var customerRepository = new MemoryRepository<Customer>();

        foreach (Customer customer in Customer.Customers) {
            customerRepository.Add(customer);
        }

        services.AddSingleton<IRepository<Customer>>(customerRepository);
    }

AddCustomerRepository is ran on startup of the web app. This is the JsonAsync in the .razor file, it is getting the correct number of objects in the json (814) but there is no data so it's 814 empty objects.

private Customer[] customer;

protected async override Task OnInitializedAsync()
{
    customer = await Http.GetFromJsonAsync<Customer[]>("api/OrderStatus");
    StateHasChanged();
}

Here is the API controller:

[Route("api/[controller]")]
[ApiController]
public class OrderStatusController : ControllerBase
{
    private readonly IRepository<Customer> _customerRepository;
    public OrderStatusController(IRepository<Customer> customerRepository)
    {
        _customerRepository = customerRepository;
    }

    [HttpGet]
    public IEnumerable<Customer> Get()
    {
        return _customerRepository.GetAll()
            .OrderBy(customer => customer.number);
    }
}

What would cause it to get the correct number of objects but no data in the objects? This is my first time working with Blazor WASM, as well as my first time working with .NET and I definitely bit off a bigger project than I should have to start, but I'm now so far in the project that I don't want to scrap it.

3
  • Where is the Customer class defined? Do you have separate classes in Client and Server? How is the HttpClient registered in the Client? etc. Commented Sep 21, 2021 at 18:55
  • Point a Browser at the endpoint address and look at the raw JSon. Does it have data? Commented Sep 21, 2021 at 18:56
  • @HenkHolterman There are 2 classes for Customer, one is in Shared and is just the get/set for the API, the Server is the implementation used to map the excel data. As for the HttpClient I am unsure, I was following a tutorial for that part, it is injected at the top of the .razor but that is all I am aware of. I checked the raw JSON, it is empty objects as well. Commented Sep 21, 2021 at 19:04

1 Answer 1

2

What would cause it to get the correct number of objects but no data in the objects?

Most likely a mismatch in the property names.

There are 2 classes for Customer,

Compare them. Apparently they are not "Json compatible".

The more practical approach is to have 1 class Customer in the Shared Project. See if you can make that work.

... to a static list of Customers in the Customer class.

Move that to some other place. It should not be in Shared.


If you do need two different Customer classes (that is possible) then change your API. Convert Server.Customer to Shared.Customer and return that. You can use AutoMapper.

Main point is that the communication should use Shared.Customer in both Client and Server. It is your Data Transfer Object (DTO).

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

1 Comment

I am working on moving the models to the Shared project, instead of the static list should I just have it return a list in MapOrdersFromWorkbook and I can add them to the repository by looping through that?

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.