Blazor TypeClient `AddHttpMessageHandler` emits `InvalidOperationException` (ValueFactory attempted to access the Value property of this instance.)

ibocon 31 Reputation points
2021-06-23T04:25:40.517+00:00

I try to apply ASP.NET Core Blazor WebAssembly additional security scenarios Typed HttpClient with OIDC authentication like below,

   cs  
   // Blazor WebAssembly 'Program.cs'  
   public static async Task Main(string[] args)  
   {  
       var builder = WebAssemblyHostBuilder.CreateDefault(args);  
       builder.RootComponents.Add<App>("#app");  
     
       builder.Services.AddHttpClient<ApplicationHttpClient>(nameof(ApplicationHttpClient), client =>  
       {  
           client.BaseAddress = new Uri(builder.HostEnvironment.BaseAddress);  
       }).AddHttpMessageHandler<BaseAddressAuthorizationMessageHandler>();  
     
       builder.Services.AddScoped(serviceProvider =>  
       {  
           return serviceProvider.GetRequiredService<IHttpClientFactory>().CreateClient(nameof(ApplicationHttpClient));  
       });  
     
       builder.Services.AddOidcAuthentication<RemoteAuthenticationState, RemoteUserAccount>(options =>  
       {  
           builder.Configuration.Bind("Google", options.ProviderOptions);  
           options.ProviderOptions.DefaultScopes.Add("email");  
       }).AddAccountClaimsPrincipalFactory<RemoteAuthenticationState, RemoteUserAccount, CustomAccountClaimsPrincipalFactory>();  
     
       await builder.Build().RunAsync();  
   }  

I met below exceptions if Build & Run,

   crit: Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100]  
         Unhandled exception rendering component: ValueFactory attempted to access the Value property of this instance.  
   System.InvalidOperationException: ValueFactory attempted to access the Value property of this instance.  
      at System.Lazy`1[[Microsoft.Extensions.Http.ActiveHandlerTrackingEntry, Microsoft.Extensions.Http, Version=5.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60]].ViaFactory(LazyThreadSafetyMode mode) in System.Private.CoreLib.dll:token 0x6000fa7+0x43  
      at System.Lazy`1[[Microsoft.Extensions.Http.ActiveHandlerTrackingEntry, Microsoft.Extensions.Http, Version=5.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60]].ExecutionAndPublication(LazyHelper executionAndPublication, Boolean useDefaultConstructor) in System.Private.CoreLib.dll:token 0x6000fa8+0x22  
      at System.Lazy`1[[Microsoft.Extensions.Http.ActiveHandlerTrackingEntry, Microsoft.Extensions.Http, Version=5.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60]].CreateValue() in System.Private.CoreLib.dll:token 0x6000fad+0x74  
      at System.Lazy`1[[Microsoft.Extensions.Http.ActiveHandlerTrackingEntry, Microsoft.Extensions.Http, Version=5.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60]].get_Value() in System.Private.CoreLib.dll:token 0x6000fb3+0xa  
      at Microsoft.Extensions.Http.DefaultHttpClientFactory.CreateHandler(String name) in Microsoft.Extensions.Http.dll:token 0x6000065+0xe  
      at Microsoft.Extensions.Http.DefaultHttpClientFactory.CreateClient(String name) in Microsoft.Extensions.Http.dll:token 0x6000064+0xe  

If I comment out AddHttpMessageHandler<BaseAddressAuthorizationMessageHandler>(), it works well like before.

.NET 6.0.100-preview.5 21302.13
Visual Studio 2019 16.10.2

To Reproduce

Github

  1. Run 'TypedHttpClient.Server'
  2. Navigate to 'FetchData' menu
  3. View console error message.

Exception comes from DefaultHttpClientFactory.cs.

   // Microsoft.Extensions.Http.DefaultHttpClientFactory.cs  
   public HttpMessageHandler CreateHandler(string name)  
   {  
       if (name == null)  
       {  
           throw new ArgumentNullException(nameof(name));  
       }  
     
       // Here  
       ActiveHandlerTrackingEntry entry = _activeHandlers.GetOrAdd(name, _entryFactory).Value;  
     
       StartHandlerEntryTimer(entry);  
     
       return entry.Handler;  
   }  
Blazor
Blazor
A free and open-source web framework that enables developers to create web apps using C# and HTML being developed by Microsoft.
1,371 questions
.NET Runtime
.NET Runtime
.NET: Microsoft Technologies based on the .NET software framework.Runtime: An environment required to run apps that aren't compiled to machine language.
1,117 questions
{count} votes