AspNetCore.Docs icon indicating copy to clipboard operation
AspNetCore.Docs copied to clipboard

Transient Scope for Custom AuthorizationMessageHandler class

Open Zhiyuan-Amos opened this issue 2 years ago • 3 comments

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.

Zhiyuan-Amos avatar Jul 23 '22 03:07 Zhiyuan-Amos

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 in graph-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

guardrex avatar Jul 23 '22 10:07 guardrex

I don't have money anymore to go out to the workshop, I want to eat, let alone me

Jhstvf4t avatar Jul 24 '22 14:07 Jhstvf4t

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.

guardrex avatar Aug 11 '22 14:08 guardrex

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 ...

guardrex avatar Aug 12 '22 10:08 guardrex

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: @.***>

Jhstvf4t avatar Aug 16 '22 12:08 Jhstvf4t

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: @.***>

Jhstvf4t avatar Aug 16 '22 12:08 Jhstvf4t

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: @.***>

Jhstvf4t avatar Oct 11 '22 09:10 Jhstvf4t

This was resolved by https://github.com/dotnet/AspNetCore.Docs/pull/26755.

guardrex avatar Oct 11 '22 10:10 guardrex