-
Notifications
You must be signed in to change notification settings - Fork 6.1k
Removed .NET framework versioning where appropriate #55340
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,14 +1,14 @@ | ||
| --- | ||
| title: Use the IHttpClientFactory | ||
| description: Learn how to use the HttpClient and IHttpClientFactory implementations with dependency injection in your .NET workloads. | ||
| ms.date: 05/06/2025 | ||
| ms.date: 08/06/2026 | ||
| --- | ||
|
|
||
| # IHttpClientFactory with .NET | ||
|
|
||
| In this article, you'll learn how to use the `IHttpClientFactory` interface to create `HttpClient` types with various .NET fundamentals, such as dependency injection (DI), logging, and configuration. The <xref:System.Net.Http.HttpClient> type was introduced in .NET Framework 4.5, which was released in 2012. In other words, it's been around for a while. `HttpClient` is used for making HTTP requests and handling HTTP responses from web resources identified by a <xref:System.Uri>. The HTTP protocol makes up the vast majority of all internet traffic. | ||
| This article explains how to use the `IHttpClientFactory` interface to create `HttpClient` types with .NET fundamentals such as dependency injection (DI), logging, and configuration. <xref:System.Net.Http.HttpClient> makes HTTP requests and handles HTTP responses from web resources identified by a <xref:System.Uri>. The HTTP protocol makes up the vast majority of all internet traffic. | ||
|
|
||
| With modern application development principles driving best practices, the <xref:System.Net.Http.IHttpClientFactory> serves as a factory abstraction that can create `HttpClient` instances with custom configurations. <xref:System.Net.Http.IHttpClientFactory> was introduced in .NET Core 2.1. Common HTTP-based .NET workloads can take advantage of resilient and transient-fault-handling third-party middleware with ease. | ||
| To use <xref:System.Net.Http.IHttpClientFactory> in .NET or .NET Framework, reference the [`Microsoft.Extensions.Http`](https://www.nuget.org/packages/microsoft.extensions.http) NuGet package. The factory creates `HttpClient` instances with custom configurations and lets .NET workloads use resilient, transient-fault-handling third-party middleware. | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do we need "in .NET or .NET Framework"? |
||
|
|
||
| > [!WARNING] | ||
| > If your app requires cookies, it's recommended to avoid using <xref:System.Net.Http.IHttpClientFactory>. Pooling the <xref:System.Net.Http.HttpMessageHandler> instances results in sharing of <xref:System.Net.CookieContainer> objects. Unanticipated <xref:System.Net.CookieContainer> sharing might leak cookies between unrelated parts of the application. Moreover, when <xref:Microsoft.Extensions.Http.HttpClientFactoryOptions.HandlerLifetime> expires, the handler is recycled, meaning that all cookies stored in its <xref:System.Net.CookieContainer> are lost. | ||
|
|
@@ -19,7 +19,7 @@ With modern application development principles driving best practices, the <xref | |
|
|
||
| ## The `IHttpClientFactory` type | ||
|
|
||
| All of the sample source code provided in this article requires the installation of the [`Microsoft.Extensions.Http`](https://www.nuget.org/packages/microsoft.extensions.http) NuGet package. Furthermore, the code examples demonstrate the usage of HTTP `GET` requests to retrieve user `Todo` objects from the free [{JSON} Placeholder](https://jsonplaceholder.typicode.com/) API. | ||
| All of the sample source code provided in this article requires the [`Microsoft.Extensions.Http`](https://www.nuget.org/packages/microsoft.extensions.http) and [`Microsoft.Extensions.Hosting`](https://www.nuget.org/packages/microsoft.extensions.hosting) NuGet packages. Furthermore, the code examples demonstrate the usage of HTTP `GET` requests to retrieve user `Todo` objects from the free [{JSON} Placeholder](https://jsonplaceholder.typicode.com/) API. | ||
|
|
||
| When you call any of the <xref:Microsoft.Extensions.DependencyInjection.HttpClientFactoryServiceCollectionExtensions.AddHttpClient*> extension methods, you're adding the `IHttpClientFactory` and related services to the <xref:Microsoft.Extensions.DependencyInjection.IServiceCollection>. The `IHttpClientFactory` type offers the following benefits: | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -2,6 +2,7 @@ | |||||
| title: "Connection Strings and Configuration Files" | ||||||
| description: Learn how to store connection strings for ADO.NET applications in an application configuration file. | ||||||
| ms.date: "03/30/2017" | ||||||
| ai-usage: ai-assisted | ||||||
| dev_langs: | ||||||
| - "csharp" | ||||||
| - "vb" | ||||||
|
|
@@ -63,14 +64,14 @@ Embedding connection strings in your application's code can lead to security vul | |||||
|
|
||||||
| ## Retrieve Connection Strings at Run Time | ||||||
|
|
||||||
| .NET Framework 2.0 introduced new classes in the <xref:System.Configuration> namespace to simplify retrieving connection strings from configuration files at runtime. You can programmatically retrieve a connection string by name or by provider name. | ||||||
| Use classes in the <xref:System.Configuration> namespace to retrieve connection strings from configuration files at runtime, by name or provider name. | ||||||
|
|
||||||
| > [!NOTE] | ||||||
| > The **machine.config** file also contains a `connectionStrings` section, which contains connection strings used by Visual Studio. When retrieving connection strings by provider name from the **app.config** file in a Windows application, the connection strings in **machine.config** get loaded first, and then the entries from **app.config**. Adding `clear` immediately after the `connectionStrings` element removes all inherited references from the data structure in memory, so that only the connection strings defined in the local **app.config** file are considered. | ||||||
|
|
||||||
| ### Work with the Configuration Classes | ||||||
|
|
||||||
| Starting with .NET Framework 2.0, <xref:System.Configuration.ConfigurationManager> is used when working with configuration files on the local computer, replacing the deprecated <xref:System.Configuration.ConfigurationSettings> class. <xref:System.Web.Configuration.WebConfigurationManager> is used to work with ASP.NET configuration files. It is designed to work with configuration files on a Web server, and allows programmatic access to configuration file sections such as **system.web**. | ||||||
| Use <xref:System.Configuration.ConfigurationManager> to work with configuration files on the local computer. It replaces the deprecated <xref:System.Configuration.ConfigurationSettings> class. Use <xref:System.Web.Configuration.WebConfigurationManager> to work with ASP.NET configuration files. It works with configuration files on a web server and provides programmatic access to configuration file sections such as **system.web**. | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||||||
|
|
||||||
| > [!NOTE] | ||||||
| > Accessing configuration files at runtime requires granting permissions to the caller; the required permissions depend on the type of application, configuration file, and location. For more information, see <xref:System.Web.Configuration.WebConfigurationManager> for ASP.NET applications, and <xref:System.Configuration.ConfigurationManager> for Windows applications. | ||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -80,7 +80,7 @@ The CLR activation system provides the same behavior and UI on Windows 8 as it d | |||||
|  | ||||||
|
|
||||||
| > [!NOTE] | ||||||
| > The .NET Framework 4.5 replaces the .NET Framework 4 (CLR 4) on the user's computer. Therefore, .NET Framework 4 applications run seamlessly, without displaying this dialog box, on Windows 8. | ||||||
| > The .NET Framework 4.5+ replaces the .NET Framework 4 (CLR 4) on the user's computer. Therefore, .NET Framework 4 applications run seamlessly, without displaying this dialog box, on Windows 8. | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
|
||||||
| When the .NET Framework 3.5 is installed, users can run applications that depend on .NET Framework 2.0, 3.0, or 3.5 on their Windows 8 computers. They can also run .NET Framework 1.0 and 1.1 applications, provided that those applications are not explicitly configured to run only on the .NET Framework 1.0 or 1.1. See [Migrating from the .NET Framework 1.1](../migration-guide/migrating-from-the-net-framework-1-1.md). | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Delete this paragraph? |
||||||
|
|
||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -48,7 +48,7 @@ Event tracing for Windows (ETW) events can be filtered by category and level. Ev | |||||
| |`ContentionKeyword`|0x00004000|Enables the collection of [contention events](contention-etw-events.md).| | ||||||
| |`ExceptionKeyword`|0x00008000|Enables the collection of [exception events](exception-thrown-v1-etw-event.md).| | ||||||
| |`ThreadingKeyword`|0x00010000|Enables the collection of [thread pool events](thread-pool-etw-events.md).| | ||||||
| |`OverrideAndSuppressNGenEventsKeyword`|0x00040000|(Available in the .NET Framework 4.5 and later.) Suppresses the high-overhead `NGenKeyword` keyword and prevents the generation of events for methods that are inside NGen modules. Starting with .NET Framework 4.5, profiling tools should use `OverrideAndSuppressNGenEventsKeyword` and `NGenKeyword` together to suppress the generation of events for methods in NGen modules. This enables the profiling tool to use the more efficient NGen PDBs to get information about methods in NGen modules. The CLR in the .NET Framework 4 and earlier versions does not support the creation of NGen PDBs. In these earlier versions, the CLR will not recognize `OverrideAndSuppressNGenEventsKeyword` and will process `NGenKeyword` to generate events for methods in NGen modules.| | ||||||
| |`OverrideAndSuppressNGenEventsKeyword`|0x00040000|(Available in the .NET Framework 4.5 and later.) Suppresses the high-overhead `NGenKeyword` keyword and prevents the generation of events for methods that are inside NGen modules. Profiling tools should use `OverrideAndSuppressNGenEventsKeyword` and `NGenKeyword` together to suppress the generation of events for methods in NGen modules. This enables the profiling tool to use the more efficient NGen PDBs to get information about methods in NGen modules. The CLR in the .NET Framework 4 and earlier versions does not support the creation of NGen PDBs. In these earlier versions, the CLR will not recognize `OverrideAndSuppressNGenEventsKeyword` and will process `NGenKeyword` to generate events for methods in NGen modules.| | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| |`PerfTrackKeyWord`|0x2000000|Enables the collection of the `ModuleLoad` and `ModuleRange` events.| | ||||||
| |`StackKeyword`|0x40000000|Enables the collection of CLR [stack trace events](stack-etw-event.md).| | ||||||
|
|
||||||
|
|
@@ -67,7 +67,7 @@ Event tracing for Windows (ETW) events can be filtered by category and level. Ev | |||||
| |`EndRundownKeyword`|0x00000100|Enables the enumeration of system state during an end rundown.| | ||||||
| |`AppDomainResourceManagementRundownKeyword`|0x00000800|Enables the collection of events for resource monitoring at an <xref:System.AppDomain> level when used with `StartRundownKeyword` or `EndRundownKeyword`.| | ||||||
| |`ThreadingKeyword`|0x00010000|Enables the collection of thread pool events.| | ||||||
| |`OverrideAndSuppressNGenEventsRundownKeyword`|0x00040000|(Available in the .NET Framework 4.5 and later.) Suppresses the high-overhead `NGenRundownKeyword` keyword and prevents the generation of events for methods that are inside NGen modules. Starting with .NET Framework 4.5, profiling tools should use `OverrideAndSuppressNGenEventsRundownKeyword` and `NGenRundownKeyword` together to suppress the generation of events for methods in NGen modules. This enables the profiling tool to use the more efficient NGen PDBs to get information about methods in NGen modules. The CLR in the .NET Framework 4 and earlier versions does not support the creation of NGen PDBs. In these earlier versions, the CLR will not recognize `OverrideAndSuppressNGenEventsRundownKeyword` and will process `NGenRundownKeyword` to generate events for methods in NGen modules.| | ||||||
| |`OverrideAndSuppressNGenEventsRundownKeyword`|0x00040000|(Available in the .NET Framework 4.5 and later.) Suppresses the high-overhead `NGenRundownKeyword` keyword and prevents the generation of events for methods that are inside NGen modules. Profiling tools should use `OverrideAndSuppressNGenEventsRundownKeyword` and `NGenRundownKeyword` together to suppress the generation of events for methods in NGen modules. This enables the profiling tool to use the more efficient NGen PDBs to get information about methods in NGen modules. The CLR in the .NET Framework 4 and earlier versions does not support the creation of NGen PDBs. In these earlier versions, the CLR will not recognize `OverrideAndSuppressNGenEventsRundownKeyword` and will process `NGenRundownKeyword` to generate events for methods in NGen modules.| | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| |`PerfTrackKeyWord`|0x2000000|Enables the collection of the `ModuleDCStart`, `ModuleDCEnd`, `ModuleRangeDCStart`, and `ModuleRangeDCEnd` events.| | ||||||
|
|
||||||
| <a name="runtime_combo"></a> | ||||||
|
|
||||||
Uh oh!
There was an error while loading. Please reload this page.