.NET 6 中的 HTTP/3 支持
根據官方博客的介紹,.NET 6 提供了對 HTTP/3 的預覽支持,主要包括以下場景:
- 在 Kestrel、HTTP.Sys 和 IIS 中,用于 ASP.NET 服務器
- 在 HttpClient 中發送 outbound 請求
- 面向 gRPC
.NET 開發團隊表示,HTTP/3 的 RFC 還沒有最終確定,但他們還是將 HTTP/3 引入到了 .NET 6 中,方便用戶開始進行試驗,但這只是 .NET 6 的預覽功能——因為它不符合 .NET 6 其余部分的質量標準。因此需要與其他服務器和客戶端進行更廣泛的測試以確保兼容性,尤其是在邊界情況下。
試用 HTTP/3
如需使用 HTTP/3,需安裝 MSQuic 及其 TLS 依賴項。
目前只支持 Windows 和 Linux,.NET 6 暫不支持 macOS 上的 HTTP/3,主要是因為缺少與 QUIC 兼容的 TLS API。.NET 團隊認為,由于 .NET 在 macOS 上使用 SecureTransport 來實現其 TLS 實現,它尚未包含支持 QUIC 握手的 TLS API。雖然可以使用 OpenSSL,但他們認為最好不要引入未與操作系統的證書管理集成的附加依賴項。
示例
使用 HTTP/3 的 gRPC
gRPC 是一種使用 protobuf 序列化格式的 RPC 機制。gRPC 通常使用 HTTP/2 作為其傳輸。HTTP/3 使用了相同的語義,因此幾乎不需要更改即可使其工作。gRPC over HTTP/3 由 .NET 團隊提出,目前還不是一個標準。
ASP.NET Server
- var builder = WebApplication.CreateBuilder(args);
- // Add services to the container.
- builder.Services.AddGrpc();
- builder.WebHost.ConfigureKestrel((context, options) =>
- {
- options.Listen(IPAddress.Any, 5001, listenOptions =>
- {
- listenOptions.Protocols = HttpProtocols.Http3;
- listenOptions.UseHttps();
- });
- });
- var app = builder.Build();
- // Configure the HTTP request pipeline.
- if (app.Environment.IsDevelopment())
- {
- app.UseDeveloperExceptionPage();
- }
- app.MapGrpcService<GreeterService>();
- app.MapGet("/", () => "Communication with gRPC endpoints must be made through a gRPC client. To learn how to create a client, visit: https://go.microsoft.com/fwlink/?linkid=2086909");
- app.Run();
Client
- using Grpc.Net.Client;
- using GrpcService1;
- using System.Net;
- var httpClient = new HttpClient();
- httpClient.DefaultRequestVersion = HttpVersion.Version30;
- httpClient.DefaultVersionPolicy = HttpVersionPolicy.RequestVersionExact;
- var channel = GrpcChannel.ForAddress("https://localhost:5001", new GrpcChannelOptions() { HttpClient = httpClient });
- var client = new Greeter.GreeterClient(channel);
- var response = await client.SayHelloAsync(
- new HelloRequest { Name = "World" });
- Console.WriteLine(response.Message);
詳情查看官方博客。
本站新聞禁止未經授權轉載,違者依法追究相關法律責任。授權請聯系:oscbianji#oschina.cn
本文標題:.NET 6 中的 HTTP/3 支持
本文地址:https://www.oschina.net/news/161364/http-3-support-in-dotnet-6