Skip to content

Latest commit

 

History

History
173 lines (110 loc) · 10.5 KB

File metadata and controls

173 lines (110 loc) · 10.5 KB
title author description keywords ms.author manager ms.date ms.topic ms.assetid ms.technology ms.prod uid
WebListener web server implementation in ASP.NET Core | Microsoft Docs
rick-anderson
Introduces WebListener, a web server for ASP.NET Core on Windows. Built on the Http.Sys kernel mode driver, WebListener is an alternative to Kestrel that can be used for direct connection to the Internet without IIS.
ASP.NET Core 中文文档, WebListener, HttpListener, url prefixes, SSL
riande
wpickett
10/27/2016
article
0a7286e4-6428-424e-b5c4-5c98815cf61c
aspnet
asp.net-core
fundamentals/servers/weblistener

WebListener web server implementation in ASP.NET Core

By Tom Dykstra and Chris Ross

WebListener is a web server for ASP.NET Core that runs only on Windows. It's built on the Http.Sys kernel mode driver. WebListener is an alternative to Kestrel that can be used for direct connection to the Internet without relying on IIS as a reverse proxy server. In fact, WebListener can't be used with IIS or IIS Express, as it isn't compatible with the ASP.NET Core Module.

Although WebListener was developed for ASP.NET Core, it can be used directly in any .NET Core or .NET Framework application via the Microsoft.Net.Http.Server NuGet package.

WebListener supports the following features:

  • Windows Authentication
  • Port sharing
  • HTTPS with SNI
  • HTTP/2 over TLS (Windows 10)
  • Direct file transmission
  • Response caching
  • WebSockets (Windows 8)

Supported Windows versions:

  • Windows 7 and Windows Server 2008 R2 and later

View or download sample code

When to use WebListener

WebListener is useful for deployments where you need to expose the server directly to the Internet without using IIS.

WebListener to Internet

Because it's built on Http.Sys, WebListener doesn't require a reverse proxy server for protection against attacks. Http.Sys is mature technology that protects against many kinds of attacks and provides the robustness, security, and scalability of a full-featured web server. IIS itself runs as an HTTP listener on top of Http.Sys.

WebListener is also a good choice for internal deployments when you need one of the features it offers that you can't get by using Kestrel.

WebListener to Internet

How to use WebListener

Here's an overview of setup tasks for the host OS and your ASP.NET Core application.

Configure Windows Server

  • Install the version of .NET that your application requires, such as .NET Core or .NET Framework 4.5.1.

  • Preregister URL prefixes to bind to WebListener, and set up SSL certificates

    If you don't preregister URL prefixes in Windows, you have to run your application with administrator privileges. The only exception is if you bind to localhost using HTTP (not HTTPS) with a port number greater than 1024; in that case administrator privileges aren't required.

    For details, see How to preregister prefixes and configure SSL later in this article.

  • Open firewall ports to allow traffic to reach WebListener.

    You can use netsh.exe or PowerShell cmdlets.

There are also Http.Sys registry settings.

Configure your ASP.NET Core application

  • Install the NuGet package Microsoft.AspNetCore.Server.WebListener. This also installs Microsoft.Net.Http.Server as a dependency.

  • Call the UseWebListener extension method on WebHostBuilder in your Main method, specifying any WebListener options and settings that you need, as shown in the following example:

    [!code-csharp]

  • Configure URLs and ports to listen on

    By default ASP.NET Core binds to http://localhost:5000. To configure URL prefixes and ports, you can use the UseURLs extension method, the urls command-line argument or the ASP.NET Core configuration system. For more information, see Hosting.

    Web Listener uses the Http.Sys prefix string formats. There are no prefix string format requirements that are specific to WebListener.

    [!NOTE] Make sure that you specify the same prefix strings in UseUrls that you preregister on the server.

  • Make sure your application is not configured to run IIS or IIS Express.

    In Visual Studio, the default launch profile is for IIS Express. To run the project as a console application you have to manually change the selected profile, as shown in the following screen shot.

    Select console app profile

How to use WebListener outside of ASP.NET Core

There are also Http.Sys registry settings.

Here's a code sample that demonstrates WebListener use outside of ASP.NET Core:

var settings = new WebListenerSettings();
settings.UrlPrefixes.Add("http://localhost:8080");

using (WebListener listener = new WebListener(settings))
{
    listener.Start();

    while (true)
    {
        var context = await listener.AcceptAsync();
        byte[] bytes = Encoding.ASCII.GetBytes("Hello World: " + DateTime.Now);
        context.Response.ContentLength = bytes.Length;
        context.Response.ContentType = "text/plain";

        await context.Response.Body.WriteAsync(bytes, 0, bytes.Length);
        context.Dispose();
    }
}

Preregister URL prefixes and configure SSL

Both IIS and WebListener rely on the underlying Http.Sys kernel mode driver to listen for requests and do initial processing. In IIS, the management UI gives you a relatively easy way to configure everything. However, if you're using WebListener you need to configure Http.Sys yourself. The built-in tool for doing that is netsh.exe.

The most common tasks you need to use netsh.exe for are reserving URL prefixes and assigning SSL certificates.

NetSh.exe is not an easy tool to use for beginners. The following example shows the bare minimum needed to reserve URL prefixes for ports 80 and 443:

netsh http add urlacl url=http://+:80/ user=Users
netsh http add urlacl url=https://+:443/ user=Users

The following example shows how to assign an SSL certificate:

netsh http add sslcert ipport=0.0.0.0:443 certhash=MyCertHash_Here appid={00000000-0000-0000-0000-000000000000}".

Here is the official reference documentation:

The following resources provide detailed instructions for several scenarios. Articles that refer to HttpListener apply equally to WebListener, as both are based on Http.Sys.

Here are some third-party tools that can be easier to use than the netsh.exe command line. These are not provided by or endorsed by Microsoft. The tools run as administrator by default, since netsh.exe itself requires administrator privileges.

  • HttpSysManager provides UI for listing and configuring SSL certificates and options, prefix reservations, and certificate trust lists.
  • HttpConfig lets you list or configure SSL certificates and URL prefixes. The UI is more refined than HttpSysManager and exposes a few more configuration options, but otherwise it provides similar functionality. It cannot create a new certificate trust list (CTL), but can assign existing ones.

For generating self-signed SSL certificates, Microsoft provides command-line tools: MakeCert.exe and the PowerShell cmdlet New-SelfSignedCertificate. There are also third-party UI tools that make it easier for you to generate self-signed SSL certificates:

Next steps

For more information, see the following resources: