How to implement HTTP.sys web server in ASP.NET Core
By FoxLearn 1/4/2025 3:33:38 AM 27
By default, it uses Kestrel, a cross-platform web server, but Kestrel has limitations. To overcome these, HTTP.sys, a Windows-only HTTP server based on the HTTP.sys kernel driver, is recommended due to its maturity, security, and scalability.
Reasons to Use HTTP.sys
HTTP.sys is useful when you need to expose your server to the outside world without using IIS. It handles incoming requests through the HTTP.sys Kernel mode driver, creating a queue and individual application pools for each request. HTTP.sys is also ideal when Kestrel lacks certain features, such as Windows Authentication, Web Sockets, Post Sharing, HTTPS, response caching, and direct file transmission.
Starting a Project with HTTP.sys
To create an ASP.NET Core Web API project in Visual Studio 2022, follow these steps:
- Open Visual Studio and go to File > New > Project.
- Select ASP.Net Core Web Application from the available templates.
- Enter UsingHTTPSysInCode as the project name and click Next.
- In the Create a new ASP.Net Core Web Application window, choose API.
- Select the desired version of ASP.Net Core from the drop-down menu.
- Uncheck Enable Docker Support and choose No Authentication since these options will not be used.
- Click Create.
Configure the ASP.NET Core application for HTTP.sys
To start, install the necessary packages by using the Microsoft.AspNetCore.All meta package via the NuGet package manager.
Next, open the Program.cs file in your project.
public class Program { public static void Main(string[] args) { CreateHostBuilder(args).Build().Run(); } public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); }); }
After installing the packages, configure the HTTP.sys server by using the UseHttpSys extension method of WebHostBuilder in the Main method of the Program.cs file.
public static void Main(string[] args) { CreateHostBuilder(args).Build().Run(); } public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>() .UseHttpSys(options => { options.Authentication.Schemes = AuthenticationSchemes.None; options.Authentication.AllowAnonymous = true; options.MaxConnections = 100; options.MaxRequestBodySize = 1000000; options.UrlPrefixes.Add("http://localhost:5000"); }); });
When running the application, make sure to select the correct launch profile.
By default, Visual Studio uses IIS, but for this example, choose the UsingHTTPSysInCode launch profile, which matches the project name and namespace. When selected, a console window will open to display the steps being executed, followed by the output of the Get method in the ValuesController (or your default controller) in your web browser.
- How to use TinyIoC in ASP.NET Core
- How to use FusionCache in ASP.NET Core
- How to use Brotli for response compression in ASP.NET Core
- How to use SignalR in ASP.NET Core
- How to use the Dapper ORM in ASP.NET Core
- How to enable CORS in ASP.NET Core
- How to use File Providers in ASP.NET Core
- How to use policy-based authorization in ASP.NET Core