Skip to content
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

Transient Scope for Custom AuthorizationMessageHandler class #26528

Closed
Zhiyuan-Amos opened this issue Jul 23, 2022 · 8 comments · Fixed by #26755
Closed

Transient Scope for Custom AuthorizationMessageHandler class #26528

Zhiyuan-Amos opened this issue Jul 23, 2022 · 8 comments · Fixed by #26755
Assignees
Labels
Blazor doc-enhancement Pri2 Source - Docs.ms Docs Customer feedback via GitHub Issue

Comments

@Zhiyuan-Amos
Copy link

Zhiyuan-Amos commented Jul 23, 2022

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.

@dotnet-bot dotnet-bot added Blazor Source - Docs.ms Docs Customer feedback via GitHub Issue labels Jul 23, 2022
@guardrex guardrex self-assigned this Jul 23, 2022
@guardrex
Copy link
Collaborator

guardrex commented Jul 23, 2022

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
#19231

... which came about via docs issue ...

[3.1.6] Incorrect HTTPClient registration
#18359

... which was based on the PU issue ...

[Blazor] HttpClient is registered incorrectly
dotnet/aspnetcore#21655

... with @javiercn's PR at ...

[Blazor] Register HttpClient as a scoped instance
dotnet/aspnetcore#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 ...

@guardrex guardrex added the PU label Jul 23, 2022
@Jhstvf4t

This comment was marked as spam.

@guardrex
Copy link
Collaborator

guardrex commented Aug 11, 2022

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
Copy link
Collaborator

guardrex commented Aug 12, 2022

New information on this that resolves it ...

dotnet/aspnetcore#43234

Leave this open, @Zhiyuan-Amos, as I plan to clarify it in the docs based on Javier's remarks there ...

@Jhstvf4t
Copy link

Jhstvf4t commented Aug 16, 2022 via email

@Jhstvf4t
Copy link

Jhstvf4t commented Aug 16, 2022 via email

@Jhstvf4t
Copy link

Jhstvf4t commented Oct 11, 2022 via email

@guardrex
Copy link
Collaborator

This was resolved by #26755.

@dotnet dotnet locked as resolved and limited conversation to collaborators Oct 11, 2022
@Rick-Anderson Rick-Anderson moved this to Done in Blazor.Docs Oct 18, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Blazor doc-enhancement Pri2 Source - Docs.ms Docs Customer feedback via GitHub Issue
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

4 participants