Search code examples
blazor.net-6.0blazorstrap

Blazorstrap not working for .NET 6 Blazor?


I am new to Blazor and is giving Blazor in .NET 6.0 a try.

I created the default Blazor Server App project and followed the instructions on Blazorstrap's website (version 5)

https://blazorstrap.io/V5/

// Add services to the container.
builder.Services.AddRazorPages();
builder.Services.AddServerSideBlazor();
builder.Services.AddBlazorStrap();
builder.Services.AddSingleton<WeatherForecastService>();

var app = builder.Build();

I added the service, import, and web page lines and it builds fine.

However, when I start the project, I get the following error

System.AggregateException: 'Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType: BlazorStrap.Utilities.ISvgLoader Lifetime: Scoped ImplementationType: BlazorStrap.Utilities.SvgLoader': Unable to resolve service for type 'System.Net.Http.HttpClient' while attempting to activate 'BlazorStrap.Utilities.SvgLoader'.)'

Is Blazorstrap not working for .NET 6? Or is there a setting I didn't set?

I am a complete Blazor newbie, so any help would be greatly appreciated.


Solution

  • Adding the HttpClient service like this worked for me.

    if (!builder.Services.Any(x => x.ServiceType == typeof(HttpClient)))
    {
    // Setup HttpClient for server side in a client side compatible fashion
    builder.Services.AddScoped<HttpClient>(s =>
    {
        // Creating the URI helper needs to wait until the JS Runtime is initialized, so defer it.
        NavigationManager navman = s.GetRequiredService<NavigationManager>();
        return new HttpClient
        {
            BaseAddress = new Uri(navman.BaseUri)
        };
    });
    }
    builder.Services.AddBlazorStrap();