tags:

views:

78

answers:

1

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

+1  A: 

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

Onkelborg
That has nothing to do with the question.
Developer Art
Thanks that seems to be what I was looking for.
joniba