{"id":1708,"date":"2025-02-06T18:13:18","date_gmt":"2025-02-06T18:13:18","guid":{"rendered":"https:\/\/rssfeedtelegrambot.bnaya.co.il\/index.php\/2025\/02\/06\/net-9-networking-improvements\/"},"modified":"2025-02-06T18:13:18","modified_gmt":"2025-02-06T18:13:18","slug":"net-9-networking-improvements","status":"publish","type":"post","link":"https:\/\/rssfeedtelegrambot.bnaya.co.il\/index.php\/2025\/02\/06\/net-9-networking-improvements\/","title":{"rendered":".NET 9 Networking Improvements"},"content":{"rendered":"<p>Continuing our tradition, we are excited to share a blog post highlighting the latest and most interesting changes in the networking space with the new <a href=\"https:\/\/devblogs.microsoft.com\/dotnet\/announcing-dotnet-9\/\">.NET release<\/a>. This year, we are introducing updates in the <a href=\"https:\/\/devblogs.microsoft.com\/dotnet\/dotnet-9-networking-improvements\/#http\">HTTP<\/a> space, new <a href=\"https:\/\/devblogs.microsoft.com\/dotnet\/dotnet-9-networking-improvements\/#httpclientfactory\">HttpClientFactory<\/a> APIs, <a href=\"https:\/\/devblogs.microsoft.com\/dotnet\/dotnet-9-networking-improvements\/#net-framework-compatibility\">.NET Framework compatibility<\/a> improvements, and more.<\/p>\n<h2>HTTP<\/h2>\n<p>In the following section, we\u2019re introducing the most impactful changes in the HTTP space. Among which belong perf improvements in connection pooling, support for multiple HTTP\/3 connections, auto-updating Windows proxy, and, last but not least, community contributions.<\/p>\n<h3>Connection Pooling<\/h3>\n<p>In this release, we made two impactful performance improvements in HTTP connection pooling.<\/p>\n<p>We added opt-in support for multiple HTTP\/3 connections. Using more than one HTTP\/3 connection to the peer is discouraged by the <a href=\"https:\/\/datatracker.ietf.org\/doc\/html\/rfc9114#section-3.3\">RFC 9114<\/a> since the connection can multiplex parallel requests. However, in certain scenarios, like server-to-server, one connection might become a bottleneck even with request multiplexing. We saw such limitations with HTTP\/2 (<a href=\"https:\/\/github.com\/dotnet\/runtime\/issues\/35088\">dotnet\/runtime#35088<\/a>), which has the same concept of multiplexing over one connection. For the same reasons (<a href=\"https:\/\/github.com\/dotnet\/runtime\/issues\/51775\">dotnet\/runtime#51775<\/a>), we decided to implement multiple connection support for HTTP\/3 (<a href=\"https:\/\/github.com\/dotnet\/runtime\/issues\/101535\">dotnet\/runtime#101535<\/a>).<\/p>\n<p>The implementation itself tries to closely match the behavior of HTTP\/2 multiple connections. Which, at the moment, always prefer to saturate existing connections with as many requests as allowed by the peer before opening a new one. Note that this is an implementation detail and the behavior might change in the future.<\/p>\n<p>As a result, <a href=\"https:\/\/github.com\/aspnet\/Benchmarks\/tree\/main\/src\/BenchmarksApps\/HttpClientBenchmarks\">our benchmarks<\/a> showed a nontrivial increase in requests per seconds (RPS), comparison for 10,000 parallel requests:<\/p>\n<p>client<br \/>\nsingle HTTP\/3 connection<br \/>\nmultiple HTTP\/3 connections<\/p>\n<p>Max CPU Usage (%)<br \/>\n35<br \/>\n92<\/p>\n<p>Max Cores Usage (%)<br \/>\n971<br \/>\n2,572<\/p>\n<p>Max Working Set (MB)<br \/>\n3,810<br \/>\n6,491<\/p>\n<p>Max Private Memory (MB)<br \/>\n4,415<br \/>\n7,228<\/p>\n<p>Processor Count<br \/>\n28<br \/>\n28<\/p>\n<p>First request duration (ms)<br \/>\n519<br \/>\n594<\/p>\n<p>Requests<br \/>\n345,446<br \/>\n4,325,325<\/p>\n<p>Mean RPS<br \/>\n23,069<br \/>\n288,664<\/p>\n<p>Note that the increase in <em>Max CPU Usage<\/em> implies better CPU utilization, which means that the CPU is busy processing requests instead of being idle.<\/p>\n<p>This feature can be turned on via the EnableMultipleHttp3Connections property on <a href=\"https:\/\/learn.microsoft.com\/dotnet\/api\/system.net.http.socketshttphandler.enablemultiplehttp3connections\">SocketsHttpHandler<\/a>:<\/p>\n<p>var client = new HttpClient(new SocketsHttpHandler<br \/>\n{<br \/>\n    EnableMultipleHttp3Connections = true<br \/>\n});<\/p>\n<p>We also addressed lock contention in HTTP 1.1 connection pooling (<a href=\"https:\/\/github.com\/dotnet\/runtime\/issues\/70098\">dotnet\/runtime#70098<\/a>). The HTTP 1.1 connection pool previously used a single lock to manage the list of connections and the queue of pending requests. This lock was observed to be a bottleneck in high throughput scenarios on machines with a high number of CPU cores. We resolved this problem (<a href=\"https:\/\/github.com\/dotnet\/runtime\/pull\/99364\">dotnet\/runtime#99364<\/a>) by replacing an ordinary list with a lock with a concurrent collection. We chose <a href=\"https:\/\/learn.microsoft.com\/dotnet\/api\/system.collections.concurrent.concurrentstack-1\">ConcurrentStack<\/a> as it preserves the observable behavior when requests are handled by the newest available connection, which allows collecting older connections when their configured <a href=\"https:\/\/learn.microsoft.com\/dotnet\/api\/system.net.http.socketshttphandler.pooledconnectionlifetime\">lifetime<\/a> expires. The throughput of HTTP 1.1 requests in our benchmarks increased by more than 30%:<\/p>\n<p>Client<br \/>\n.NET 8.0<br \/>\n.NET 9.0<br \/>\nIncrease<\/p>\n<p>Requests<br \/>\n80,028,791<br \/>\n107,128,778<br \/>\n+33.86%<\/p>\n<p>Mean RPS<br \/>\n666,886<br \/>\n892,749<br \/>\n+33.87%<\/p>\n<h3>Proxy Auto Update on Windows<\/h3>\n<p>One of the main pain points when debugging HTTP traffic of applications using earlier versions of .NET is that the application doesn\u2019t react to changes in Windows proxy settings (<a href=\"https:\/\/github.com\/dotnet\/runtime\/issues\/46910\">dotnet\/runtime#70098<\/a>). The proxy settings were previously initialized once per process with no reasonable ability to refresh the settings. For example (with .NET 8), <a href=\"https:\/\/learn.microsoft.com\/dotnet\/api\/system.net.http.httpclient.defaultproxy\">HttpClient.DefaultProxy<\/a> returns the same instance upon repeated access and never refetch the settings. As a result, tools like Fiddler, that set themself as system proxy to listen for the traffic, weren\u2019t able to capture traffic from already running processes. This issue was mitigated in <a href=\"https:\/\/github.com\/dotnet\/runtime\/pull\/103364\">dotnet\/runtime#103364<\/a>, where the HttpClient.DefaultProxy is set to an instance of Windows proxy that listens for registry changes and reloads the proxy settings when notified.<\/p>\n<p>The following code:<\/p>\n<p>while (true)<br \/>\n{<br \/>\n    using var resp = await client.GetAsync(&#8220;https:\/\/httpbin.org\/&#8221;);<br \/>\n    Console.WriteLine(HttpClient.DefaultProxy.GetProxy(new Uri(&#8220;https:\/\/httpbin.org\/&#8221;))?.ToString() ?? &#8220;null&#8221;);<br \/>\n    await Task.Delay(1_000);<br \/>\n}<\/p>\n<p>produces output like this:<\/p>\n<p>null<br \/>\n\/\/ After Fiddler&#8217;s &#8220;System Proxy&#8221; is turned on.<br \/>\nhttp:\/\/127.0.0.1:8866\/<\/p>\n<p>Note that this change applies only for Windows as it has a unique concept of <a href=\"https:\/\/support.microsoft.com\/windows\/use-a-proxy-server-in-windows-03096c53-0554-4ffe-b6ab-8b1deee8dae1\">machine wide proxy settings<\/a>. Linux and other UNIX-based systems only allow setting up proxy via environment variables, which can\u2019t be changed during process lifetime.<\/p>\n<h3>Community contributions<\/h3>\n<p>We\u2019d like to call out community contributions.<\/p>\n<p><a href=\"https:\/\/learn.microsoft.com\/dotnet\/api\/system.threading.cancellationtoken\">CancellationToken<\/a> overloads were missing from <a href=\"https:\/\/learn.microsoft.com\/dotnet\/api\/system.net.http.httpcontent.loadintobufferasync\">HttpContent.LoadIntoBufferAsync<\/a>. This gap was resolved by an API proposal (<a href=\"https:\/\/github.com\/dotnet\/runtime\/issues\/102659\">dotnet\/runtime#102659<\/a>) from <a href=\"https:\/\/github.com\/andrewhickman-aveva\">@andrewhickman-aveva<\/a> and an implementation (<a href=\"https:\/\/github.com\/dotnet\/runtime\/pull\/103991\">dotnet\/runtime#103991<\/a>) was from <a href=\"https:\/\/github.com\/manandre\">@manandre<\/a>.<\/p>\n<p>Another change improves a units discrepancy for the MaxResponseHeadersLength property on <a href=\"https:\/\/learn.microsoft.com\/dotnet\/api\/system.net.http.socketshttphandler\">SocketsHttpHandler<\/a> and <a href=\"https:\/\/learn.microsoft.com\/dotnet\/api\/system.net.http.httpclienthandler\">HttpClientHandler<\/a> (<a href=\"https:\/\/github.com\/dotnet\/runtime\/issues\/75137\">dotnet\/runtime#75137<\/a>). All the other size and length properties are interpreted as being in bytes, however this one is interpreted as being in kilobytes. And since the actual behavior can\u2019t be changed due to backward compatibility, the problem was solved by implementing an analyzer (<a href=\"https:\/\/github.com\/dotnet\/roslyn-analyzers\/pull\/6796\">dotnet\/roslyn-analyzers#6796<\/a>). The analyzer tries to make sure the user is aware that the value provided is interpreted as kilobytes, and warns if the usage suggests otherwise.<\/p>\n<p>If the value is higher than a certain threshold, it looks like this:\n<\/p>\n<p>The analyzer was implemented by <a href=\"https:\/\/github.com\/amiru3f\">@amiru3f<\/a>.<\/p>\n<h2>QUIC<\/h2>\n<p>The prominent changes in QUIC space in .NET 9 include making the library public, more configuration options for connections and several performance improvements.<\/p>\n<h3>Public APIs<\/h3>\n<p>From this release on, System.Net.Quic isn\u2019t hidden behind <a href=\"https:\/\/learn.microsoft.com\/dotnet\/fundamentals\/apicompat\/preview-apis#requirespreviewfeaturesattribute\">PreviewFeature<\/a> anymore and all the APIs are generally available without any opt-in switches (<a href=\"https:\/\/github.com\/dotnet\/runtime\/pull\/104227\">dotnet\/runtime#104227<\/a>).<\/p>\n<h3>QUIC Connection Options<\/h3>\n<p>We expanded the configuration options for <a href=\"https:\/\/learn.microsoft.com\/dotnet\/api\/system.net.quic.quicconnection\">QuicConnection<\/a> (<a href=\"https:\/\/github.com\/dotnet\/runtime\/issues\/72984\">dotnet\/runtime#72984<\/a>). The implementation (<a href=\"https:\/\/github.com\/dotnet\/runtime\/pull\/94211\">dotnet\/runtime#94211<\/a>) added three new properties to <a href=\"https:\/\/learn.microsoft.com\/dotnet\/api\/system.net.quic.quicconnectionoptions\">QuicConnectionOptions<\/a>:<\/p>\n<p><a href=\"https:\/\/learn.microsoft.com\/dotnet\/api\/system.net.quic.quicconnectionoptions.handshaketimeout\">HandshakeTimeout<\/a> \u2013 we were already imposing a limit on how long a connection establishment can take, this property just enables the user to adjust it.<br \/>\n<a href=\"https:\/\/learn.microsoft.com\/dotnet\/api\/system.net.quic.quicconnectionoptions.keepaliveinterval\">KeepAliveInterval<\/a> \u2013 if this property is set up to a positive value, <a href=\"https:\/\/www.rfc-editor.org\/rfc\/rfc9000#name-ping-frames\">PING frames<\/a> are sent out regularly in this interval (in case no other activity is happening on the connection) which prevents the connection from being closed on <a href=\"https:\/\/www.rfc-editor.org\/rfc\/rfc9000#name-idle-timeout\">idle timeout<\/a>.<br \/>\n<a href=\"https:\/\/learn.microsoft.com\/dotnet\/api\/system.net.quic.quicconnectionoptions.initialreceivewindowsizes\">InitialReceiveWindowSizes<\/a> \u2013 a set of parameters to adjust the initial receive limits for data flow control sent in <a href=\"https:\/\/www.rfc-editor.org\/rfc\/rfc9000#transport-parameter-definitions\">transport parameters<\/a>. These data limits apply only until the dynamic flow control algorithm starts adjusting the limits based on the data reading speed. And due to <a href=\"https:\/\/github.com\/microsoft\/msquic\/blob\/main\/docs\/api\/QUIC_SETTINGS.md\">MsQuic<\/a> limitations, these parameters can only be set to values that are power of 2.<\/p>\n<p>All of these parameters are optional. Their default values are derived from <a href=\"https:\/\/github.com\/microsoft\/msquic\/blob\/main\/docs\/Settings.md\">MsQuic defaults<\/a>. The following code reports the defaults programmatically:<\/p>\n<p>var options = new QuicClientConnectionOptions();<br \/>\nConsole.WriteLine($&#8221;KeepAliveInterval = {PrettyPrintTimeStamp(options.KeepAliveInterval)}&#8221;);<br \/>\nConsole.WriteLine($&#8221;HandshakeTimeout = {PrettyPrintTimeStamp(options.HandshakeTimeout)}&#8221;);<br \/>\nConsole.WriteLine(@$&#8221;InitialReceiveWindowSizes =<br \/>\n{{<br \/>\n    Connection = {PrettyPrintInt(options.InitialReceiveWindowSizes.Connection)},<br \/>\n    LocallyInitiatedBidirectionalStream = {PrettyPrintInt(options.InitialReceiveWindowSizes.LocallyInitiatedBidirectionalStream)},<br \/>\n    RemotelyInitiatedBidirectionalStream = {PrettyPrintInt(options.InitialReceiveWindowSizes.RemotelyInitiatedBidirectionalStream)},<br \/>\n    UnidirectionalStream = {PrettyPrintInt(options.InitialReceiveWindowSizes.UnidirectionalStream)}<br \/>\n}}&#8221;);<\/p>\n<p>static string PrettyPrintTimeStamp(TimeSpan timeSpan)<br \/>\n    =&gt; timeSpan == Timeout.InfiniteTimeSpan ? &#8220;infinite&#8221; : timeSpan.ToString();<\/p>\n<p>static string PrettyPrintInt(int sizeB)<br \/>\n    =&gt; sizeB % 1024 == 0 ? $&#8221;{sizeB \/ 1024} * 1024&#8243; : sizeB.ToString();<\/p>\n<p>\/\/ Prints:<br \/>\nKeepAliveInterval = infinite<br \/>\nHandshakeTimeout = 00:00:10<br \/>\nInitialReceiveWindowSizes =<br \/>\n{<br \/>\n    Connection = 16384 * 1024,<br \/>\n    LocallyInitiatedBidirectionalStream = 64 * 1024,<br \/>\n    RemotelyInitiatedBidirectionalStream = 64 * 1024,<br \/>\n    UnidirectionalStream = 64 * 1024<br \/>\n}<\/p>\n<h3>Stream Capacity API<\/h3>\n<p>.NET 9 also introduced new APIs to support <a href=\"https:\/\/devblogs.microsoft.com\/dotnet\/dotnet-9-networking-improvements\/#connection-pooling\">multiple HTTP\/3 connections<\/a> in <a href=\"https:\/\/learn.microsoft.com\/dotnet\/api\/system.net.http.socketshttphandler.enablemultiplehttp3connections\">SocketsHttpHandler<\/a> (<a href=\"https:\/\/github.com\/dotnet\/runtime\/issues\/101534\">dotnet\/runtime#101534<\/a>). The APIs were designed with this specific usage in mind, and we don\u2019t expect them to be used apart from very niche scenarios.<\/p>\n<p>QUIC has built-in logic for managing <a href=\"https:\/\/www.rfc-editor.org\/rfc\/rfc9000#name-max_streams-frames\">stream limits<\/a> within the protocol. As a result, calling <a href=\"https:\/\/learn.microsoft.com\/dotnet\/api\/system.net.quic.quicconnection.openoutboundstreamasync\">OpenOutboundStreamAsync<\/a> on a connection gets suspended if there isn\u2019t any available stream capacity. Moreover, there isn\u2019t an efficient way to learn whether the stream limit was reached or not. All these limitations together didn\u2019t allow the HTTP\/3 layer to know when to open a new connection. So we introduced a new <a href=\"https:\/\/learn.microsoft.com\/dotnet\/api\/system.net.quic.quicconnectionoptions.streamcapacitycallback\">StreamCapacityCallback<\/a> that gets called whenever stream capacity is increased. The callback itself is registered via <a href=\"https:\/\/learn.microsoft.com\/dotnet\/api\/system.net.quic.quicconnectionoptions\">QuicConnectionOptions<\/a>. More details about the callback can be found in the <a href=\"https:\/\/learn.microsoft.com\/dotnet\/fundamentals\/networking\/quic\/quic-options#streamcapacitycallback\">documentation<\/a>.<\/p>\n<h3>Performance Improvements<\/h3>\n<p>Both performance improvements in System.Net.Quic are TLS related and both only affect connection establishing times.<\/p>\n<p>The first performance related change was to run the peer certificate validation asynchronously in .NET thread pool (<a href=\"https:\/\/github.com\/dotnet\/runtime\/pull\/98361\">dotnet\/runtime#98361<\/a>). The certificate validation can be time consuming on its own and it might even include an execution of a user callback. Moving this logic to .NET thread pool stops us blocking the MsQuic thread, of which MsQuic has a limited number, and thus enables MsQuic to process higher number of new connections at the same time.<\/p>\n<p>On top of that, we have introduced caching of MsQuic configuration (<a href=\"https:\/\/github.com\/dotnet\/runtime\/pull\/99371\">dotnet\/runtime#99371<\/a>). MsQuic configuration is a set of native structures containing connection settings from <a href=\"https:\/\/learn.microsoft.com\/dotnet\/api\/system.net.quic.quicconnectionoptions\">QuicConnectionOptions<\/a>, potentially including certificate and its intermediaries. Constructing and initializing the native structure might be very expensive since it might require serializing and deserializing all the certificate data to and from <a href=\"https:\/\/datatracker.ietf.org\/doc\/html\/rfc7292\">PKS #12<\/a> format. Moreover, the cache  allows reusing the same MsQuic configuration for different connections if their settings are identical. Specifically server scenarios with static configuration can notably profit from the caching, like the following code:<\/p>\n<p>var alpn = &#8220;test&#8221;;<br \/>\nvar serverCertificate = X509CertificateLoader.LoadCertificateFromFile(&#8220;..\/path\/to\/cert&#8221;);<\/p>\n<p>\/\/ Prepare the connection option upfront and reuse them.<br \/>\nvar serverConnectionOptions = new QuicServerConnectionOptions()<br \/>\n{<br \/>\n    DefaultStreamErrorCode = 123,<br \/>\n    DefaultCloseErrorCode = 456,<br \/>\n    ServerAuthenticationOptions = new SslServerAuthenticationOptions<br \/>\n    {<br \/>\n        ApplicationProtocols = new List&lt;SslApplicationProtocol&gt;() { alpn },<br \/>\n        \/\/ Re-using the same certificate.<br \/>\n        ServerCertificate = serverCertificate<br \/>\n    }<br \/>\n};<\/p>\n<p>\/\/ Configure the listener to return the pre-prepared options.<br \/>\nawait using var listener = await QuicListener.ListenAsync(new QuicListenerOptions()<br \/>\n{<br \/>\n    ListenEndPoint = new IPEndPoint(IPAddress.Loopback, 0),<br \/>\n    ApplicationProtocols = [ alpn ],<br \/>\n    \/\/ Callback returns the same object.<br \/>\n    \/\/ Internal cache will re-use the same native structure for every incoming connection.<br \/>\n    ConnectionOptionsCallback = (_, _, _) =&gt; ValueTask.FromResult(serverConnectionOptions)<br \/>\n});<\/p>\n<p>We also built it an escape hatch for this feature, it can be turned off with either environment variable:<\/p>\n<p>export DOTNET_SYSTEM_NET_QUIC_DISABLE_CONFIGURATION_CACHE=1<br \/>\n# run the app<\/p>\n<p>or with an <a href=\"https:\/\/learn.microsoft.com\/dotnet\/api\/system.appcontext\">AppContext<\/a> switch:<\/p>\n<p>AppContext.SetSwitch(&#8220;System.Net.Quic.DisableConfigurationCache&#8221;, true);<\/p>\n<h2>WebSockets<\/h2>\n<p>.NET 9 introduces the long-desired PING\/PONG Keep-Alive strategy to WebSockets (<a href=\"https:\/\/github.com\/dotnet\/runtime\/issues\/48729\">dotnet\/runtime#48729<\/a>).<\/p>\n<p>Prior to .NET 9, the only available Keep-Alive strategy was Unsolicited PONG. It was enough to keep the underlying TCP connection from idling out, but in a case when a remote host becomes unresponsive (for example, a remote server crashes), the only way to detect such situations was to depend on the TCP timeout.<\/p>\n<p>In this release, we complement the existing KeepAliveInterval setting with the new KeepAliveTimeout setting, so that the Keep-Alive strategy is selected as follows:<\/p>\n<p>Keep-Alive is <strong>OFF<\/strong>, if<\/p>\n<p>KeepAliveInterval is TimeSpan.Zero or Timeout.InfiniteTimeSpan<\/p>\n<p><strong>Unsolicited PONG<\/strong>, if<\/p>\n<p>KeepAliveInterval is a positive finite TimeSpan, -AND-<br \/>\nKeepAliveTimeout is TimeSpan.Zero or Timeout.InfiniteTimeSpan<\/p>\n<p><strong>PING\/PONG<\/strong>, if<\/p>\n<p>KeepAliveInterval is a positive finite TimeSpan, -AND-<br \/>\nKeepAliveTimeout is a positive finite TimeSpan<\/p>\n<p>By default, the preexisting Keep-Alive behavior is maintained: KeepAliveTimeout default value is Timeout.InfiniteTimeSpan, so Unsolicited PONG remains as the default strategy.<\/p>\n<p>The following example illustrates how to enable the PING\/PONG strategy for a ClientWebSocket:<\/p>\n<p>var cws = new ClientWebSocket();<br \/>\ncws.Options.KeepAliveInterval = TimeSpan.FromSeconds(10);<br \/>\ncws.Options.KeepAliveTimeout = TimeSpan.FromSeconds(10);<br \/>\nawait cws.ConnectAsync(uri, cts.Token);<\/p>\n<p>\/\/ NOTE: There should be an outstanding read at all times to<br \/>\n\/\/ ensure incoming PONGs are promptly processed<br \/>\nvar result = await cws.ReceiveAsync(buffer, cts.Token);<\/p>\n<p>If no PONG response received after KeepAliveTimeout elapsed, the remote endpoint is deemed unresponsive, and the WebSocket connection is automatically aborted. It also unblocks the outstanding ReceiveAsync with an OperationCanceledException.<\/p>\n<p>To learn more about the feature, you can check out the <a href=\"https:\/\/learn.microsoft.com\/dotnet\/core\/extensions\/httpclient-factory-keyed-di\">dedicated conceptual docs<\/a>.<\/p>\n<h2>.NET Framework Compatibility<\/h2>\n<p>One of the biggest hurdles in the networking space when migrating projects from .NET Framework to .NET Core is the difference between the HTTP stacks. In .NET Framework, the main class to handle HTTP requests is <a href=\"https:\/\/learn.microsoft.com\/dotnet\/api\/system.net.httpwebrequest\">HttpWebRequest<\/a> which uses global <a href=\"https:\/\/learn.microsoft.com\/dotnet\/api\/system.net.servicepointmanager\">ServicePointManager<\/a> and individual <a href=\"https:\/\/learn.microsoft.com\/dotnet\/api\/system.net.servicepoint\">ServicePoints<\/a> to handle connection pooling. Whereas in .NET Core, <a href=\"https:\/\/learn.microsoft.com\/dotnet\/api\/system.net.http.httpclient\">HttpClient<\/a> is the recommended way to access HTTP resources. On top of that, all the classes from .NET Framework are present in .NET, but they\u2019re either obsolete, or missing implementation, or are just not maintained at all. As a result, we often see mistakes like using ServicePointManager to configure the connections while using HttpClient to access the resources.<\/p>\n<p>The recommendation always was to fully migrate to HttpClient, but sometimes it\u2019s not possible. Migrating projects from .NET Framework to .NET Core can be difficult on its own, let alone rewriting all the networking code. Expecting customers to do all this work in one step proved to be unrealistic and is one of the reasons why customers might be reluctant to migrate. To mitigate these pain points, we filled in some missing implementations of the legacy classes and created a comprehensive guide to help with the migration.<\/p>\n<p>The first part is expansion of supported ServicePointManager and ServicePoint properties that were missing implementation in .NET Core up until this release (<a href=\"https:\/\/github.com\/dotnet\/runtime\/pull\/94664\">dotnet\/runtime#94664<\/a> and <a href=\"https:\/\/github.com\/dotnet\/runtime\/pull\/97537\">dotnet\/runtime#97537<\/a>). With these changes, they\u2019re now taken into account when using <a href=\"https:\/\/learn.microsoft.com\/dotnet\/api\/system.net.httpwebrequest\">HttpWebRequest<\/a>.<\/p>\n<p>For <a href=\"https:\/\/learn.microsoft.com\/dotnet\/api\/system.net.httpwebrequest\">HttpWebRequest<\/a>, we implemented full support of <a href=\"https:\/\/learn.microsoft.com\/dotnet\/api\/system.net.httpwebrequest.allowwritestreambuffering\">AllowWriteStreamBuffering<\/a> in <a href=\"https:\/\/github.com\/dotnet\/runtime\/pull\/95001\">dotnet\/runtime#95001<\/a>. And also added missing support for <a href=\"https:\/\/learn.microsoft.com\/dotnet\/api\/system.net.webrequest.impersonationlevel\">ImpersonationLevel<\/a> in <a href=\"https:\/\/github.com\/dotnet\/runtime\/pull\/102038\">dotnet\/runtime#102038<\/a>.<\/p>\n<p>On top of these changes, we also obsoleted a few legacy classes to prevent further confusion:<\/p>\n<p><a href=\"https:\/\/learn.microsoft.com\/dotnet\/api\/system.net.servicepointmanager\">ServicePointManager<\/a> in <a href=\"https:\/\/github.com\/dotnet\/runtime\/pull\/103456\">dotnet\/runtime#103456<\/a>. Its settings have no effect on HttpClient and SslStream while it might be misused in good faith for exactly that purpose.<br \/>\n<a href=\"https:\/\/learn.microsoft.com\/dotnet\/api\/system.net.authenticationmanager\">AuthenticationManager<\/a> in <a href=\"https:\/\/github.com\/dotnet\/runtime\/pull\/93171\">dotnet\/runtime#93171<\/a>, done by community contributor <a href=\"https:\/\/github.com\/deeprobin\">@deeprobin<\/a>. It\u2019s either missing implementation or the methods throw PlatformNotSupportedException.<\/p>\n<p>Lastly, we put up together a guide for migration from HttpWebRequest to HttpClient in <a href=\"https:\/\/learn.microsoft.com\/dotnet\/fundamentals\/networking\/http\/httpclient-migrate-from-httpwebrequest\"><em>HttpWebRequest to HttpClient migration guide<\/em><\/a>. It includes comprehensive lists of mappings between individual properties and methods, e.g., <a href=\"https:\/\/learn.microsoft.com\/dotnet\/fundamentals\/networking\/http\/httpclient-migrate-from-httpwebrequest#migrate-servicepointmanager-usage\"><em>Migrate ServicePoint(Manager) usage<\/em><\/a> and many examples for trivial and not so trivial scenarios, e.g., <a href=\"https:\/\/learn.microsoft.com\/dotnet\/fundamentals\/networking\/http\/httpclient-migrate-from-httpwebrequest#example-enable-dns-round-robin\"><em>Example: Enable DNS round robin<\/em><\/a>.<\/p>\n<h2>Diagnostics<\/h2>\n<p>In this release, diagnostics improvements focus on enhancing privacy protection and advancing distributed tracing capabilities.<\/p>\n<h3>Uri Query Redaction in HttpClientFactory Logs<\/h3>\n<p>Starting with version 9.0.0 of <a href=\"https:\/\/www.nuget.org\/packages\/Microsoft.Extensions.Http\">Microsoft.Extensions.Http<\/a>, the default logging logic of HttpClientFactory prioritizes <em>protecting privacy<\/em>. In older versions, it emits the full request URI in the RequestStart and RequestPipelineStart events. In cases where some components of the URI contain sensitive information, this can lead to privacy incidents by leaking such data into logs.<\/p>\n<p>Version 8.0.0 introduced the ability to secure HttpClientFactory usage by <a href=\"https:\/\/devblogs.microsoft.com\/dotnet\/dotnet-8-networking-improvements\/#modify-httpclient-logging\">customizing logging<\/a>. However, this doesn\u2019t change the fact that the default behavior might be risky for unaware users.<\/p>\n<p>In the majority of the problematic cases, sensitive information resides in the query component. Therefore, a <a href=\"https:\/\/learn.microsoft.com\/dotnet\/core\/compatibility\/networking\/9.0\/query-redaction-logs\">breaking change<\/a> was introduced in 9.0.0, removing the entire query string from HttpClientFactory logs by default. A global <a href=\"https:\/\/learn.microsoft.com\/dotnet\/core\/compatibility\/networking\/9.0\/query-redaction-logs#recommended-action\">opt-out switch<\/a> is available for services\/apps where it\u2019s safe to log the full URI.<\/p>\n<p>For consistency and maximum safety, a <a href=\"https:\/\/learn.microsoft.com\/dotnet\/core\/compatibility\/networking\/9.0\/query-redaction-events\">similar change<\/a> was implemented for <a href=\"https:\/\/learn.microsoft.com\/dotnet\/api\/system.diagnostics.tracing.eventsource\">EventSource<\/a> events in System.Net.Http.<\/p>\n<p>We recognize that this solution might not suit everyone. Ideally, there should be a fine-grained URI filtering mechanism, allowing users to retain non-sensitive query entries or filter other URI components (e.g., parts of the path). We plan to explore such a feature for future versions (<a href=\"https:\/\/github.com\/dotnet\/runtime\/issues\/110018\">dotnet\/runtime#110018<\/a>).<\/p>\n<h3>Distributed Tracing Improvements<\/h3>\n<p><a href=\"https:\/\/learn.microsoft.com\/dotnet\/core\/diagnostics\/distributed-tracing\">Distributed tracing<\/a> is a diagnostic technique for tracking the path of a specific transaction across multiple processes and machines, helping identify bottlenecks and failures. This technique models the transaction as a hierarchical tree of <a href=\"https:\/\/learn.microsoft.com\/dotnet\/core\/diagnostics\/distributed-tracing-concepts#traces-and-activities\">Activities<\/a>, also referred to as <a href=\"https:\/\/opentelemetry.io\/docs\/concepts\/signals\/traces\/#spans\">spans<\/a> in OpenTelemetry terminology.<\/p>\n<p>HttpClientHandler and SocketsHttpHandler are instrumented to start an Activity for each request and propagate the <a href=\"https:\/\/www.w3.org\/TR\/trace-context\/\">trace context<\/a> via standard W3C headers when tracing is enabled.<\/p>\n<p>Before .NET 9, users needed the OpenTelemetry .NET SDK to produce useful OpenTelemetry-compliant traces. This SDK was required not just for collection and export but also to <a href=\"https:\/\/github.com\/open-telemetry\/opentelemetry-dotnet-contrib\/blob\/85afc5dd1bd7deef9b8949f36a14cd3fd1aacecb\/src\/OpenTelemetry.Instrumentation.Http\/README.md\">extend the instrumentation<\/a>, as the built-in logic didn\u2019t populate the Activity with request data.<\/p>\n<p>Starting with .NET 9, the instrumentation dependency (OpenTelemetry.Instrumentation.Http) can be omitted unless advanced features like enrichment are required. In <a href=\"https:\/\/github.com\/dotnet\/runtime\/pull\/104251\">dotnet\/runtime#104251<\/a>, we extended the built-in tracing to ensure that the shape of the Activity is <a href=\"https:\/\/opentelemetry.io\/docs\/specs\/semconv\/http\/http-spans\/#http-client\">OTel-compliant<\/a>, with the name, status, and most required tags populated according to the standard.<\/p>\n<h4>Experimental Connection Tracing<\/h4>\n<p>When investigating bottlenecks, you might want to zoom into specific HTTP requests to identify where most of the time is spent. Is it during a connection establishment or the content download? If there are connection issues, it\u2019s helpful to determine whether the problem lies with DNS lookups, TCP connection establishment, or the TLS handshake.<\/p>\n<p>.NET 9 has introduced several new spans to represent activities around connection establishment in SocketsHttpHandler. The most significant one HTTP connection setup span which breaks down to three child spans for DNS, TCP, and TLS activities.<\/p>\n<p>Because connection setup isn\u2019t tied to a particular request in SocketsHttpHandler connection pool, the connection setup span can\u2019t be modeled as a child span of the HTTP client request span. Instead, the relationship between requests and connections is being represented using <a href=\"https:\/\/opentelemetry.io\/docs\/concepts\/signals\/traces\/#span-links\">Span Links<\/a>, also known as Activity Links.<\/p>\n\n<div class=\"alert alert-primary\">\n<p class=\"alert-divider\"><strong>Note<\/strong><\/p>\n<p>The new spans are produced by various ActivitySources matching the wildcard Experimental.System.Net.*. These spans are experimental because monitoring tools like <a href=\"https:\/\/learn.microsoft.com\/azure\/azure-monitor\/app\/app-insights-overview\">Azure Monitor Application Insights<\/a> have difficulty visualizing the resulting traces effectively due to the numerous connection_setup \u2192 request backlinks. To improve the user experience in monitoring tools, further work is needed. It involves collaboration between the .NET team, OTel, and tool authors, and may result in breaking changes in the design of the new spans.<\/p><\/div>\n<p>The simplest way to set up and try connection trace collection is by using <a href=\"https:\/\/learn.microsoft.com\/dotnet\/aspire\/get-started\/aspire-overview\">.NET Aspire<\/a>. Using <a href=\"https:\/\/learn.microsoft.com\/dotnet\/aspire\/fundamentals\/dashboard\/overview\">Aspire Dashboards<\/a> it\u2019s possible to expand the connection_setup activity and see a breakdown of the connection initialization.<\/p>\n\n<p>If you think the .NET 9 tracing additions might bring you valuable diagnostic insights, and you want to get some hands-on experience, don\u2019t hesitate to read our full article about <a href=\"https:\/\/learn.microsoft.com\/dotnet\/fundamentals\/networking\/telemetry\/tracing\">Distributed tracing in System.Net libraries<\/a>.<\/p>\n<h2>HttpClientFactory<\/h2>\n<p>For HttpClientFactory, we\u2019re introducing the Keyed DI support, offering a new convenient consumption pattern, and changing a default Primary Handler to mitigate a common erroneous usecase.<\/p>\n<h3>Keyed DI Support<\/h3>\n<p>In the previous release, <a href=\"https:\/\/learn.microsoft.com\/dotnet\/core\/extensions\/dependency-injection?source=recommendations#keyed-services\">Keyed Services<\/a> were introduced to <a href=\"https:\/\/www.nuget.org\/packages\/Microsoft.Extensions.DependencyInjection\">Microsoft.Extensions.DependencyInjection<\/a> packages. Keyed DI allows you to specify the keys while registering multiple implementations of a single service type\u2014and to later retrieve a specific implementation using the respective key.<\/p>\n<p>HttpClientFactory and named HttpClient instances, unsurprisingly, align well with the Keyed Services idea. Among other things, HttpClientFactory was a way to overcome this long-missing DI feature. But it required you to obtain, store and query the IHttpClientFactory instance\u2014instead of simply injecting a configured HttpClient\u2014which might be inconvenient. While Typed clients attempted to simplify that part, it came with a catch: Typed clients are easy to <a href=\"https:\/\/learn.microsoft.com\/dotnet\/core\/extensions\/httpclient-factory-troubleshooting#typed-client-has-the-wrong-httpclient-injected\">misconfigure<\/a> and <a href=\"https:\/\/learn.microsoft.com\/dotnet\/core\/extensions\/httpclient-factory#avoid-typed-clients-in-singleton-services\">misuse<\/a> (and the supporting infra can also be a tangible overhead in <a href=\"https:\/\/github.com\/dotnet\/runtime\/issues\/66863\">certain scenarios<\/a>). As a result, the user experience in both cases was far from ideal.<\/p>\n<p>This changes as <a href=\"https:\/\/www.nuget.org\/packages\/Microsoft.Extensions.DependencyInjection\">Microsoft.Extensions.DependencyInjection<\/a> 9.0.0 and <a href=\"https:\/\/www.nuget.org\/packages\/Microsoft.Extensions.Http\">Microsoft.Extensions.Http<\/a> 9.0.0 packages bring the Keyed DI support into HttpClientFactory (<a href=\"https:\/\/github.com\/dotnet\/runtime\/issues\/89755\">dotnet\/runtime#89755<\/a>). Now you can have the best of both worlds: you can pair the convenient, highly configurable HttpClient registrations with the straightforward injection of the specific configured HttpClient instances.<\/p>\n<p>As of 9.0.0, you need to <em>opt in<\/em> to the feature by calling the <a href=\"https:\/\/learn.microsoft.com\/dotnet\/api\/microsoft.extensions.dependencyinjection.httpclientbuilderextensions.addaskeyed\">AddAsKeyed()<\/a> extension method. It registers a Named HttpClient as a Keyed service for the key equal to the client\u2019s name\u2014and enables you to use the Keyed Services APIs (e.g., <a href=\"https:\/\/learn.microsoft.com\/dotnet\/api\/microsoft.extensions.dependencyinjection.fromkeyedservicesattribute\">[FromKeyedServices(&#8230;)]<\/a>) to obtain the required HttpClients.<\/p>\n<p>The following code demonstrates the integration between HttpClientFactory, Keyed DI and ASP.NET Core 9.0 Minimal APIs:<\/p>\n<p>var builder = WebApplication.CreateBuilder(args);<\/p>\n<p>builder.Services.AddHttpClient(&#8220;github&#8221;, c =&gt;<br \/>\n    {<br \/>\n        c.BaseAddress = new Uri(&#8220;https:\/\/api.github.com\/&#8221;);<br \/>\n        c.DefaultRequestHeaders.Add(&#8220;Accept&#8221;, &#8220;application\/vnd.github.v3+json&#8221;);<br \/>\n        c.DefaultRequestHeaders.Add(&#8220;User-Agent&#8221;, &#8220;dotnet&#8221;);<br \/>\n    })<br \/>\n    .AddAsKeyed(); \/\/ Add HttpClient as a Keyed Scoped service for key=&#8221;github&#8221;<\/p>\n<p>var app = builder.Build();<\/p>\n<p>\/\/ Directly inject the Keyed HttpClient by its name<br \/>\napp.MapGet(&#8220;\/&#8221;, ([FromKeyedServices(&#8220;github&#8221;)] HttpClient httpClient) =&gt;<br \/>\n    httpClient.GetFromJsonAsync&lt;Repo&gt;(&#8220;\/repos\/dotnet\/runtime&#8221;));<\/p>\n<p>app.Run();<\/p>\n<p>record Repo(string Name, string Url);<\/p>\n<p>Endpoint response:<\/p>\n<p>&gt; ~  curl http:\/\/localhost:5000\/<br \/>\n{&#8220;name&#8221;:&#8221;runtime&#8221;,&#8221;url&#8221;:&#8221;https:\/\/api.github.com\/repos\/dotnet\/runtime&#8221;}<\/p>\n<p>By default, AddAsKeyed() registers HttpClient as a Keyed <em>Scoped<\/em> service. The Scoped lifetime can help catching cases of captive dependencies:<\/p>\n<p>services.AddHttpClient(&#8220;scoped&#8221;).AddAsKeyed();<br \/>\nservices.AddSingleton&lt;CapturingSingleton&gt;();<\/p>\n<p>\/\/ Throws: Cannot resolve scoped service &#8216;System.Net.Http.HttpClient&#8217; from root provider.<br \/>\nrootProvider.GetRequiredKeyedService&lt;HttpClient&gt;(&#8220;scoped&#8221;);<\/p>\n<p>using var scope = provider.CreateScope();<br \/>\nscope.ServiceProvider.GetRequiredKeyedService&lt;HttpClient&gt;(&#8220;scoped&#8221;); \/\/ OK<\/p>\n<p>\/\/ Throws: Cannot consume scoped service &#8216;System.Net.Http.HttpClient&#8217; from singleton &#8216;CapturingSingleton&#8217;.<br \/>\npublic class CapturingSingleton([FromKeyedServices(&#8220;scoped&#8221;)] HttpClient httpClient)<br \/>\n\/\/{ &#8230;<\/p>\n<p>You can also explicitly specify the lifetime by passing the ServiceLifetime parameter to the AddAsKeyed() method:<\/p>\n<p>services.AddHttpClient(&#8220;explicit-scoped&#8221;)<br \/>\n    .AddAsKeyed(ServiceLifetime.Scoped);<\/p>\n<p>services.AddHttpClient(&#8220;singleton&#8221;)<br \/>\n    .AddAsKeyed(ServiceLifetime.Singleton);<\/p>\n<p>You don\u2019t have to call AddAsKeyed for every single client\u2014you can easily opt in \u201cglobally\u201d (for any client name) via ConfigureHttpClientDefaults. From Keyed Services perspective, it results in the <a href=\"https:\/\/learn.microsoft.com\/dotnet\/api\/microsoft.extensions.dependencyinjection.keyedservice.anykey\">KeyedService.AnyKey<\/a> registration.<\/p>\n<p>services.ConfigureHttpClientDefaults(b =&gt; b.AddAsKeyed());<\/p>\n<p>services.AddHttpClient(&#8220;foo&#8221;, \/* &#8230; *\/);<br \/>\nservices.AddHttpClient(&#8220;bar&#8221;, \/* &#8230; *\/);<\/p>\n<p>public class MyController(<br \/>\n    [FromKeyedServices(&#8220;foo&#8221;)] HttpClient foo,<br \/>\n    [FromKeyedServices(&#8220;bar&#8221;)] HttpClient bar)<br \/>\n\/\/{ &#8230;<\/p>\n<p>Even though the \u201cglobal\u201d opt-in is a one-liner, it\u2019s unfortunate that the feature still requires it, instead of just working \u201cout of the box\u201d. For full context and reasoning on that decision, see <a href=\"https:\/\/github.com\/dotnet\/runtime\/issues\/89755\">dotnet\/runtime#89755<\/a> and <a href=\"https:\/\/github.com\/dotnet\/runtime\/pull\/104943\">dotnet\/runtime#104943<\/a>.<\/p>\n<p>You can explicitly opt out from Keyed DI for HttpClients by calling <a href=\"https:\/\/learn.microsoft.com\/dotnet\/api\/microsoft.extensions.dependencyinjection.httpclientbuilderextensions.removeaskeyed\">RemoveAsKeyed()<\/a> (for example, per specific client, in case of the \u201cglobal\u201d opt-in):<\/p>\n<p>services.ConfigureHttpClientDefaults(b =&gt; b.AddAsKeyed());      \/\/ opt IN by default<br \/>\nservices.AddHttpClient(&#8220;keyed&#8221;, \/* &#8230; *\/);<br \/>\nservices.AddHttpClient(&#8220;not-keyed&#8221;, \/* &#8230; *\/).RemoveAsKeyed(); \/\/ opt OUT per name<\/p>\n<p>provider.GetRequiredKeyedService&lt;HttpClient&gt;(&#8220;keyed&#8221;);     \/\/ OK<br \/>\nprovider.GetRequiredKeyedService&lt;HttpClient&gt;(&#8220;not-keyed&#8221;); \/\/ Throws: No service for type &#8216;System.Net.Http.HttpClient&#8217; has been registered.<br \/>\nprovider.GetRequiredKeyedService&lt;HttpClient&gt;(&#8220;unknown&#8221;);   \/\/ OK (unconfigured instance)<\/p>\n<p>If called together, or any of them more than once, AddAsKeyed() and RemoveAsKeyed() generally follow the rules of HttpClientFactory configs and DI registrations:<\/p>\n<p>If used within the same name, the last setting wins: the lifetime from the last AddAsKeyed() is used to create the Keyed registration (unless RemoveAsKeyed() was called last, in which case the name is excluded).<br \/>\nIf used only within ConfigureHttpClientDefaults, the last setting wins.<br \/>\nIf both ConfigureHttpClientDefaults and a specific client name were used, all defaults are considered to \u201chappen\u201d before all per-name settings for this client. Thus, the defaults can be disregarded, and the last of the per-name ones wins.<\/p>\n<p>You can learn more about the feature in the <a href=\"https:\/\/learn.microsoft.com\/dotnet\/fundamentals\/networking\/websockets#keep-alive-strategies\">dedicated conceptual docs<\/a>.<\/p>\n<h3>Default Primary Handler Change<\/h3>\n<p>One of the most common problems HttpClientFactory users run into is when a Named or a Typed client erroneously gets <a href=\"https:\/\/learn.microsoft.com\/dotnet\/core\/extensions\/httpclient-factory#avoid-typed-clients-in-singleton-services\">captured in a Singleton service<\/a>, or, in general, stored somewhere for a period of time that\u2019s longer than the specified HandlerLifetime. Because HttpClientFactory can\u2019t rotate such handlers, they might end up <a href=\"https:\/\/learn.microsoft.com\/dotnet\/core\/extensions\/httpclient-factory-troubleshooting#httpclient-doesnt-respect-dns-changes\">not respecting DNS changes<\/a>. It is, unfortunately, easy and seemingly \u201cintuitive\u201d to inject a Typed client into a singleton, but hard to have any kind of check\/analyzer to make sure HttpClient isn\u2019t captured when it wasn\u2019t supposed to. It might be even harder to troubleshoot the resulting issues.<\/p>\n<p>On the other hand, the problem can be <a href=\"https:\/\/learn.microsoft.com\/dotnet\/core\/extensions\/httpclient-factory#using-ihttpclientfactory-together-with-socketshttphandler\">mitigated<\/a> by using SocketsHttpHandler, which can control PooledConnectionLifetime. Similarly to HandlerLifetime, it allows regularly recreating connections to pick up the DNS changes, but on a lower level. A client with PooledConnectionLifetime set up can be safely used as a Singleton.<\/p>\n<p>Therefore, to minimize the potential impact of the erroneous usage patterns, .NET 9 makes the default Primary handler a SocketsHttpHandler (on platforms that support it; other platforms, e.g. .NET Framework, continue to use HttpClientHandler). And most importantly, SocketsHttpHandler also has the PooledConnectionLifetime property <em>preset<\/em> to match the HandlerLifetime value (it reflects the latest value, if you configured HandlerLifetime one or more times).<\/p>\n<p>The change only affects cases when the client was <em>not<\/em> configured to have a custom Primary handler (via e.g. ConfigurePrimaryHttpMessageHandler&lt;T&gt;()).<\/p>\n<p>While the default Primary handler is an implementation detail, as it was never specified in the docs, it\u2019s still considered a <a href=\"https:\/\/learn.microsoft.com\/dotnet\/core\/compatibility\/networking\/9.0\/default-handler\">breaking change<\/a>. There could be cases in which you wanted to use the specific type, for example, casting the Primary handler to HttpClientHandler to set properties like ClientCertificates, UseCookies, UseProxy, etc. If you need to use such properties, it\u2019s suggested to check for both HttpClientHandler and SocketsHttpHandler in the configuration action:<\/p>\n<p>services.AddHttpClient(&#8220;test&#8221;)<br \/>\n    .ConfigurePrimaryHttpMessageHandler((h, _) =&gt;<br \/>\n    {<br \/>\n        if (h is HttpClientHandler hch)<br \/>\n        {<br \/>\n            hch.UseCookies = false;<br \/>\n        }<\/p>\n<p>        if (h is SocketsHttpHandler shh)<br \/>\n        {<br \/>\n            shh.UseCookies = false;<br \/>\n        }<br \/>\n    });<\/p>\n<p>Alternatively, you can explicitly specify a Primary handler for each of your clients:<\/p>\n<p>services.AddHttpClient(&#8220;test&#8221;)<br \/>\n    .ConfigurePrimaryHttpMessageHandler(() =&gt; new HttpClientHandler() { UseCookies = false });<\/p>\n<p>Or, configure the default Primary handler for all clients using ConfigureHttpClientDefaults:<\/p>\n<p>services.ConfigureHttpClientDefaults(b =&gt;<br \/>\n    b.ConfigurePrimaryHttpMessageHandler(() =&gt; new HttpClientHandler() { UseCookies = false }));<\/p>\n<h2>Security<\/h2>\n<p>In System.Net.Security, we\u2019re introducing the highly sought support for SSLKEYLOGFILE, more scenarios supporting TLS resume, and new additions in negotiate APIs.<\/p>\n<h3>SSLKEYLOGFILE Support<\/h3>\n<p>The most upvoted issue in the security space was to support logging of pre-master secret (<a href=\"https:\/\/github.com\/dotnet\/runtime\/issues\/37915\">dotnet\/runtime#37915<\/a>). The logged secret can be used by packet capturing tool Wireshark to decrypt the traffic. It\u2019s a useful diagnostics tool when investigating networking issues. Moreover, the same functionality is provided by browsers like Firefox (via <a href=\"https:\/\/udn.realityripple.com\/docs\/Mozilla\/Projects\/NSS\/Key_Log_Format\">NSS<\/a>) and Chrome and command line HTTP tools like <a href=\"https:\/\/everything.curl.dev\/usingcurl\/tls\/sslkeylogfile\">cURL<\/a>.<br \/>\nWe have implemented this feature for both <a href=\"https:\/\/learn.microsoft.com\/dotnet\/api\/system.net.security.sslstream\">SslStream<\/a> and <a href=\"https:\/\/learn.microsoft.com\/dotnet\/api\/system.net.quic.quicconnection\">QuicConnection<\/a>. For the former, the functionality is limited to the platforms on which we use OpenSSL as a cryptographic library. In the terms of the officially released .NET runtime, it means only on Linux operating systems. For the latter, it\u2019s supported everywhere, regardless of the cryptographic library. That\u2019s because TLS is part of the QUIC protocol (<a href=\"https:\/\/www.rfc-editor.org\/rfc\/rfc9001.html\">RFC 9001<\/a>) so the user-space MsQuic has access to all the secrets and so does .NET. The limitation of SslStream on Windows comes from SChannel using a separate, privileged process for TLS which won\u2019t allow exporting secrets due to security concerns (<a href=\"https:\/\/github.com\/dotnet\/runtime\/issues\/94843\">dotnet\/runtime#94843<\/a>).<\/p>\n<p>This feature exposes security secrets and relying solely on an environmental variable could unintentionally leak them. For that reason, we\u2019ve decided to introduce an additional AppContext switch necessary to enable the feature (<a href=\"https:\/\/github.com\/dotnet\/runtime\/pull\/100665\">dotnet\/runtime#100665<\/a>). It requires the user to prove the ownership of the application by either setting it programmatically in the code:<\/p>\n<p>AppContext.SetSwitch(&#8220;System.Net.EnableSslKeyLogging&#8221;, true);<\/p>\n<p>or by changing the {appname}.runtimeconfig.json next to the application:<\/p>\n<p>{<br \/>\n  &#8220;runtimeOptions&#8221;: {<br \/>\n    &#8220;configProperties&#8221;: {<br \/>\n      &#8220;System.Net.EnableSslKeyLogging&#8221;: true<br \/>\n    }<br \/>\n  }<br \/>\n}<\/p>\n<p>The last thing is to set up an environmental variable SSLKEYLOGFILE and run the application:<\/p>\n<p>export SSLKEYLOGFILE=~\/keylogfile<\/p>\n<p>.\/&lt;appname&gt;<\/p>\n<p>At this point, ~\/keylogfile will contain pre-master secrets that can be used by Wireshark to decrypt the traffic. For more information, see <a href=\"https:\/\/wiki.wireshark.org\/TLS#using-the-pre-master-secret\">TLS Using the (Pre)-Master-Secret<\/a> documentation.<\/p>\n<h3>TLS Resume with Client Certificate<\/h3>\n<p>TLS resume enables reusing previously stored TLS data to re-establish connection to previously connected server. It can save round trips during the handshake as well as CPU processing. This feature is a native part of Windows SChannel, therefore it\u2019s implicitly used by .NET on Windows platforms. However, on Linux platforms where we use OpenSSL as a cryptographic library, enabling caching and reusing TLS data is more involved. We first introduced the support in .NET 7 (see <a href=\"https:\/\/devblogs.microsoft.com\/dotnet\/dotnet-7-networking-improvements\/#performance\">TLS Resume<\/a>). It has its own limitations that in general are not present on Windows. One such limitation was that it was not supported for sessions using mutual authentication by providing a client certificate (<a href=\"https:\/\/github.com\/dotnet\/runtime\/issues\/94561\">dotnet\/runtime#94561<\/a>). It has been fixed in .NET 9 (<a href=\"https:\/\/github.com\/dotnet\/runtime\/pull\/102656\">dotnet\/runtime#102656<\/a>) and works if one these properties is set as described:<\/p>\n<p><a href=\"https:\/\/learn.microsoft.com\/dotnet\/api\/system.net.security.sslclientauthenticationoptions.clientcertificatecontext\">ClientCertificateContext<\/a><br \/>\n<a href=\"https:\/\/learn.microsoft.com\/dotnet\/api\/system.net.security.sslclientauthenticationoptions.localcertificateselectioncallback\">LocalCertificateSelectionCallback<\/a> returns non-null certificate on the first call<br \/>\n<a href=\"https:\/\/learn.microsoft.com\/dotnet\/api\/system.net.security.sslclientauthenticationoptions.clientcertificates\">ClientCertificates<\/a> collection has at least one certificate with private key<\/p>\n<h3>Negotiate API Integrity Checks<\/h3>\n<p>In .NET 7, we added <a href=\"https:\/\/learn.microsoft.com\/dotnet\/api\/system.net.security.negotiateauthentication\">NegotiateAuthentication<\/a> APIs, see <a href=\"https:\/\/devblogs.microsoft.com\/dotnet\/dotnet-7-networking-improvements\/#negotiate-api\">Negotiate API<\/a>. The original implementation\u2019s goal was to remove access via reflection to the internals of NTAuthentication. However, that proposal was missing functions to generate and verify message integrity codes from <a href=\"https:\/\/datatracker.ietf.org\/doc\/html\/rfc2743\">RFC 2743<\/a>. They\u2019re usually implemented as cryptographic signing operation with a negotiated key. The API was proposed in <a href=\"https:\/\/github.com\/dotnet\/runtime\/issues\/86950\">dotnet\/runtime#86950<\/a> and implemented in <a href=\"https:\/\/github.com\/dotnet\/runtime\/pull\/96712\">dotnet\/runtime#96712<\/a> and as the original change, all the work from the API proposal to the implementation was done by a community contributor <a href=\"https:\/\/github.com\/filipnavara\">filipnavara<\/a>.<\/p>\n<h2>Networking Primitives<\/h2>\n<p>This section encompasses changes in System.Net namespace. We\u2019re introducing new support for server-side events and some small additions in APIs, for example new MIME types.<\/p>\n<h3>Server-Sent Events Parser<\/h3>\n<p>Server-sent events is a technology that allows servers to push data updates on clients via an HTTP connection. It is defined in <a href=\"https:\/\/html.spec.whatwg.org\/multipage\/server-sent-events.html\">living HTML standard<\/a>. It uses text\/event-stream MIME type and it\u2019s always decoded as UTF-8. The advantage of the server-push approach over client-pull is that it can make better use of network resources and also save battery life of mobile devices.<\/p>\n<p>In this release, we\u2019re introducing an OOB package System.Net.ServerSentEvents. It\u2019s available as a .NET Standard 2.0 <a href=\"https:\/\/www.nuget.org\/packages\/System.Net.ServerSentEvents\">NuGet package<\/a>. The package offers a parser for server-sent event stream, following the <a href=\"https:\/\/html.spec.whatwg.org\/multipage\/server-sent-events.html#parsing-an-event-stream\">specification<\/a>. The protocol is stream based, with individual items separated by an empty line.<\/p>\n<p>Each item has two fields:<\/p>\n<p>type \u2013 default type is message<br \/>\ndata \u2013 data itself<\/p>\n<p>On top of that, there are two other optional fields that progressively update properties of the stream:<\/p>\n<p>id \u2013 determines the last event id that is sent in <a href=\"https:\/\/html.spec.whatwg.org\/multipage\/server-sent-events.html#the-last-event-id-header\">Last-Event-Id header<\/a> in case the connection needs to be reconnected<br \/>\nretry \u2013 number of milliseconds to wait between reconnection attempts<\/p>\n<p>The library APIs were proposed in <a href=\"https:\/\/github.com\/dotnet\/runtime\/issues\/98105\">dotnet\/runtime#98105<\/a> and contain type definitions for the parser and the items:<\/p>\n<p><a href=\"https:\/\/learn.microsoft.com\/dotnet\/api\/system.net.serversentevents.sseparser\">SseParser<\/a> \u2013 static class to create the actual parser from the stream, allowing the user to optionally provide a parsing delegate for the item data<br \/>\n<a href=\"https:\/\/learn.microsoft.com\/dotnet\/api\/system.net.serversentevents.sseparser-1\">SseParser&lt;T&gt;<\/a> \u2013 parser itself, offers methods to enumerate (synchronously or asynchronously) the stream and return the parsed items<br \/>\n<a href=\"https:\/\/learn.microsoft.com\/dotnet\/api\/system.net.serversentevents.sseitem-1\">SseItem&lt;T&gt;<\/a> \u2013 struct holding parsed item data<\/p>\n<p>Then the parser can be used like this, for example:<\/p>\n<p>using HttpClient client = new HttpClient();<br \/>\nusing Stream stream = await client.GetStreamAsync(&#8220;https:\/\/server\/sse&#8221;);<\/p>\n<p>var parser = SseParser.Create(stream, (type, data) =&gt;<br \/>\n{<br \/>\n    var str = Encoding.UTF8.GetString(data);<br \/>\n    return Int32.Parse(str);<br \/>\n});<br \/>\nawait foreach (var item in parser.EnumerateAsync())<br \/>\n{<br \/>\n    Console.WriteLine($&#8221;{item.EventType}: {item.Data} [{parser.LastEventId};{parser.ReconnectionInterval}]&#8221;);<br \/>\n}<\/p>\n<p>And for the following input:<\/p>\n<p>: stream of integers<\/p>\n<p>data: 123<br \/>\nid: 1<br \/>\nretry: 1000<\/p>\n<p>data: 456<br \/>\nid: 2<\/p>\n<p>data: 789<br \/>\nid: 3<\/p>\n<p>It outputs:<\/p>\n<p>message: 123 [1;00:00:01]<br \/>\nmessage: 456 [2;00:00:01]<br \/>\nmessage: 789 [3;00:00:01]<\/p>\n<h3>Primitives Additions<\/h3>\n<p>Apart from server sent event, System.Net namespace got a few small other additions:<\/p>\n<p>IEquatable&lt;Uri&gt; interface implementation for <a href=\"https:\/\/learn.microsoft.com\/dotnet\/api\/system.uri.equals#system-uri-equals(system-uri)\">Uri<\/a> in <a href=\"https:\/\/github.com\/dotnet\/runtime\/issues\/97940\">dotnet\/runtime#97940<\/a><\/p>\n<p>Which allows using Uri in functions that require IEquatable like <a href=\"https:\/\/learn.microsoft.com\/dotnet\/api\/system.memoryextensions.contains#system-memoryextensions-contains-1(system-readonlyspan((-0)\">Span.Contains<\/a>-0)) or <a href=\"https:\/\/learn.microsoft.com\/dotnet\/api\/system.memoryextensions.sequenceequal#system-memoryextensions-sequenceequal-1(system-readonlyspan((-0)\">SequenceEquals<\/a>-system-readonlyspan((-0))))<\/p>\n<p>span-based <a href=\"https:\/\/learn.microsoft.com\/dotnet\/api\/system.uri.escapedatastring#system-uri-escapedatastring(system-readonlyspan((system-char)\">(Try)EscapeDataString<\/a>)) and <a href=\"https:\/\/learn.microsoft.com\/dotnet\/api\/system.uri.unescapedatastring?system-uri-unescapedatastring(system-readonlyspan((system-char)\">(Try)UnescapeDataString<\/a>)) for Uri in <a href=\"https:\/\/github.com\/dotnet\/runtime\/issues\/40603\">dotnet\/runtime#40603<\/a><\/p>\n<p>The goal is to support low-allocation scenarios and we now take advantage of these methods in <a href=\"https:\/\/learn.microsoft.com\/dotnet\/api\/system.net.http.formurlencodedcontent\">FormUrlEncodedContent<\/a>.<\/p>\n<p>new MIME types for <a href=\"https:\/\/learn.microsoft.com\/dotnet\/api\/system.net.mime.mediatypenames\">MediaTypeNames<\/a> in <a href=\"https:\/\/github.com\/dotnet\/runtime\/issues\/95446\">dotnet\/runtime#95446<\/a><\/p>\n<p>These types were collected over the course of the release and implemented in <a href=\"https:\/\/github.com\/dotnet\/runtime\/pull\/103575\">dotnet\/runtime#103575<\/a> by a community contributor <a href=\"https:\/\/github.com\/CollinAlpert\">@CollinAlpert<\/a>.<\/p>\n<h2>Final Notes<\/h2>\n<p>As each year, we try to write about the interesting and impactful changes in the networking space. This article can\u2019t possibly cover all the changes that were made. If you are interested, you can find the complete list in our<br \/>\n<a href=\"https:\/\/github.com\/dotnet\/runtime\/pulls?q=is%3Apr+is%3Aclosed+is%3Amerged+milestone%3A9.0.0+label%3Aarea-System.Net%2Carea-System.Net.Http%2Carea-System.Net.Sockets%2Carea-System.Net.Security+-label%3Atest-enhancement%2Ctest-bug%2Cdisabled-test\">dotnet\/runtime<\/a><br \/>\nrepository where you can also reach out to us with question and bugs. On top of that, many of the performance changes that are not mentioned here are in Stephen\u2019s great article <a href=\"https:\/\/devblogs.microsoft.com\/dotnet\/performance-improvements-in-net-9\/#networking\">Performance Improvements in .NET 9<\/a>. We\u2019d also like to hear from you, so if you encounter an issue or have any feedback, you can file it in <a href=\"https:\/\/github.com\/dotnet\/runtime\/issues\">our GitHub repo<\/a>.<\/p>\n<p>Lastly, I\u2019d like to thank my co-authors:<\/p>\n<p><a href=\"https:\/\/github.com\/antonfirsov\">@antonfirsov<\/a> who wrote <a href=\"https:\/\/devblogs.microsoft.com\/dotnet\/dotnet-9-networking-improvements\/#diagnostics\">Diagnostics<\/a>.<br \/>\n<a href=\"https:\/\/github.com\/CarnaViire\">@CarnaViire<\/a> who wrote <a href=\"https:\/\/devblogs.microsoft.com\/dotnet\/dotnet-9-networking-improvements\/#httpclientfactory\">HttpClientFactory<\/a> and <a href=\"https:\/\/devblogs.microsoft.com\/dotnet\/dotnet-9-networking-improvements\/#websockets\">WebSockets<\/a>.<\/p>\n<p>The post <a href=\"https:\/\/devblogs.microsoft.com\/dotnet\/dotnet-9-networking-improvements\/\">.NET 9 Networking Improvements<\/a> appeared first on <a href=\"https:\/\/devblogs.microsoft.com\/dotnet\">.NET Blog<\/a>.<\/p>","protected":false},"excerpt":{"rendered":"<p>Continuing our tradition, we are excited to share a blog post highlighting the latest and most interesting changes in the [&hellip;]<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[7],"tags":[],"class_list":["post-1708","post","type-post","status-publish","format-standard","hentry","category-dotnet"],"_links":{"self":[{"href":"https:\/\/rssfeedtelegrambot.bnaya.co.il\/index.php\/wp-json\/wp\/v2\/posts\/1708","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/rssfeedtelegrambot.bnaya.co.il\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/rssfeedtelegrambot.bnaya.co.il\/index.php\/wp-json\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/rssfeedtelegrambot.bnaya.co.il\/index.php\/wp-json\/wp\/v2\/comments?post=1708"}],"version-history":[{"count":0,"href":"https:\/\/rssfeedtelegrambot.bnaya.co.il\/index.php\/wp-json\/wp\/v2\/posts\/1708\/revisions"}],"wp:attachment":[{"href":"https:\/\/rssfeedtelegrambot.bnaya.co.il\/index.php\/wp-json\/wp\/v2\/media?parent=1708"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/rssfeedtelegrambot.bnaya.co.il\/index.php\/wp-json\/wp\/v2\/categories?post=1708"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/rssfeedtelegrambot.bnaya.co.il\/index.php\/wp-json\/wp\/v2\/tags?post=1708"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}