Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can my code find if it's running inside IIS?

My C# code may be running inside an MVC3 application under IIS (currently 7.5, but I'd like to not depend on a specific version) or elsewhere.

Looks like one way to know that code is running under IIS is to check the current process name, but this approach depends on having a filename string hardcoded.

Is there some programmatic way to detect my code is running under IIS not depending on IIS version?

like image 933
sharptooth Avatar asked Apr 19 '12 07:04

sharptooth


People also ask

How to debug ASP net Application hosted in IIS?

To start debugging, select the profile name in the toolbar, such as <project profile name>, IIS Express, or <IIS profile name> in the toolbar, select Start Debugging from the Debug menu, or press F5. The debugger pauses at the breakpoints. If the debugger can't hit the breakpoints, see Troubleshoot debugging.

How to check ASP.NET Core Module is installed in IIS Express?

You can check if you have the module in your IIS Express configuration file %\PROGRAMFILES(x86)%\IIS Express\config\templates\PersonalWebServer\applicationhost. config . If there is at least one of the following lines, then you have the module installed: <add name="AspNetCoreModule"...

What is IISHttpServer?

The ASP.NET Core Module receives the native request and passes it to IIS HTTP Server ( IISHttpServer ). IIS HTTP Server is an in-process server implementation for IIS that converts the request from native to managed.

What is Kestrel vs IIS?

IIS server is a reverse proxy server and Kestrel is an application server. The difference is, in an application server, the request from the browser directly hits the hosted application and the code gets executed. A reverse proxy server provides an additional level of abstraction and control over the network traffic.


1 Answers

Have a look at the HostingEnvironment class, especially the IsHosted method.

This will tell you if you are being hosted in an ApplicationManager, which will tell you if you are being hosted by ASP.NET.

Strictly, it will not tell you that you are running under IIS but I think this actually meets your needs better.

Example code:

// Returns the file-system path for a given path.
public static string GetMappedPath(string path)
{
    if (HostingEnvironment.IsHosted)
    {
        if (!Path.IsPathRooted(path))
        {
            // We are about to call MapPath, so need to ensure that 
            // we do not pass an absolute path.
            // 
            // We use HostingEnvironment.MapPath, rather than 
            // Server.MapPath, to allow this method to be used
            // in application startup. Server.MapPath calls 
            // HostingEnvironment.MapPath internally.
            return HostingEnvironment.MapPath(path);
        }
        else {
            return path;
        }
    }
    else 
    {
        throw new ApplicationException (
                "I'm not in an ASP.NET hosted environment :-(");
    }
}
like image 149
RB. Avatar answered Oct 03 '22 02:10

RB.