Shawn Eary
Shawn Eary

Reputation: 748

How Do I Determine WebServer Type at Runtime in ASP.NET Core?

Is there an easy way for C# to tell at runtime if a .NET Core WebSite is running under: Kestrel; IIS Express; Apache; Full Blown IIS; or, some other WebServer?

I may not be looking in the right places, but I'm not seeing the information in HttpContext.

UPDATE: The HttpContext.Response.Headers Keys and Values are pretty scarce when I look at them. I'm doing this inside a Filter to log connection information per:
How to auto log every request in .NET Core WebAPI?
I'm not sure if the location where I'm checking the response headers has anything to do with the issue I'm experiencing or not.

Upvotes: -1

Views: 725

Answers (1)

sommmen
sommmen

Reputation: 7618

There does not seem to be builtin way, You could however recognize the windows process that the application runs in.

You can use Process.GetCurrentProcess().ProcessName to get the name, and then recognize the following values, and return the appropriate server.

    w3wp.exe is IIS 6.0 and later.
    aspnet_wp.exe is earlier versions of IIS.
    iisexpress.exe is IISExpress.
    dotnet.exe is ASP.NET Core.
    inetinfo.exe is older ASP applications running in-process.

source

e.g.

ServerEnum GetRunningProcessManager()
{
    var procName = Process.GetCurrentProcess().ProcessName;

    Switch(procName)
    {
       case "w3wp.exe":
          return ServerEnum.IIS6Later;
       ...
       default:
          return ServerEnum.Unknown;
    }

}

Upvotes: 1

Related Questions