MVC6 (.Net core) applications are self hosted : Optional IIS integration

All ASP.NET Core applications are self-hosted.

Yes you read it right!

So does it mean IIS is not required?

Yes, I mean we can host MVC6 application even without IIS.

Wow…but How?

I assume you have already downloaded .Net core from here.

Next step would be to create sample MVC6 project.

For now you can directly clone it from Github by below commands:

git clone git@github.com:aspnet/cli-samples.git
cd cli-samples\HelloMvc

Then we would restore and run the project as below:

dotnet restore
dotnet run 

That is it!

Now navigate to localhost:5000 in your web browser and you can see your application hosted there.

You must be wondering how it happened?

You can check it by yourself in code.

Open Program.cs class and you can see below code there:

public class Program
{
    public static void Main(string[] args)
    {
        var config = new ConfigurationBuilder()
            .AddCommandLine(args)
            .AddEnvironmentVariables(prefix: "ASPNETCORE_")
            .Build();

        var host = new WebHostBuilder()
            .UseConfiguration(config)
            .UseKestrel()
            .UseContentRoot(Directory.GetCurrentDirectory())
            .UseIISIntegration() //// Here IIS integration is optional
            .UseStartup()
            .Build();

        host.Run();
    }
}

As you can see in above comments, IIS integration is optional in .Net core.

Happy coding!

WebListener : A Windows HTTP server for ASP.NET Core

Special thanks to Mr.Tom Dykstra from Microsoft for suggesting some points for this post.

Web Listener is now available on Nuget.

First of all, what is Web Listener?

Web Listener is one of the servers of .Net core.

ASP.NET Core is completely decoupled from the web server environment that hosts the application. ASP.NET Core supports hosting in IIS and IIS Express, and self-hosting scenarios using the Kestrel and WebListener HTTP servers.

As per the .Net team says

WebListener is a Windows HTTP server built on the Http.Sys kernel mode driver. It exposes a number of Http.Sys features previously only availabe in IIS.

Fine, but when should we use Web Listener?

If you are on Windows and  if you want to expose your server directly to the internet. Because Kestrel is not certified for direct exposure.

Or you need any of the features listed below:

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

It is also Recommended for edge deployment because it’s built on the existing Http.Sys HTTP stack. Http.Sys is mature technology that protects against many kinds of attacks. Kestrel is newer, hasn’t had a chance to develop as many protective features yet.

NOTE: Please note that it is Not compatible with ASP.NET Core Module, so can’t be used with IIS even if you wanted to.

Example scenario:

  • Can use Kestrel for internal micro service to micro service communications on private network (kestrel faster), should use WebListener for directly externally facing micro service layer.
  • If a cluster is not on a closed private virtual network, should use WebListener for the inter-service communication as they are open to other things contacting them on their “internal” ports.

Great, and what are Supported Windows versions?

  • Windows 7 and Windows Server 2008 R2 and later

So, How to get it via Nuget?

You can get it using below command:

webl

Great, What to do after getting it from Nuget?

You can use it in code as shown below:

public static void Main(string[] args)
        {
            var host = new WebHostBuilder()
                .UseWebListener(options =>
                {
                    options.ListenerSettings.Authentication.Schemes = AuthenticationSchemes.NTLM;
                    options.ListenerSettings.Authentication.AllowAnonymous = false;
                })
                .UseStartup()
                .Build();

            host.Run();
        }

Awesome, anything extra needed to use WebListener?

Yes, below are few points I got from Mr.Tom Dykstra:

 

Happy coding!