How can a .NET code know whether it is running within a web server application?

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

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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.

Method 2

You can check for

HttpContext.Current

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

See HttpContext.Current Property

Method 3

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.

Method 4

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

Method 5

  • 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

Method 6

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.

Method 7

See the answer I provided here

Basically, you can use HostingEnvironment.IsHosted.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x