-
Notifications
You must be signed in to change notification settings - Fork 9.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Blazor 8 - delegating handler cannot use local/session storage #55770
Comments
@AdamJachocki thanks for contacting us. IJSRuntime is a scoped service. You are consuming it from inside a delegating handler, if you are using HttpClientFactory that's creating its own scope, which results in the service not being initialized. Not sure what's going on in your app/code, but its signaling that you are getting a new instance of an uninitialized IJSRuntime. |
@javiercn So what are the best practices for getting local storage when requesting with httpclient? |
OK, I found a solution but it was like the other problem. I mean, I searched it differently. You need to put scope services into DI scope of delegating handler. And it's done pretty easy. Official solution is to use CircuitHandler: https://learn.microsoft.com/en-us/aspnet/core/blazor/fundamentals/dependency-injection?view=aspnetcore-8.0#access-server-side-blazor-services-from-a-different-di-scope But I had some other problems with that, so I did some other work. My solution can use client side stuff.
So I can use it like that insinde Blazor client side:
Now, the real thing is that my UserRequests class inherits from BaseRequest class that looks something like that:
Now every Request class looks like that:
And the real stuff is done in delegating handler:
So this is quite simple:
That's what I came up with and it works quite good. |
@AdamJachocki thanks for the additional details. I don't fully follow everything in detail, but what you've said sounds reasonable. With HttpClientFactory is about getting scoped services in the right scope. The challenge is understanding the scopes that are at play at a given point (since HttpClient creates their own). |
@ElderJames what @AdamJachocki came up with, and its documented in the link he referred to. |
Official solution uses CircuitHandler to have proper scope in Delegating handler. My solution doesn't even uses this scope. It creates HttpRequestMessage on client side with proper information in temp headers. But this solution is more specific. |
This issue has been resolved and has not had any activity for 1 day. It will be closed for housekeeping purposes. See our Issue Management Policies for more information. |
Is there an existing issue for this?
Describe the bug
I consider it a bug.
First of all notice that this is not DI problem, as I have already gone through ServicesAccessorCircuitHandler that fills ScopedService accessors.
Blazor 8 interactive server application. I have turned off prerendering.
In my component I call API client from OnAfterRenderAsync method like:
RefreshDocuments calls my API client which has delegating handler that is suppose to attach and refresh access token to headers:
Now in AddAccessTokenIfNeeded I try to get a service that dependes on browser session storage:
return _scopedServiceAccessor.Services?.GetRequiredService<IClientTokenStorage>();
And note that this works - I get the proper service.
But when I call session storage read, I get this JS exception:
"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."
although I called this from withing OnAfterRenderAsync.
This one makes Blazor 8 not production ready from my point of view. Unless there is some workaround for this?
Expected Behavior
Session/Local storage should be readable from DelegatingHandler
Steps To Reproduce
No response
Exceptions (if any)
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.
.NET Version
8.0.5
Anything else?
No response
The text was updated successfully, but these errors were encountered: