Skip to content

Blazor server DelegatingHandler for http client causing JavaScript error with prerendering disabled #40336

@McVanS

Description

@McVanS

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

Converting an application to Blazor server, I was going to use HttpFactory. Initial code worked as expected. While refactoring the Http client was changed and setup to use a DelegatingHandler with the aim of retrieving token information from protected local storage and adding the authentication header to the client instead of manually adding the header per http client session. A javascript error occurs with in the DelegatingHandler when the protected local storage function is executed despite prerendering being disabled and the action being triggered by a button click which is outside the rendering cycle.

Expected Behavior

With server prerendering disabled there should not be no difference between accessing protected local storage directly or using a DelegatingHandler and then accessing protected local storage when needed. Raising a prerendering error when prerendering is disabled is a little confusing.

Steps To Reproduce

https://github.com/McVanS/DelegatingHandler-problem-project

Exceptions (if any)

[2022-02-21T13:00:45.690Z] Error: System.InvalidOperationException: JavaScript interop calls cannot be issued at this time. This is because the component is being statically rendered. When prerendering is enabled, JavaScript interop calls can only be performed during the OnAfterRenderAsync lifecycle method.
at Microsoft.AspNetCore.Components.Server.Circuits.RemoteJSRuntime.BeginInvokeJS(Int64 asyncHandle, String identifier, String argsJson, JSCallResultType resultType, Int64 targetInstanceId)
at Microsoft.JSInterop.JSRuntime.InvokeAsync[TValue](Int64 targetInstanceId, String identifier, CancellationToken cancellationToken, Object[] args)
at Microsoft.JSInterop.JSRuntime.InvokeAsync[TValue](Int64 targetInstanceId, String identifier, Object[] args)
at Microsoft.AspNetCore.Components.Server.ProtectedBrowserStorage.ProtectedBrowserStorage.GetAsync[TValue](String purpose, String key)
at DelegatingHandlerProblem.AuthenticationHeaderHandler.GetTokenAsync() in C:\Temp\DelegatingHandler problem project\DelegatingHandlerProblem\AuthenticationHeaderHandler.cs:line 39
at DelegatingHandlerProblem.AuthenticationHeaderHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) in C:\Temp\DelegatingHandler problem project\DelegatingHandlerProblem\AuthenticationHeaderHandler.cs:line 15
at Microsoft.Extensions.Http.Logging.LoggingScopeHttpMessageHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
at System.Net.Http.HttpClient.g__Core|83_0(HttpRequestMessage request, HttpCompletionOption completionOption, CancellationTokenSource cts, Boolean disposeCts, CancellationTokenSource pendingRequestsCts, CancellationToken originalCancellationToken)
at DelegatingHandlerProblem.Pages.Index.ErrorHttpClient() in C:\Temp\DelegatingHandler problem project\DelegatingHandlerProblem\Pages\Index.razor:line 90
at Microsoft.AspNetCore.Components.ComponentBase.CallStateHasChangedOnAsyncCompletion(Task task)
at Microsoft.AspNetCore.Components.RenderTree.Renderer.GetErrorHandledTask(Task taskToHandle, ComponentState owningComponentState)

.NET Version

6.0.102

Anything else?

I have seen this issue in the MAUI Blazor section. I have been researching for a few days now with no definitive answer to this problem. If the capability is there to setup a http client like this but Blazor Server for some reason can not utilize such settings surly the documentation would point that out.

Thank you for reading up to here.
Your effort is appreciated.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions