How do I know if I'm running in a web environment?
I'm wonder开发者_JS百科ing if someone has a method for detecting whether or not a thread is running in a web environment.
HttpContext.Current
is not reliable because I'm making these checks in background threads where there is no HttpContext. It seems like
HttpRuntime.AppDomainAppId
is pretty reliable (if it is null then I'm not in a web app), but I'm wondering if anyone else has some tried and true technique.
By the way, I'll be using this for a variety of things. For example this code:
if (HttpRuntime.AppDomainAppId != null)
{
config = WebConfigurationManager.OpenWebConfiguration("~");
}
else
{
config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
}
Thx
JoniTry HostingEnvironment.IsHosted, I think it will work as expected
精彩评论