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?
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.
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"...
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.
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.
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 :-(");
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With