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

4

6

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.");
Xiomaraxiong answered 14/3, 2022 at 20:28 Comment(4)
You could use DI to get IServer learn.microsoft.com/en-us/dotnet/api/… . For kestrel, the implementation type contains the word "Kestrel" github.com/dotnet/aspnetcore/blob/main/src/Servers/Kestrel/Core/… while I believe it should be "MessagePump" for http.sys: github.com/dotnet/aspnetcore/blob/main/src/Servers/HttpSys/src/…Alvarado
Simon, that works, but it does require extras steps due to the DI. The results of taking the injected object ToString() are: "Microsoft.AspNetCore.Server.Kestrel.Core.KestrelServer" and "Microsoft.AspNetCore.Server.HttpSys.MessagePump". Or by using InjectedObject.GetType().Name you get "KestrelServer" or "MessagePump". It seems there should be a property somewhere so we don't have to depend on a class name.Xiomaraxiong
Which server exactly provides the "server service" is an implementation detail. General principle for ASP.NET Core is for the developer to ask for a service using DI, whatever the environment details are. You can analyze ASP.NET core and check what services are provided by one server vs the other, but there's no current notion of "server name" service. If you own program/startup/config, you can build your own DI service for that type of information to be able to query it.Alvarado
I didn't a reason for why you needed to know this?Sly
G
6

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.

Gosser answered 25/3, 2022 at 0:27 Comment(2)
Thanks for looking into this. On what object would I find an instance of Microsoft.AspNetCore.Hosting.Server.IServer?Xiomaraxiong
I used IWebHost.Services.GetRequiredService<Microsoft.AspNetCore.Hosting.Server.IServer>();Gosser
L
1

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

Leakage answered 15/3, 2022 at 2:30 Comment(1)
That would only be useful if I was defining unique ports for Kestrel vs. HTTP.sys. What about in production: www.example.com.Xiomaraxiong
I
0

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

Immunize answered 14/3, 2022 at 21:24 Comment(1)
That only tells me if I am running behind IIS (IISExpress for example) or the name of my .NET Core applications ("MyTestWebApp"). I'm looking for Kestrel vs HTTP.sys.Xiomaraxiong
A
0

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

Awfully answered 25/3, 2022 at 3:15 Comment(2)
Thanks for the research and the idea. Your solution would work with a customized appsettings.json. In my tests neither 3.1 or 6.0 projects add anything Kestrel or HTTP.sys related to appsettings.json by default. For Option 2, do you have any example code? Where do I find that collection?Xiomaraxiong
@MikeSmith-MCT-MVP did you try the snippets, I did my work based on these, when I was doing work recently for another company. I can try to compact a full working sample when I have time.. if you upvoteAwfully

© 2022 - 2024 — McMap. All rights reserved.