James X.
James X.

Reputation: 87

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.

Upvotes: 1

Views: 1049

Answers (2)

John
John

Reputation: 11

Blazor Server does not load in HttpClient by default. The SvgLoader depends on it. Is why your getting the error. This will be addressed in the next release. Adding what blazor_guest posted to your startup.cs or program.cs depending on your template will resolve the issue.

See https://github.com/chanan/BlazorStrap/issues/486 For tracking

Upvotes: 1

blazor_guest
blazor_guest

Reputation: 26

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();

Upvotes: 1

Related Questions