开发者

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

Joni


Try HostingEnvironment.IsHosted, I think it will work as expected

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜