29

In a core assembly, which is run both in a windows service, and in a web application, I need to store information per user session. The service will have a single user session, and the web application uses HttpContext.Current.

I want to configure which method to use within the core assembly - convention over configuration. I want to do this only once, and I believe HttpContext.Current will be null when run from Application_Start.

How can I reliably determine if the application is a web application?

Andreas Folkesson
  • 475
  • 1
  • 4
  • 10

5 Answers5

45
if(HttpRuntime.AppDomainAppId != null)
{
  //is web app
}
else
{
  //is windows app
}
M4N
  • 94,805
  • 45
  • 217
  • 260
Paul
  • 466
  • 4
  • 2
  • 5
    HttpRuntime is a good solution for framework apps, but System.Web doesn't exist in core / standard st this point in time. – Roger Hill Aug 28 '19 at 23:24
18

I'd go for

HostingEnvironment.IsHosted

Note that there is a slight overhead incurred when you're using a method from an assembly like this, even when you don't intend to use it. (System.Web will be loaded and several classes could be initialized and JITed.) Also, there's a hard dependency on System.Web now, so you can't use it in a limited framework setting (currently IIRC only with the Client Profile).

Another way (although not as neat and documented), is to check

Path.GetFileName(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile)

If this returns web.config (or a casing variant thereof), it's probably a web application. (Although you can setup any appdomain with a config file named web.config, this is not a likely scenario.) This avoids taking a dependency on System.Web.

However, HostingEnvironment.IsHosted is intended to indicate whether an appdomain is configured to run under ASP.NET.

Ruben
  • 15,217
  • 2
  • 35
  • 45
  • Thank you for the details here. This worked for me when other things did not (trying to detect if it was a gui or a console, especially while working in debug in VS2022). `HostingEnvironment.IsHosted` worked the best for me. – Joshua K Feb 08 '23 at 05:19
3

Just so no one else are making the same mistake as me.

Assembly.GetEntryAssembly() do not work to define if its an web application or a not. When it's running as a service then Assembly.GetEntryAssembly() are null, but when i debug from VS, it's not null.

2

In web application Assembly.GetEntryAssembly() is null. I use it in two libraries and so far it works great.

Stefan Cebulak
  • 169
  • 2
  • 10
  • I second this solution. Just tested it with code running in a web context via ASP.NET and also in a Windows Service. – Scott Koland Sep 29 '16 at 00:46
1

If possible I'd suggest having it as an input parameter to some initialize method in the class library that would need to be called before the class library can be used.

If that's not an option I'd look at HttpRuntime.Cache which I think would be non null even if HttpRuntime.Current is null. I'm not a webforms guy but I remember someone mentioning that for a similar question sometime somewhere (can't find that question now).

Hans Olsson
  • 54,199
  • 15
  • 94
  • 116