Web-Development-with-Blazor-Second-Edition icon indicating copy to clipboard operation
Web-Development-with-Blazor-Second-Edition copied to clipboard

Issue in chapter 07 Adding data access to Web Assembly

Open chopper2001 opened this issue 2 years ago • 4 comments

I hit an issue following the code in chapter 7 when adding this code:

builder.Services.AddOptions<BlogApiJsonDirectAccessSetting>() .Configure(options => {. options.DataPath = @"........\Data"; options.BlogPostsFolder = "Blogposts"; options.TagsFolder = "Tags"; options.CategoriesFolder = "Categories"; }); builder.Services.AddScoped<IBlogApi, BlogApiJsonDirectAccess>();

When setting the startup project to be BlazorWebAssembly.Server and running I get the following error (partially shown):

blazor.webassembly.js:1 crit: Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100] Unhandled exception rendering component: Cannot provide a value for property '_api' on type 'Components.Pages.Index'. There is no registered service of type 'Data.Models.Interfaces.IBlogApi'. System.InvalidOperationException: Cannot provide a value for property '_api' on type 'Components.Pages.Index'. There is no registered service of type 'Data.Models.Interfaces.IBlogApi'. at Microsoft.AspNetCore.Components.ComponentFactory.<>c__DisplayClass7_0.<CreateInitializer>g__Initialize|1(IServiceProvider serviceProvider, IComponent component) at Microsoft.AspNetCore.Components.ComponentFactory.PerformPropertyInjection(IServiceProvider serviceProvider, IComponent instance) .....

I suspected I had made some error in my version of the code but could not spot anything. So, I forked the code from this repository and tried running the solution for chapter 7. I hit a couple of other issues first (see below) but once I corrected those, I got the same error as above with the repository code when running the BlazorWebAssembly.Server project, so I don't believe the issue is with my version.

As an aside, if I add the code to the BlazorWebAssemblyClient project just after the add services for HttpClient the BlazorWebAssembly.Server project does then start without the error above.

I do hit another issue if it starts this way, in that no data is saved to the filesystem, but that may be a consequence of the above issue.


The two issues I had to resolve prior to being able to build the solutions in Chapter 7 were in Data.Models\Models. In both Tags.cs and Categories.cs there are two issues:

  1. Spurious line with CustomCssClassProvider instead of closing }
  2. Missing using System.ComponentModel.DataAnnotations;

chopper2001 avatar Jul 01 '23 17:07 chopper2001

I believe that is as it should. Are you at the point where you are testing the api? Just continue testing the api. The error you are seeing will be fixed later. We don't have an api client yet (since we don't have an api yet) that's why you are seeing the error.

EngstromJimmy avatar Jul 01 '23 17:07 EngstromJimmy

Thanks for the very quick feedback. In my code I had added the first minimal API code for api/Blogpost so was at point 6; time to test the API; so was expecting things to work at this point. The error does not occur when trying to test the API; it is the initial load; so at this point none of the Web Assembly functionality is running.

This is why I forked the code from your repo, as the chapter 7 code will include the client etc. Unfortunately still cannot start the Web Assembly version with the repo code.

As I mentioned there were the typos in the Tags.cs and Categories.cs files in the repo code; so a couple of things that would need to be corrected even if the other error turns out to be at my end. Here is the Tags.cs; line 10 is the problem.

Screenshot 2023-07-01 193425

chopper2001 avatar Jul 01 '23 18:07 chopper2001

Hello, I´m having the same error:

blazor.webassembly.js:1 crit: Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100] Unhandled exception rendering component: Cannot provide a value for property '_api' on type 'Components.Pages.Index'. There is no registered service of type 'Data.Models.Interfaces.IBlogApi'. System.InvalidOperationException: Cannot provide a value for property '_api' on type 'Components.Pages.Index'. There is no registered service of type 'Data.Models.Interfaces.IBlogApi'.

If I move builder.Services.AddScoped<IBlogApi, BlogApiJsonDirectAccess>(); to Program.cs in Client, the code starts without error but I can´t get access the filesystem.

icefoxj avatar Jul 31 '23 19:07 icefoxj

I believe that the step you are on is to test the API, is that correct? The WebAssembly client is not runnable at this point, first we are testing the API then we will build a client for it, and then hook it up to the WebAssembly client. Is that the step you are on?

EngstromJimmy avatar Jul 31 '23 19:07 EngstromJimmy