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
CodePudding user response:
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.
CodePudding user response:
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.