[Bug] Does not work inside Http DelegatingHandler in .NET MAUI Blazor
See original GitHub issueThe following code works perfectly on Blazor WebAssembly but does not work in .NET MAUI Blazor:
[TransientService]
public class AuthorizationDelegatingHandler : DelegatingHandler
{
private readonly ILocalStorageService _localStorage;
public AuthorizationDelegatingHandler(ILocalStorageService localStorage)
{
_localStorage = localStorage;
}
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
if (request == null)
{
throw new ArgumentNullException(nameof(request));
}
// This line perfectly works in Blazor WebAssebly app but does not work in .NET MAUI Blazor
LoggedInUserInfo loggedInUserInfo = await _localStorage.GetItemAsync<LoggedInUserInfo>(LocalStorageKey.LoggedInUserInfo);
if (loggedInUserInfo != null)
{
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", loggedInUserInfo.AccessToken);
}
HttpResponseMessage httpResponseMessage = await base.SendAsync(request, cancellationToken);
return httpResponseMessage;
}
}
The following line does work in any place of the .NET MAUI Blazor app but does not work inside AuthorizationDelegatingHandler
:
LoggedInUserInfo loggedInUserInfo = await _localStorage.GetItemAsync<LoggedInUserInfo>(LocalStorageKey.LoggedInUserInfo);
And throws the following error:
System.NullReferenceException: Object reference not set to an instance of an object.
[DOTNET] at Microsoft.AspNetCore.Components.WebView.Services.WebViewJSRuntime.BeginInvokeJS(Int64 taskId, String identifier, String argsJson, JSCallResultType resultType, Int64
[DOTNET] targetInstanceId) in Microsoft.AspNetCore.Components.WebView.dll:token 0x6000113+0x0
[DOTNET] at Microsoft.JSInterop.JSRuntime.InvokeAsync[String](Int64 targetInstanceId, String identifier, CancellationToken cancellationToken, Object[] args) in Microsoft.JSInter
[DOTNET] op.dll:token 0x600003a+0x10a
[DOTNET] at Microsoft.JSInterop.JSRuntime.InvokeAsync[String](String identifier, CancellationToken cancellationToken, Object[] args) in Microsoft.JSInterop.dll:token 0x6000038+0x0
[DOTNET] at Microsoft.JSInterop.JSRuntimeExtensions.InvokeA
[DOTNET] sync[String](IJSRuntime jsRuntime, String identifier, CancellationToken cancellationToken, Object[] args) in Microsoft.JSInterop.dll:token 0x6000048+0xe
[DOTNET] at Microsoft.JSInterop.JSRuntimeExtens07-16 00:29:47.884 I/DOTNET (24839): at Blazored.LocalStorage.BrowserStorageProvider.GetItemAsync(String key, Nullable`1 cancellationToke
[DOTNET] n) in Blazored.LocalStorage.dll:token 0x6000005+0x24
[DOTNET] at Blazored.LocalStorage.LocalStorageService.<GetItemAsync>d__5`1[[MauiBlazor.Shared.Models.IdentityModels.LoggedInUserInfo, MauiBlazor.Shared, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].
[DOTNET] MoveNext() in Blazored.LocalStorage.dll:token 0x6000082+0x29
[DOTNET] at MauiBlazor.Shared.Extensions.LocalStorageServiceExtensions.GetUserInfoAsync(ILocalStorageService localStorage) in D:\GitHub\CleanArchitecture\src\ClientApps\MauiBlazor.Shared\Extensions\Loc
[DOTNET] alStorageServiceExtensions.cs:line 19
[DOTNET] at MauiBlazor.Shared.Common.AuthorizationDelegatingHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) in D:\GitHub\CleanArchitecture\src\ClientApps\MauiBlazor.Shared\Common\Authorizat
[DOTNET] ionDelegatingHandler.cs:line 30
[DOTNET] at System.Net.Http.Ht
[DOTNET] tpClient.<SendAsync>g__Core|83_0(HttpRequestMessage request, HttpCompletionOption completionOption, CancellationTokenSource cts, Boolean disposeCts, CancellationTokenSource pendingRequestsCts, CancellationToken originalCancellationToken) in System.Net.Http
[DOTNET] at MauiBlazor.Shared.Services.UserService.LoginAsync(LoginModel loginModel) in D:\GitHub\CleanArchitecture\src\ClientApps\MauiBlazor.Shared\Services\UserService.cs:line 128
[DOTNET] at MauiBlazorApp.Components.IdentityComponents.Log
[DOTNET] inComponent.HandleValidSubmitAsync() in D:\GitHub\CleanArchitecture\src\ClientApps\MauiBlazorApp\MauiBlazorApp\Components\IdentityComponents\LoginComponent.razor.cs:line 51
Issue Analytics
- State:
- Created 2 years ago
- Comments:14 (14 by maintainers)
Top Results From Across the Web
Accessing Local Storage inside DelegatingHandler in ...
We need to access local storage to get the access token stored in local storage and bind it to the HTTP request but...
Read more >Custom DelegatingHandler unsuccessful status code ...
I have a custom DelegatingHandler that scans calls to the back-end watching for 401 (cookie expired) and redirects the browser to relogin. The...
Read more >Troubleshoot known issues - .NET MAUI
This article describes some of the known issues with .NET Multi-platform App UI (.NET MAUI), and how you can solve or work around...
Read more >Blazored
[Bug] Does not work inside Http DelegatingHandler in .NET MAUI Blazor $ 0. Created 2 years ago in Blazored/LocalStorage with 12 comments. The...
Read more >Creating and Using HTTP Client SDKs in .NET 6 - InfoQ
In this article, the author explains the process behind developing HTTP Client SDKs in .NET 6. The article also contains a sample SDK ......
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Ah apologies. I didn’t spot the service scoping. The issue here is that we register the service as scoped.
I’m going to add an additional method for registering services as transient which will solve this. No ETA right now though.
Closing as there’s been no further activity.