Joydip Kanjilal
Contributor

How to implement HTTP.sys web server in ASP.Net Core

how-to
Jul 30, 20184 mins
Software DevelopmentWeb Development

Learn how to implement HTTP.sys, a Windows-only HTTP server based on the HTTP.sys kernel driver that is mature, secure and scalable in ASP.Net Core

thread weave colorful network web
Credit: Thinkstock

ASP.Net Core is an open source, cross-platform, lean, and modular framework for building high-performance web applications. Kestrel is a cross-platform web server for ASP.Net Core that is include by default. However, it has limitations.

To get around those limitations, I suggest you use HTTP.sys, a Windows-only HTTP server based on the HTTP.sys kernel driver that is more mature, secure, and scalable.

Why you should use HTTP.sys

Typically, you need HTTP.sys when you have to expose your server to the outside world sans IIS (Microsoft Internet Information Services). The requests first come to the HTTP.sys—built on the HTTP.sys Kernel mode driver. HTTP.sys in turn creates a queue as well as an individual application pool for each request based on the request.

You can also use HTTP.sys when you need a feature that is not supported by Kestrel. Features supported by HTTP.sys include:

  1. Windows Authentication
  2. Web Sockets
  3. Post Sharing
  4. HTTPS
  5. Response caching
  6. Direct file transmission

Get a project started in HTTP.sys

If you’re running Visual Studio 2017, follow these steps to create an ASP.Net Core Web API project:

  1. In the Visual Studio IDE, choose File > New > Project.
  2. Select ASP.Net Core Web Application (.Net Core) from the list of the templates displayed.
  3. Specify UsingHTTPSysInCode as the name for the project.
  4. Click OK to save the project.
  5. Select API in the New .Net Core Web Application window.
  6. Select the version of ASP.Net Core you want to use from the drop-down menu at the top.
  7. Uncheck Enable Docker Support and select No Authentication, because you won’t be using either of these here.
  8. Click OK.

These steps create a new ASP.Net Core Project named UsingHTTPSysInCode in Visual Studio 2017.

Configure the ASP.net Core application for HTTP.sys

Next, you should install the packages you need. The best way to do this is to install the Microsoft.AspNetCore.All meta package via the NuGet package manager. This ensures that all necessary packages are installed at one go.

Then open the Program.cs file in your project. It should look like this:

public class Program
    {
        public static void Main(string[] args)
        {
            CreateWebHostBuilder(args).Build().Run();
        }
        public static IWebHostBuilder
         CreateWebHostBuilder(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .UseStartup<Startup>();
     }

With the packages installed, configure HTTP.sys server via the UseHttpSys extension method of WebHostBuilder in the Main method for the Program class in Program.cs file. Here’s how:

public static void Main(string[] args)
        {
            CreateWebHostBuilder(args).Run();
        }
public static IWebHost CreateWebHostBuilder(string[] args) =>
          WebHost.CreateDefaultBuilder(args)
              .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");
                })
              .Build();

Here’s the complete source code of the Program class:

using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Server.HttpSys;
namespace UsingHTTPSysInCode
{
    public class Program
    {
        public static void Main(string[] args)
        {
            CreateWebHostBuilder(args).Run();
        }
        public static IWebHost CreateWebHostBuilder(string[] args) =>
          WebHost.CreateDefaultBuilder(args)
              .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");
                })
              .Build();
    }

}

Finally, when you run the application, ensure that you select the launch profile appropriately. The default launch profile is IIS in Visual Studio. Select UsingHTTPSysInCode for this example; it is the same as the name of the project as well as the namespace.

When you run the application with the launch profile as UsingHTTPSysInCode, a console window opens to display the series of steps being executed before you see the output of the Get method of the ValuesController (assuming that’s your default controller) in your web browser.

Joydip Kanjilal
Contributor

Joydip Kanjilal is a Microsoft Most Valuable Professional (MVP) in ASP.NET, as well as a speaker and the author of several books and articles. He received the prestigious MVP award for 2007, 2008, 2009, 2010, 2011, and 2012.

He has more than 20 years of experience in IT, with more than 16 years in Microsoft .Net and related technologies. He has been selected as MSDN Featured Developer of the Fortnight (MSDN) and as Community Credit Winner several times.

He is the author of eight books and more than 500 articles. Many of his articles have been featured at Microsoft’s Official Site on ASP.Net.

He was a speaker at the Spark IT 2010 event and at the Dr. Dobb’s Conference 2014 in Bangalore. He has also worked as a judge for the Jolt Awards at Dr. Dobb's Journal. He is a regular speaker at the SSWUG Virtual Conference, which is held twice each year.

More from this author