dotnet / dotnet/docs

Conflicting information about Transient Disposables

Open
#43,242 1 comment 0 reactions 0 assignees View on GitHub
:watch: Not Triaged dotnet-fundamentals/svc
Dominant language
No language data
Stars
4.8k
Forks
6.1k
Avg merge
15h 21m
Merged PRs (30d)
370

Description

### Describe the issue or suggestion

The docs at https://learn.microsoft.com/en-us/dotnet/core/extensions/dependency-injection-guidelines#idisposable-guidance-for-transient-and-shared-instances mention:

`Don't register [IDisposable](https://learn.microsoft.com/en-us/dotnet/api/system.idisposable) instances with a transient lifetime. Use the factory pattern instead.`

However when Configuring a http service, I can do:

`integrationServices.AddHttpClient().AddHttpMessageHandler()`

But reading https://learn.microsoft.com/en-us/dotnet/core/extensions/httpclient-factory#message-handler-scopes-in-ihttpclientfactory

It says:
>If you require access to an app DI scope from your message handler, for authentication as an example, you'd encapsulate scope-aware logic in a separate transient DelegatingHandler

So how should the custom `HttpMessageHandlers` be registered. As transient? Will this cause memory leak?

---
#### Document Details

⚠ *Do not edit this section. It is required for learn.microsoft.com ➟ GitHub issue linking.*

* ID: 8d1c8ef1-6a01-df24-34ad-570f88df9303
* Version Independent ID: 7fb2be80-cb54-27bf-4f93-423d8540429e
* Content: [Dependency injection guidelines - .NET](https://learn.microsoft.com/en-us/dotnet/core/extensions/dependency-injection-guidelines#idisposable-guidance-for-transient-and-shared-instances)
* Content Source: [docs/core/extensions/dependency-injection-guidelines.md](https://github.com/dotnet/docs/blob/main/docs/core/extensions/dependency-injection-guidelines.md)
* Service: **dotnet-fundamentals**
* GitHub Login: @IEvangelist
* Microsoft Alias: **dapine**

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.