DEV Community

Naman Singh
Naman Singh

Posted on • Updated on

Default HTTP/3 .NET Support

As the world of web development continues to evolve, the need for faster, more efficient, and secure network protocols becomes increasingly crucial. In .NET 8, developers now have access to the power of the HTTP/3 protocol by default. In this blog post, we will delve into the technical aspects of this feature, covering how it can boost your application's performance and security and go over a basic implementation.

Image description

What is HTTP/3?

HTTP/3, the third major version of the Hypertext Transfer Protocol, is the successor to HTTP/2. Built on the QUIC transport layer protocol, HTTP/3 improves upon its predecessor by offering several advantages, including:

  1. Faster connection establishment
  2. Better multiplexing and prioritization
  3. Improved connection migration
  4. Enhanced loss recovery

Image description

.NET 8 Default Support

To leverage the advantages of HTTP/3 in your applications, .NET 8 has incorporated it as a default feature, streamlining the development process. The following sections will detail how to enable and utilize HTTP/3 support in your .NET 8 projects.

To enable it for your applications, set the SocketsHttpHandler.Http3Enabled property to true

var handler = new SocketsHttpHandler { Http3Enabled = true };
var httpClient = new HttpClient(handler);
Enter fullscreen mode Exit fullscreen mode

To enable HTTP/3 support on the server-side using Kestrel, you need to configure the server to listen for QUIC connections. This is achieved by specifying the HttpProtocols.Http3 protocol and providing a valid TLS certificate:

var host = new HostBuilder()
    .ConfigureWebHost(webHost =>
    {
        webHost.UseKestrel(options =>
        {
            options.ListenAnyIP(5001, listenOptions =>
            {
                listenOptions.Protocols = HttpProtocols.Http3;
                listenOptions.UseHttps("testCert.pfx", "testPassword");
            });
        });
    })
    .Build();

host.Run();
Enter fullscreen mode Exit fullscreen mode

To enable for gRPC:

Since gRPC relies on HTTP/2 for communication, enabling HTTP/3 support for gRPC in .NET 8 requires a few adjustments. To use gRPC with HTTP/3, you need to configure the client and server to use HttpClient and Kestrel, respectively:

Client-side:

var channel = GrpcChannel.ForAddress("https://localhost:5001", new GrpcChannelOptions
{
    HttpHandler = new SocketsHttpHandler
    {
        Http3Enabled = true
    }
});
Enter fullscreen mode Exit fullscreen mode

Server-side:

var host = new HostBuilder()
    .ConfigureWebHost(webHost =>
    {
        webHost.UseKestrel(options =>
        {
            options.ListenAnyIP(5001, listenOptions =>
            {
                listenOptions.Protocols = HttpProtocols.Http3;
                listenOptions.UseHttps("certificate.pfx", "testPassword");
            });
        });
    })
    .ConfigureServices(services =>
    {
        services.AddGrpc();
    })

.Configure(app =>
{
    app.UseRouting();
    app.UseEndpoints(endpoints =>
    {
        endpoints.MapGrpcService<MyGrpcService>();
    });
})
.Build();
host.Run();

Enter fullscreen mode Exit fullscreen mode

In the next series of this I will post on how to achieve testing and debugging against the HTTP/3 protocol.

Keep coding!

References:
https://learn.microsoft.com/en-us/dotnet/core/whats-new/dotnet-8
https://blog.cloudflare.com/content/images/2018/07/http-request-over-quic@2x.png
https://images.ctfassets.net/ee3ypdtck0rk/5oX21p5upAslPIzwJlVPCw/d50a0811796d5c9307ad84ddd0332c90/Screen_Shot_2021-01-28_at_6.46.26.png?w=1323&h=661&q=50&fm=png

Top comments (0)