joniba
joniba

Reputation: 3499

How do I know if I'm running in a web environment?

I'm wondering 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

Upvotes: 4

Views: 359

Answers (1)

Onkelborg
Onkelborg

Reputation: 3997

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

Upvotes: 2

Related Questions