Skip to content

Conflicting information about Transient Disposables #43242

@CoenraadS

Description

@CoenraadS

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<MyClient>().AddHttpMessageHandler<MyHandler>()

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions