tags:

views:

86

answers:

6

Dear ladies and sirs.

I have a library code, which should be aware whether it is executed in the context of a web server or standalone application server.

The obvious that comes to mind is to check the name of the application configuration file and if it is web.config - then this is a web server, otherwise - standalone application server.

Another way is to look for something like "Temporary ASP.NET files" in the path of the shadow folder.

But I dislike both of these, since they seem too hacky and fragile. Is there a robust way to do what I want?

Thanks.

P.S.

One may define a dedicated app config setting - IsWebServer, but I dislike it even more.

EDIT:

While looking for a solution to another problem, I think I solved this one - the details are here

+6  A: 

You can check for

HttpContext.Current

If it is not null then it is run from a web app.

See HttpContext.Current Property

rahul
That will only work if it is running in the same thread as the request - subsequently spawned background threads won't have a HttpContext.
DrJokepu
@DrJokepu - precisely. In my case HttpContext.Current is null.
mark
+1  A: 
  • BrowserInteropHelper..::.IsBrowserHosted Property

Gets a value that specifies whether the current Windows Presentation Foundation (WPF) application is browser hosted.

Thats how its done in XBAP

or if you have an appdomain do reflection and get something from it? using

abmv
+2  A: 

You can Find out the current process name:

Process p = Process.GetCurrentProcess();
string assemblyName = p.ProcessName;

and then check if this is the ASP.NET process name

sagie
And what if it's running in mono?
DrJokepu
I think that the answer will be the same.The code will be executed under the context of the web server host process.
sagie
+3  A: 

The best option is a config setting.

It's better for testability; it's better because it's an obvious statement of how the code will work, and it's better because it doesn't rely on implementation details; you specifically branch from a value you set. It may be that the decision you are making is non-obvious and your variable/config can be suggestive of the reasoning.

It's the option I'd go for.

Noon Silk
+3  A: 

Trying to solve another problem, I found a good solution for this one. There is a private method System.Configuration.SettingsPropertyValue.IsHostedInAspnet, which does exactly what I need. Being a private method, I do not want to call it (though I could using reflection), but its implementation is trivial:

private bool IsHostedInAspnet()
{
    return (AppDomain.CurrentDomain.GetData(".appDomain") != null);
}

(according to Reflector)

Looks like there is a special key in the app domain data - ".appDomain", which is set when running in ASP.NET web server.

I will stick to that.

mark
I really think this is terrible. It's using an implementation detail that may change. Does it work on Mono? I can't imagine why you wouldn't do the config option. Each to his own.
Noon Silk
A: 

I think the very best thing to do is to split up the library into 2 dlls, one that's truly generic, and one (the one that requires ASP.NET) that only gets loaded in the website. Checking if it's currently running in ASP.NET feels like a bit of a hack.

Ofcourse it depends on the situation wether or not you can easily do this.

Sander Rijken