Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I determine if the executing assembly is a web app or winform/console?

I would like to write a helper function which build the exception message to write to a log. The code look like:

if(IsWebApp)
{

      use HttpContext to get the Request Path and RawUrl }
else
{
      //else it a winform/console
      Use Assembly to get executing path.

}

like image 646
archaictree Avatar asked Apr 16 '10 15:04

archaictree


2 Answers

Use the HttpRuntime class:

if (!String.IsNullOrEmpty(HttpRuntime.AppDomainAppVirtualPath))
    //ASP.Net
else 
    //Non-ASP.Net
like image 109
SLaks Avatar answered Sep 23 '22 08:09

SLaks


Just check for some object that only exists in a web application, like HttpRuntime.AppVirtualPath that SLaks suggested.

If it's a web application, you would still want to check if HttpContext.Current is null. If the exception occurs in code that is not run beacuse of a request, it doesn't have any context. The Session_OnEnd event for example runs when a server session is removed, so it doesn't have the context.

like image 33
Guffa Avatar answered Sep 22 '22 08:09

Guffa