Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How determine if application is web application

Tags:

.net

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?

like image 296
Andreas Folkesson Avatar asked Jul 05 '10 13:07

Andreas Folkesson


People also ask

How can you tell if a website is a web app?

A website is a group of globally accessible, interlinked web pages which have a single domain name. A web application is a software or program which is accessible using any web browser. Developing your website helps you in branding your business.

What is considered a web application?

A Web application (Web app) is an application program that is stored on a remote server and delivered over the Internet through a browser interface. Web services are Web apps by definition and many, although not all, websites contain Web apps.

What is the difference between application and web application?

The difference is that a web application will reside in the server instead of the client's computer and will process everything in the server - but , the control of the program is left for the client. A good example for a web application is this site.

Can an application be web based?

Web-based applications often run inside a web browser. However, web-based applications also may be client-based, where a small part of the program is downloaded to a user's desktop, but processing is done over the internet on an external server. Web-based applications are also known as web apps.


2 Answers

if(HttpRuntime.AppDomainAppId != null) {   //is web app } else {   //is windows app } 
like image 126
Paul Avatar answered Sep 25 '22 02:09

Paul


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.

like image 30
Ruben Avatar answered Sep 24 '22 02:09

Ruben