dotnet / dotnet/AspNetCore.Docs

Documentation on registering DelegatingHandler's via Service Registration could explicitly say that the delegating handler must be registered with a Transient lifetime

Open
#32,255 1 comment 0 reactions 1 assignee Claimed by @tdykstra View on GitHub
aspnet-core/svc doc-enhancement fundamentals/subsvc Source - Docs.ms
Dominant language
C#
Stars
13.1k
Forks
24.6k
Avg merge
1d 3h
Merged PRs (30d)
97

Description

### Description

[Enter feedback here]
The section with heading "Outgoing request middleware" shows an example of adding one or more delegating handlers that are resolved from the DI container, and while the sample code correctly registers the delegating handler types as Transient it would be useful if the requirement to use Transient lifetime were called out explicitly in the documentation.

Only when following the link through to the [API documentation ](https://learn.microsoft.com/en-us/dotnet/api/microsoft.extensions.dependencyinjection.httpclientbuilderextensions.addhttpmessagehandler?view=dotnet-plat-ext-8.0#microsoft-extensions-dependencyinjection-httpclientbuilderextensions-addhttpmessagehandler-1(microsoft-extensions-dependencyinjection-ihttpclientbuilder)) is there a mention of the Transient requirement.

I fell foul of this when I registered my delegating handlers in the DI container as singletons because they had no shared state, only to get an exception at runtime saying they cannot be reused.

### Page URL

https://learn.microsoft.com/en-us/aspnet/core/fundamentals/http-requests?view=aspnetcore-8.0

### Content source URL

https://github.com/dotnet/AspNetCore.Docs/blob/main/aspnetcore/fundamentals/http-requests.md

### Document ID

3d356334-e672-7e8b-1fef-7944ebee99eb

### Article author

@stevejgordon

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.