How can I know if my app is running under Kestrel or HTTP.sys?

2.6k views Asked by At

What's the best way to know if my app is running under Kestrel or HTTP.sys. All that I have found so far is to check for "Kestrel" in one of the HttpConext property class names.

Within an MVC controller I can do something like this:

Boolean IsKestrel = HttpContext.Features.GetType().ToString().Contains("Kestrel");

i.e. check this:

Features = {Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.Http1Connection<Microsoft.AspNetCore.Hosting.HostingApplication.Context>}

When using HTTP.sys I only get:

Features = {Microsoft.AspNetCore.Http.Features.FeatureCollection}

(Is "Http" here enough to know that this is HTTP.sys?)

There must be a better way. Is there an obvious property somewhere that contains the name of the host being used?

A broader question might be, how do I know what the builder pattern built?

Update

Found something better, but still looking for a Property that has the server name or type.

In an MVC controller:

var isKestrel = HttpContext.Request.Headers.GetType().ToString().Contains(".Kestrel.");
var isHTTPsys = HttpContext.Request.Headers.GetType().ToString().Contains(".HttpSys.");
4

There are 4 answers

1
Deepak-MSFT On

I am not sure whether you want to check this information using the code only or you are just looking for a way to know on which web server your app is running.

In my search result, I found that we could set the ports for a specific web server. When the application will run on that specific web server then it will use that pre-configured port. I am assuming your app also has a similar configuration. You could set the different ports for Kestrel, Http.sys, or IIS. By checking the port number you could say that on which web server your site is running.

You could try to go to the launchSettings.json file in your project where you could configure ports for IIS and Kestral.

enter image description here

Helpful References:

  1. Kestrel Web Server in ASP.NET Core

  2. Understand HTTP.sys Web Server In ASP.NET Core

1
jack On

you can use System.Diagnostics.Process.GetCurrentProcess().ProcessName

2
Peter Wishart On

At the operating system level, netsh http show servicestate will list all active URLs listening via HTTP.SYS.

From code you can locate an instance of Microsoft.AspNetCore.Hosting.Server.IServer and check what its implementation is, in netcore 6:

  • Kestrel => Microsoft.AspNetCore.Server.Kestrel.Core.KestrelServerImpl
  • IIS ==> Microsoft.AspNetCore.Server.IIS.Core.IISHttpServer
  • HTTP.SYS => Microsoft.AspNetCore.Server.HttpSys.MessagePump

This relies on implementation details (so can break), also other extensions can change these e.g. CoreWcf creates CoreWCF.Configuration.WrappingIServer that wraps one of the above implementations.

2
Transformer On

Hello this is a good question, you question is asking how to find out from inside the code and not from a console.

OOB I did not find anything. So, I had to get very creative to figure this out, sorry for the typo's its brand new stuff...


Option 1:

Since the Kestrel section & endpoints are inside the appsettings.json I used that to find out if its hosted by Kestrel!

//Please create a static class to hold the config.
public static class MyStartupIsItKestrelConfiguration
{
    public static IConfiguration Configuration;

    public bool static IsKestrel()
    {
      //check your section kestrel??
      var kestrel =  configuration.GetSection("Kestrel"); 

      // now check kestrel section or any other section 
      // see picture for kestrel endpoint in app setting sbelow
      return true;
    }
}

Now you can access it anywhere and see if you used Kestrel

//Now add it/save it in your startup and access later
public Startup(IConfiguration configuration)
{
    Configuration = configuration;
    MyStartupIsItKestrelConfiguration.Configuration = configuration;
}

Once you have this

//you can use it in ** YOUR CONTROLLER
MyStartupIsItKestrelConfiguration.IsKestrel();

Kestrel appsettings

Option 2:

Please check this public Microsoft.AspNetCore.Http.Features.IFeatureCollection Features { get; }

You can get the features public TFeature? Get<TFeature> (); as a Key Value Pair - and then check the feature for e.g. KESTREL DOES NOT ALLOW PORT SHARING

Get Kestrel Features

they split the features namespace in .net core 6 there are breaking changes

Breaking Changes to ASP Core hosting Kesterl

You should use the features collection