Determine whether running in ASP.NET or WinForms/console without System.Web

From a class library, I need to determine at run-time whether I’m running in an ASP.NET app or a WinForms/console app. There have been several other questions asked on the subject, but all of those solutions require adding a reference to System.Web. If at all possible, when running my console and WinForms apps, I don’t want to load the System.Web assembly into memory just for one line of code out of thousands.

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

Use System.Diagnostics.Process.GetCurrentProcess().ProcessName

If you’re running ASP.NET then the assembly will be named thusly:

  • If you are running IIS 6.0 or IIS 7.0, the name is w3wp.exe.
  • If you are running an earlier version of IIS, the name is aspnet_wp.exe.

Another Idea: How about testing the process/app domain for the presence of the System.Web.dll with the AppDomain.CurrentDomain.GetAssemblies() API?

Method 2

One of the questions you linked to contained an answer suggesting Environment.UserInteractive.

You could also try to analyze the StackTrace of your code to figure out where you are called from.

Method 3

You could try something based on Assembly.GetEntryAssembly(). As noted in the comment below, GetEntryAssembly() returns NULL if the current code is being run in the context of a web application or service. It will return some non-null reference in cases of standalone apps such as WinForm or console apps.

Edited to change the original answer because of the comment.

Method 4

Yet another hack:

System.Configuration.ConfigurationManager.OpenExeConfiguration throws an ArgumentException with a specific message if you’re not running inside a standalone exe. You could use that fact to check in this manner:

bool notAnExe = false ;
try
{
    // see if we're running in an exe.
    Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
}
catch(ArgumentException aex)
{
    if(aex.Message == "exePath must be specified when not running inside a stand alone exe.")
    {
        notAnExe = true ;
    }
}

Method 5

You can check System.Diagnostics.Process.GetCurrentProcess().ProcessName;. If it starts with aspnet, then it’s asp.net. Otherwise, desktop.

Method 6

This is an old thread, but here is new answer that isn’t a hack.

private bool IsExe()
{
  var domainManager = AppDomain.CurrentDomain.DomainManager;
  if (domainManager == null) return false;

  var entryAssembly = domainManager.EntryAssembly;
  if (entryAssembly == null) return false;

  return entryAssembly.Location.EndsWith(".exe", StringComparison.OrdinalIgnoreCase);
}

This will not tell you if the application is ASP.Net or not, but it will tell you if this is a console or WinForms app which is the opposite approach to most of the other answers here. For example if this is an OWIN application the IsExe method will return false even though this is not an ASP.Net application.

Method 7

HostingEnvironment.IsHosted

MSDN Docs

Method 8

It may appear like a hack. It uses the DomainManager type of Current AppDomain. Check also AppDomainManager

public static class AspContext
{
    public static bool IsAspNet()
    {
        var appDomainManager = AppDomain.CurrentDomain.DomainManager;
        return appDomainManager != null && appDomainManager.GetType().Name.Contains("AspNetAppDomainManager");
    }
}

Or you can use this other answer on SO


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