Refactor Boilerplate http client pipeline (#9966)#9967
Refactor Boilerplate http client pipeline (#9966)#9967msynk merged 1 commit intobitfoundation:developfrom yasmoradi:9966
Conversation
WalkthroughThe changes reinstate and adjust key functionality across several projects. The push notification subscription in the ClientAppCoordinator is re-enabled by uncommenting its registration in the PropagateUserId method. Multiple service configuration files have been updated to replace direct HttpClientHandler registrations with retrieval via keyed HttpMessageHandler services. Additionally, the HTTP request version is now set to HTTP/3.0 in the RequestHeadersDelegatingHandler, and HTTP client configurations across Maui, Web, Windows, and Server projects have been enhanced with updated connection and TLS settings. Changes
Sequence Diagram(s)sequenceDiagram
participant CA as ClientAppCoordinator
participant PNS as PushNotificationService
CA->>PNS: PropagateUserId(userId)
PNS-->>CA: Subscribe(userId) confirmed
sequenceDiagram
participant App as Application Startup
participant DI as Dependency Injection Container
participant HMH as HttpMessageHandler (Keyed Service)
participant HC as HttpClient
App->>DI: ConfigureServices (Register HttpClient & HttpMessageHandler)
DI->>DI: Register key "PrimaryHttpMessageHandler"
HC->>DI: Resolve HttpMessageHandler via key "PrimaryHttpMessageHandler"
DI-->>HC: Return configured HMH (SocketsHttpHandler/HttpClientHandler)
Poem
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (4)
src/Templates/Boilerplate/Bit.Boilerplate/src/Client/Boilerplate.Client.Core/Components/ClientAppCoordinator.cs (1)
143-146: Consider adding unsubscription logic and deduplication.The push notification subscription could benefit from:
- Unsubscribing from previous subscription before creating a new one to prevent potential duplicate subscriptions.
- Deduplication logic to avoid unnecessary resubscriptions when the user ID hasn't changed.
//#if (notification == true) + if (isAuthenticated) + { + await pushNotificationService.Unsubscribe(CurrentCancellationToken); await pushNotificationService.Subscribe(CurrentCancellationToken); + } //#endifsrc/Templates/Boilerplate/Bit.Boilerplate/src/Client/Boilerplate.Client.Web/Program.Services.cs (1)
37-37: Consider using SocketsHttpHandler for consistency.
HttpClientHandleris used here while other platforms useSocketsHttpHandler. For consistency and better performance, consider usingSocketsHttpHandlerwith HTTP/2 and HTTP/3 support.-services.AddKeyedScoped<HttpMessageHandler, HttpClientHandler>("PrimaryHttpMessageHandler"); +services.AddKeyedScoped<HttpMessageHandler, SocketsHttpHandler>("PrimaryHttpMessageHandler", (sp, key) => new() +{ + EnableMultipleHttp2Connections = true, + EnableMultipleHttp3Connections = true, + PooledConnectionLifetime = TimeSpan.FromMinutes(15), + AutomaticDecompression = System.Net.DecompressionMethods.All +});src/Templates/Boilerplate/Bit.Boilerplate/src/Client/Boilerplate.Client.Maui/MauiProgram.Services.cs (1)
36-50: Consider extracting shared HTTP client configuration.The HTTP client configuration is identical to the Windows implementation. Consider extracting this into a shared extension method to reduce code duplication.
Create a new extension method in a shared location:
public static class HttpClientConfigurationExtensions { public static SocketsHttpHandler CreateDefaultHandler() { return new() { EnableMultipleHttp2Connections = true, #if NET9_0 EnableMultipleHttp3Connections = true, #endif PooledConnectionLifetime = TimeSpan.FromMinutes(15), AutomaticDecompression = System.Net.DecompressionMethods.All, SslOptions = new() { EnabledSslProtocols = SslProtocols.Tls12 | SslProtocols.Tls13 } }; } }Then simplify both Windows and Maui implementations:
-services.AddKeyedScoped<HttpMessageHandler, SocketsHttpHandler>("PrimaryHttpMessageHandler", (sp, key) => new() -{ - EnableMultipleHttp2Connections = true, - EnableMultipleHttp3Connections = true, - PooledConnectionLifetime = TimeSpan.FromMinutes(15), - AutomaticDecompression = System.Net.DecompressionMethods.All, - SslOptions = new() - { - EnabledSslProtocols = SslProtocols.Tls12 | SslProtocols.Tls13 - } -}); +services.AddKeyedScoped<HttpMessageHandler, SocketsHttpHandler>("PrimaryHttpMessageHandler", + (sp, key) => HttpClientConfigurationExtensions.CreateDefaultHandler());src/Templates/Boilerplate/Bit.Boilerplate/src/Client/Boilerplate.Client.Core/Extensions/IClientCoreServiceCollectionExtensions.cs (1)
90-93: Good refactoring to use keyed service for HttpMessageHandler!The change from direct HttpClientHandler registration to keyed service retrieval provides better flexibility and consistency with server-side configuration.
Consider adding a code comment explaining the keyed service pattern, similar to the existing comment about the handler chain:
services.AddScoped(serviceProvider => { + // Retrieve the primary HTTP message handler (SocketsHttpHandler/HttpClientHandler) from keyed services + // This allows for different implementations in various environments (e.g., Test Host for integration tests) var transportHandler = serviceProvider.GetRequiredKeyedService<HttpMessageHandler>("PrimaryHttpMessageHandler"); var constructedHttpMessageHandler = serviceProvider.GetRequiredService<HttpMessageHandlersChainFactory>().Invoke(transportHandler); return constructedHttpMessageHandler; });
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (7)
src/Templates/Boilerplate/Bit.Boilerplate/src/Client/Boilerplate.Client.Core/Components/ClientAppCoordinator.cs(1 hunks)src/Templates/Boilerplate/Bit.Boilerplate/src/Client/Boilerplate.Client.Core/Extensions/IClientCoreServiceCollectionExtensions.cs(1 hunks)src/Templates/Boilerplate/Bit.Boilerplate/src/Client/Boilerplate.Client.Core/Services/HttpMessageHandlers/RequestHeadersDelegatingHandler.cs(2 hunks)src/Templates/Boilerplate/Bit.Boilerplate/src/Client/Boilerplate.Client.Maui/MauiProgram.Services.cs(3 hunks)src/Templates/Boilerplate/Bit.Boilerplate/src/Client/Boilerplate.Client.Web/Program.Services.cs(1 hunks)src/Templates/Boilerplate/Bit.Boilerplate/src/Client/Boilerplate.Client.Windows/Program.Services.cs(3 hunks)src/Templates/Boilerplate/Bit.Boilerplate/src/Server/Boilerplate.Server.Web/Program.Services.cs(2 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (1)
- GitHub Check: build and test
🔇 Additional comments (4)
src/Templates/Boilerplate/Bit.Boilerplate/src/Client/Boilerplate.Client.Core/Components/ClientAppCoordinator.cs (1)
143-146: LGTM! Push notification subscription re-enabled.The subscription is properly placed after SignalR initialization, wrapped in the correct conditional compilation directive, and protected by error handling.
src/Templates/Boilerplate/Bit.Boilerplate/src/Client/Boilerplate.Client.Windows/Program.Services.cs (1)
32-46: Well-configured HTTP client with modern security and performance settings!The configuration includes:
- Multiple HTTP/2 and HTTP/3 connections for improved performance
- Connection pooling with 15-minute lifetime
- Modern TLS protocols (1.2 and 1.3)
- Automatic compression
src/Templates/Boilerplate/Bit.Boilerplate/src/Server/Boilerplate.Server.Web/Program.Services.cs (2)
125-126: LGTM! Enhanced HTTP version negotiation.The addition of
DefaultVersionPolicy = HttpVersionPolicy.RequestVersionOrLowerimproves HTTP version negotiation by allowing graceful fallback to lower versions when needed.
160-168:Details
❓ Verification inconclusive
Excellent choice of SocketsHttpHandler with enhanced connection settings!
The configuration enables multiple HTTP/2 connections and conditionally supports HTTP/3, which can significantly improve performance for concurrent requests.
Let's verify the framework version and HTTP/3 support:
🏁 Script executed:
#!/bin/bash # Check .NET SDK version and HTTP/3 support dotnet --versionLength of output: 115
🌐 Web query:
What are the minimum requirements for HTTP/3 support in .NET?💡 Result:
To enable HTTP/3 support in .NET applications, specific requirements must be met depending on the target platform and .NET version:
.NET Version Requirements
- Full support starts with ASP.NET Core 7.0+ (no longer a preview feature)[1][2][3].
- Preview functionality was available in ASP.NET Core 6.0 but required manual configuration via runtime flags[1][4].
Operating System Requirements
Platform Requirements Windows - Windows 11 Build 22000+ or Windows Server 2022
- TLS 1.3+ connection[2][3][9]Linux - libmsquicpackage installed (version 1.9.x for .NET 6 compatibility)
- TLS 1.3+[2][3][9]macOS Not currently supported[2][3] Additional Requirements
HTTPS Enforcement:
HTTP/3 requires HTTPS to be enabled on Kestrel endpoints[2][3]:listenOptions.UseHttps();Protocol Configuration:
Explicitly enable HTTP/3 in Kestrel alongside fallback protocols:options.ListenAnyIP(5001, listenOptions => { listenOptions.Protocols = HttpProtocols.Http1AndHttp2AndHttp3; });Client Configuration:
ForHttpClientto use HTTP/3:var client = new HttpClient { DefaultRequestVersion = HttpVersion.Version30, DefaultVersionPolicy = HttpVersionPolicy.RequestVersionOrHigher };Platform-Specific Notes
- Linux: Install
libmsquicvia Microsoft's package repository:apt install libmsquic=1.9*- Legacy .NET 6: Requires runtime flag
System.Net.SocketsHttpHandler.Http3Support=truein project file or environment variables[4][6].Applications should always maintain backward compatibility with HTTP/1.1 and HTTP/2 due to uneven client/server support for HTTP/3[1][2].
Citations:
- 1: https://www.coherentsolutions.com/insights/new-features-in-http-3-and-their-support-in-asp-net-core
- 2: https://learn.microsoft.com/en-us/aspnet/core/fundamentals/servers/kestrel/http3?view=aspnetcore-9.0
- 3: https://github.com/dotnet/AspNetCore.Docs/blob/main/aspnetcore/fundamentals/servers/kestrel/http3.md
- 4: https://learn.microsoft.com/en-us/dotnet/core/extensions/httpclient-http3
- 5: https://docs.fastly.com/en/guides/enabling-http3-for-fastly-services
- 6: https://hackernoon.com/upgrade-your-web-development-game-with-http3-in-net-6
- 7: https://www.ietf.org/archive/id/draft-ietf-quic-http-34.html
- 8: https://sd.blackball.lv/articles/read/19138-how-to-implement-http3-in-your-aspnet-core-application
- 9: https://www.meziantou.net/using-http-3-quic-in-dotnet.htm
- 10: https://en.wikipedia.org/wiki/HTTP/3
Optimize HTTP Connection Settings – Please Verify HTTP/3 Requirements
Excellent use of
SocketsHttpHandlerto boost HTTP/2 and conditionally enable HTTP/3 connections! The configuration looks solid; however, note that the automated check for the .NET SDK version returned “dotnet: command not found.” Please ensure:
- Your deployment environment has a supported .NET runtime (ideally ASP.NET Core 7.0+ for full HTTP/3 support or the necessary manual configurations in earlier versions).
- HTTPS is properly enforced and any platform-specific HTTP/3 prerequisites (such as required OS or library installations) are met.
Please manually verify your .NET installation and HTTP/3 support based on Microsoft’s HTTP/3 documentation.
closes #9966
closes #9959