Solution For Blazor Error - Cannot provide a value for property, There is no registered service of type [TypeName]

Problem

While experimenting with Blazor, I ran into the following error. 

Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100]
Unhandled exception rendering component: Cannot provide a value for property 'book' on
type 'MyApp.Client.Pages.DepInjection'. There is no registered service of type 'MyApp.Shared.IBook'.
System.InvalidOperationException: Cannot provide a value for property 'book'
on type 'MyApp.Client.Pages.DepInjection'. There is no registered service of type 'MyApp.Shared.IBook'.

I was trying to use a new service on a Blazor component by injecting the service into it as follows. 

@inject IBook book;

 

Solution

The solution for this issue is simple. I had to register the service for dependency injection. As the Blazor client project doesn't have a startup class, the service should be registered in the program.cs file as follows. 

   public static async Task Main(string[] args)
        {
            var builder = WebAssemblyHostBuilder.CreateDefault(args);
            builder.Services.AddSingleton< IBook, Book > ();
            
        }

I hope this helps someone. Happy programming!!

 


Search