Random thoughts of wisdom - the personal blog of Michael Holm Andersen

Are we hosted or not ?

In my classes (eg. in my Class Library) I sometimes need to find out if the code that is currently executing runs in a Hosted Environment (like an ASP.NET application) or not.

To find out we can use the System.Web.Hosting.HostingEnvironment.IsHosted property, which tells us whether the current application domain is being hosted by an ApplicationManager object (which manages ASP.NET application domains for an ASP.NET hosting application).

You can read more about the ApplicationManager Class on MSDN