AspNetCore.Docs
AspNetCore.Docs copied to clipboard
Transient Scope for Custom AuthorizationMessageHandler class
Hello! The document contains the following code snippet:
builder.Services.AddScoped<CustomAuthorizationMessageHandler>(); // 1
// AddHttpClient is an extension in Microsoft.Extensions.Http
builder.Services.AddHttpClient("WebAPI",
client => client.BaseAddress = new Uri("https://www.example.com/base"))
.AddHttpMessageHandler<CustomAuthorizationMessageHandler>(); // 2
where CustomAuthorizationMessageHandler
is registered as a Scoped
service (// 1
). However, the documentation for AddHttpMessageHandler<THandler>(IHttpClientBuilder)
(// 2
) specifies that CustomAuthorizationMessageHandler
must be registered as a Transient
service:
THandler The type of the DelegatingHandler. The handler type must be registered as a transient service.
There seems to be a difference in the lifetime (scoped
vs transient
) across the above documentations. Might this be an error in the documentation, or am I understanding the docs incorrectly?
Thank you!
Document Details
⚠ Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.
- ID: 53f3209c-2414-9b35-6245-383c9b7c6338
- Version Independent ID: cc8dfcd6-8d93-5aa3-c381-2b613e88cdbe
- Content: ASP.NET Core Blazor WebAssembly additional security scenarios
- Content Source: aspnetcore/blazor/security/webassembly/additional-scenarios.md
- Product: aspnet-core
- Technology: aspnetcore-security
- GitHub Login: @guardrex
- Microsoft Alias: riande
cc: @javiercn ... This one requires your help 🚓🚑🚒.
Thanks for the issue, @Zhiyuan-Amos. It was transient prior to the release of 3.2.1. We changed it at the same time that HttpClient
registrations went to scoped on ...
Blazor scoped service registrations https://github.com/dotnet/AspNetCore.Docs/pull/19231
... which came about via docs issue ...
[3.1.6] Incorrect HTTPClient registration https://github.com/dotnet/AspNetCore.Docs/issues/18359
... which was based on the PU issue ...
[Blazor] HttpClient is registered incorrectly https://github.com/dotnet/aspnetcore/issues/21655
... with @javiercn's PR at ...
[Blazor] Register HttpClient as a scoped instance https://github.com/dotnet/aspnetcore/pull/22770
The fix is to register the HttpClient instance in the container as Scoped instead, which in the context of Blazor WebAssembly is equivalent to Singleton, ensuring that only one instance is used across the app.
The docs PR was perhaps overzealous because I noticed that BaseAddressAuthorizationMessageHandler
is registered by the framework as transient (as is AuthorizationMessageHandler
right under it).
So, we'll need a ruling on this one, and then ...
- If it turns out that that because WASM is a special case for this registration and it should remain scoped, I think an added sentence to the explanation makes sense explaining why.
- If we change it, there's one other spot that we also need to change. The
GraphAPIAuthorizationMessageHandler
ingraph-api.md
is also scoped at this time: https://docs.microsoft.com/en-us/aspnet/core/blazor/security/webassembly/graph-api?view=aspnetcore-6.0#named-client-with-graph-api
I don't have money anymore to go out to the workshop, I want to eat, let alone me
UPDATE (8/11): Very sorry for the delay! There's a delay due to the work for the upcoming .NET 7 release. We'll get to this ASAP, but I don't have an ETA.
New information on this that resolves it ...
https://github.com/dotnet/aspnetcore/issues/43234
Leave this open, @Zhiyuan-Amos, as I plan to clarify it in the docs based on Javier's remarks there ...
- Where the custom auth handler is mentioned.
- In the section that talks about detecting transient disposables, since these will show up if that detection code is executed.
does this delay really matter?
Pada tanggal Kam, 11 Agu 2022 21:54, @.***> menulis:
UPDATE (8/11): Very sorry for the delay! There's a delay due to the work going on for the upcoming .NET 7 release. We'll get to this ASAP, but I don't have an ETA.
— Reply to this email directly, view it on GitHub https://github.com/dotnet/AspNetCore.Docs/issues/26528#issuecomment-1212102380, or unsubscribe https://github.com/notifications/unsubscribe-auth/A2CJRSNRLWIS6G4ESW2VTOLVYUH3DANCNFSM54NJM7PQ . You are receiving this because you commented.Message ID: @.***>
Got it.
Pada tanggal Jum, 12 Agu 2022 17:21, @.***> menulis:
New information on this that resolves it ...
dotnet/aspnetcore#43234 https://github.com/dotnet/aspnetcore/issues/43234
Leave this open, @Zhiyuan-Amos https://github.com/Zhiyuan-Amos, as I plan to clarify it in the docs based on Javier's remarks there.
— Reply to this email directly, view it on GitHub https://github.com/dotnet/AspNetCore.Docs/issues/26528#issuecomment-1212957702, or unsubscribe https://github.com/notifications/unsubscribe-auth/A2CJRSMPIFWSAZODIGBY7QLVYYQTDANCNFSM54NJM7PQ . You are receiving this because you commented.Message ID: @.***>
does this delay really matter?
Pada tanggal Kam, 11 Agu 2022 21:54, Luke Latham @.***> menulis:
UPDATE (8/11): Very sorry for the delay! There's a delay due to the work going on for the upcoming .NET 7 release. We'll get to this ASAP, but I don't have an ETA.
— Reply to this email directly, view it on GitHub https://github.com/dotnet/AspNetCore.Docs/issues/26528#issuecomment-1212102380, or unsubscribe https://github.com/notifications/unsubscribe-auth/A2CJRSNRLWIS6G4ESW2VTOLVYUH3DANCNFSM54NJM7PQ . You are receiving this because you commented.Message ID: @.***>
This was resolved by https://github.com/dotnet/AspNetCore.Docs/pull/26755.