markjprice / apps-services-net7

Repository for the Packt Publishing book titled "Apps and Services with .NET 7" by Mark J. Price
154 stars 58 forks source link

Blazor Client Not Showing Products Data #30

Closed jimcbell closed 8 months ago

jimcbell commented 9 months ago

Chapter: 17 Page Number: 626 Section Title: Using the Radzen tabs, image, and icon components Problem to fix: For some reason none of the products are showing up in my blazor client. I have made updates according to the errata. I can also see that the data is being returned by the minimal api in the network tab. image When I try to run this in debug mode to see for myself I get this exception: System.IO.IOException: ' Received an unexpected EOF or 0 bytes from the transport stream.' When I try to write the products to the console with a console.writeline statement, the categories no longer show up at all in the UI. I have checked my packaging compared to the ones in the repository for the Server, Client, and Shared application and all are the same.

Here is my Client code: protected override async Task OnParametersSetAsync() { Category[]? categoriesArray = null; // Web API service uses "Preserve" so // we must control how references are handled. JsonSerializerOptions jsonOptions = new() { ReferenceHandler = ReferenceHandler.Preserve, PropertyNameCaseInsensitive = true }; HttpClient client = httpClientFactory.CreateClient("Northwind.BlazorLibraries.ServerAPI"); string path = "api/categories"; try { categoriesArray = (await client.GetFromJsonAsync<Category[]?>( path, jsonOptions)); } catch (Exception ex) { Console.WriteLine($"{ex.GetType()}: {ex.Message}"); } if (categoriesArray is not null) { categories = categoriesArray.AsQueryable(); } } This is my minimal api: app.MapGet("api/categories", ( [FromServices] NorthwindContext db) => Results.Json( db.Categories.Include(c => c.Products))) .WithName("GetCategories") .Produces<Category[]>(StatusCodes.Status200OK);

I am not too positive where what to do for troubleshooting, I have also tested the api with rest client and it is returning the data fine, so I believe it is a client issue...

markjprice commented 9 months ago

You have shown that the products are being returned as JSON to the client so you are correct that the problem must be in the client. Your screenshot shows the category name and image are being returned, although it looks like the product count is 0. I don't know why the products within each category are not being deserialized properly or recognized. When I set a breakpoint and step through it I can see all the products as shown below:

image

And they output correctly:

image

jimcbell commented 9 months ago

Before I had mentioned I was getting a System.IO.IOException when attempting to debug. I had just moved on further in the reading rather than staying stuck on this. Now I am no longer getting the exception without having changed anything in the Chapter 17 code related to this. So I can debug it, but I can still see that there are no products in my results: image

Still seeing them in the browser response: image

I did do a windows update between no and when I posted it, but I performed several reboots before that.

The only thing that I can think of is that:

  1. I set up my Azure Sql Database with extremely low specs. I can try this using a local Sql database and see what happens...
  2. There is something wrong with the Json Serialization options in my client/ server.

I will switch this to a local Sql Database and retry it.

The other strange thing is that my Azure Sql Database will often throw errors in my .Net code as if the connection string is incorrect. Until I use Azure Data Studio or SSMS to connect to the database, then it works to run queries in .Net.

jimcbell commented 8 months ago

I upgraded my Visual Studio Community 2022 from 17.8.0 to 17.8.5 and I am no longer getting the error: Received an unexpected EOF or 0 bytes from the transport stream. And I am able to debug the actual CS code now - figured out why I was not getting the Products: My navigation property from Category -> Products did not have a setter, only a getter:

[InverseProperty("Category")] public virtual ICollection<Product> Products { get;} = new List<Product>();

I added the setter and it is working properly now, double checked your Category.cs file and you also have the setter there. Closing this issue, thanks! image

markjprice commented 8 months ago

I'm glad you found the problem. I'll think about how I could add a warning for future readers who might mistakenly do something similar.

jimcbell commented 8 months ago

Thanks, I have not modified that file since it was created by the dotnet ef tool, so it may not have added that setter when it automatically generated the class file.