Blazor Error: "There is no registered service of type" -- other StackOverflow questions do not solve

Viewed 3360

In my .Net CORE 5 (imported support files v5.0.12) Blazor PWA VS-2019 solution, I "had" a working 'Customer'-(data/app)service and Blazor pages to List and CRUD the Customer records (works as expected). I created similar 'Vehicle' files for both the SERVER-project and the CLIENT-project (of course changing the model field-references).

There are NO compilation errors yet, when I try to show the "VehicleList" page, this error appears in the Output-window:

crit: Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100] Unhandled exception rendering component: Cannot provide a value for property '_vehicleService' on type 'MyCore5AppPWA.Client.Pages.VehicleList'. There is no registered service of type 'MyCore5AppPWA.Client.Services.IVehicleService'. System.InvalidOperationException: Cannot provide a value for property '_vehicleService' on type 'MyCore5AppPWA.Client.Pages.VehicleList'. There is no registered service of type 'MyCore5AppPWA.Client.Services.IVehicleService'. at Microsoft.AspNetCore.Components.ComponentFactory.<>c__DisplayClass6_0.g__Initialize|2(IServiceProvider serviceProvider, IComponent component)

Please note the above notation of "...c__DisplayClass6_0..." that is curious to me (not knowing the significance of this). I have read many of the similar questions/answers related to the subject of this post that do not correct the error.

Here is a snippet from the Startup.cs file:

  public void ConfigureServices(IServiceCollection services) {

     _ = services.AddDbContext<AppDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DbConnection")));

     services.AddScoped<ICustomerRepository, CustomerRepository>();
     services.AddScoped<IVehicleRepository, VehicleRepository>();

     services.AddControllersWithViews();
     services.AddRazorPages();

     services.AddScoped<WeatherForecastService>();

  }

Here is the snippet from the "VehicleList.razor' code file.

//html... declarations.
@layout MainLayout
@page "/vehiclelist"
@using MyCore5AppPWA.Client.Services;
@using MyCore5AppPWA.Shared
//...html omitted...//

@code {

   [Inject] 
   private IVehicleService _vehicleService { get; set; }

   [Inject] 
   private NavigationManager navigationManager { get; set; } ...other code...

I also have in the SERVER-project/Controllers subfolder a "CustomerController.cs" and a "VehicleController.cs".

Since I am rather new to Blazor/Core-5/PWA, I conclude that I am missing something regarding adding new service(s) to the PWA projects.

Your comments and suggestions are very welcome. thanks John

3 Answers

You are injecting a IVehicleService in your VehicleList.razor file. But it doesn't seem like you've registered any implementation of this interface in your DI container?

There is no registered service of type 'MyCore5AppPWA.Client.Services.IVehicleService

Something like

services.AddScoped<IVehicleService, YourImplementation>();

should solve it.

Note that you need to register it with the correct lifetime (Singleton, Scoped or Transient) depending on your implementation.

Dependency injection issues typically surface at runtime because the class consuming these services won’t know they aren’t registered to the container until they request them at runtime. In your case the exception is saying you forgot to register an implementation of IVehicleService. You can register it like any other service by calling .AddScoped<> to your ConfigureServices method.

Thanks to @JOSEFtw for his helpful replies. This led me to the solution to this question.

The Blazor PWA web-app has a SERVER project and a CLIENT-SIDE project. The question stated and @JOSEFtw pointed out that the 'issue' was with the 'Client.Services.IVehicleService' not being registered.

After my comments-thread with @JOSEFtw I thought that something that @JOSEFtw was trying to tell me was not in my realm of consciousness. I made "searches' into the VS-2019 source-files which showed that the client-services are registered in the CLIENT-project's Program.cs file. I discovered this by searching for "CustomerService" text and I found it there. The problem was solved by adding the "IVehicleService, VehicleService" references to the Program.cs file as follows:

builder.Services.AddHttpClient<IVehicleService, VehicleService>(client => {
            client.BaseAddress = new Uri(builder.HostEnvironment.BaseAddress);
         });

I mistakenly thought that the 'repository' interface and implementation classes were registered in the SERVER'S Startup.cs which had the newly added Vehicle-repository references, was all that was necessary to register the services (even client-side services -- me stupid). But I did not know about the Program.cs file for the registration of the client-side registration as a separate registration process.

My lower knowledge of Blazor PWA and C# contributed to this question and comments with @JOSEFtw. Of course we all learn from our mistakes. Thanks to all.

Related