LostBeard / SpawnDev.BlazorJS

Full Blazor WebAssembly and Javascript interop. Supports all Javascript data types and web browser APIs.
https://blazorjs.spawndev.com
MIT License
111 stars 7 forks source link

One or more errors occurred. (An item with the same key has already been added. Key: Microsoft.Extensions.Http.IHttpMessageHandlerBuilderFilter) #24

Closed jbrown29 closed 11 months ago

jbrown29 commented 11 months ago

In Blazor WASM PWA .net 8, my client's program.cs file is the following...

using Microsoft.AspNetCore.Components.Web; using Microsoft.AspNetCore.Components.WebAssembly.Authentication; using Microsoft.AspNetCore.Components.WebAssembly.Hosting; using Microsoft.Extensions.DependencyInjection.Extensions; using SpawnDev.BlazorJS; using SpawnDev.BlazorJS.WebWorkers;

...

    var builder = WebAssemblyHostBuilder.CreateDefault(args);
    builder.RootComponents.Add<App>("#app");
    builder.RootComponents.Add<HeadOutlet>("head::after");

    // Add SpawnDev.BlazorJS.BlazorJSRuntime
    builder.Services.AddBlazorJSRuntime();

    // Add SpawnDev.BlazorJS.WebWorkers.WebWorkerService
    builder.Services.AddWebWorkerService();

    //PROBLEM EXISTS BECAUSE OF THIS LINE
    builder.Services.AddHttpClient("ServerAPI", client => client.BaseAddress = new Uri(builder.HostEnvironment.BaseAddress)).AddHttpMessageHandler<BaseAddressAuthorizationMessageHandler>();
    builder.Services.AddScoped(sp => sp.GetRequiredService<IHttpClientFactory>().CreateClient("ServerAPI"));

    builder.Services.AddMsalAuthentication(options =>
    {
        builder.Configuration.Bind("AzureAd", options.ProviderOptions.Authentication);
    });

    await builder.Build().BlazorJSRunAsync();

Expected behavior Using a http message handler (BaseAddressAuthorizationMessageHandler) seems to cause this. My authentication requires this and was wondering if there was some sort of work around or fix.

ERROR ON STARTUP (in chrome dev console output)

Error: One or more errors occurred. (An item with the same key has already been added. Key: Microsoft.Extensions.Http.IHttpMessageHandlerBuilderFilter) at Jn (marshal-to-js.ts:349:18) at Ul (marshal-to-js.ts:306:28) at 00b1e8b6:0x1faca at 00b1e8b6:0x1bf8b at 00b1e8b6:0xf172 at 00b1e8b6:0x1e7e4 at 00b1e8b6:0x1efda at 00b1e8b6:0xcfec at 00b1e8b6:0x440ad at e. (cwraps.ts:338:24)

LostBeard commented 11 months ago

Other than your app using BlazorJS I am not sure how your error is related to BlazorJS. Try your code without BlazorJS enabled and see if the issue persists.

BlazorJS disabled to test:

    var builder = WebAssemblyHostBuilder.CreateDefault(args);
    builder.RootComponents.Add<App>("#app");
    builder.RootComponents.Add<HeadOutlet>("head::after");

    // Add SpawnDev.BlazorJS.BlazorJSRuntime
    //builder.Services.AddBlazorJSRuntime();

    // Add SpawnDev.BlazorJS.WebWorkers.WebWorkerService
    //builder.Services.AddWebWorkerService();

    //PROBLEM EXISTS BECAUSE OF THIS LINE
    builder.Services.AddHttpClient("ServerAPI", client => client.BaseAddress = new Uri(builder.HostEnvironment.BaseAddress)).AddHttpMessageHandler<BaseAddressAuthorizationMessageHandler>();
    builder.Services.AddScoped(sp => sp.GetRequiredService<IHttpClientFactory>().CreateClient("ServerAPI"));

    builder.Services.AddMsalAuthentication(options =>
    {
        builder.Configuration.Bind("AzureAd", options.ProviderOptions.Authentication);
    });

    //await builder.Build().BlazorJSRunAsync();
   await builder.Build().RunAsync();
LostBeard commented 11 months ago

I am looking into it now. It may be related to keyed services which is what your problem line uses. If my tests indicate that is the issue I should have a fix up soon.

LostBeard commented 11 months ago

I just uploaded Nuget package version 2.2.47 for BlazorJS and WebWorkers. The issue should be fixed. Thank you for reporting it.

Keyed services were not being handled properly on startup of BlazorJS. Support for keyed services was added to Blazor in .Net 8 and I had not used them in any of my projects yet. Simply adding a single keyed service was enough to cause the issue.

jbrown29 commented 11 months ago

Updated to the latest and it is also fixed on my side. Thanks for looking into this!