简体   繁体   中英

How to determine if .NET code is running in an ASP.NET process?

I have an instance of a general purpose class that will be executed both under ASP.NET and a stand alone program. This code is sensative to the process where it is being run - that is, there are certin methods that should not called if running under ASP.NET. How do you determine if the code is executing in an ASP.NET process?

The solution I am currently using is answered below.


I wish someone would add a comment as to why this question has gotten downvoted and/or propose a better way to ask it! I can only assume at least some folks have looked at the question and said "what an idiot, ASP.NET code is .NET code".

HttpContext.Current can also be null within ASP.NET if you're using asynchronous methods, as the asynchronous task happens in a new thread that doesn't share the HttpContext of the original thread. This may or may not be what you want, but if not then I believe that HttpRuntime.AppDomainAppId will be non-null anywhere in an ASP.NET process and null elsewhere.

Try this:

using System.Web.Hosting;

// ...

if (HostingEnvironment.IsHosted)
{
    // You are in ASP.NET
}
else
{
    // You are in a standalone application
}

Worked for me!

See HostingEnvironment.IsHosted for details...

I think what you really want to do is rethink your design. A better way to do this is to use a Factory class that produces different versions of the classes you need (designed to implement interfaces so you can use them interchangeably) depending on how the application is started. This will localize the code to detect web- and non-web-based usage in one place rather than scattering it all over your code.

public interface IDoFunctions
{
    void DoSomething();
}

public static class FunctionFactory
{
  public static IDoFunctions GetFunctionInterface()
  {
     if (HttpContext.Current != null)
     {
        return new WebFunctionInterface();
     }
     else
     {
        return new NonWebFunctionInterface();
     }
   }
}

public IDoFunctions WebFunctionInterface
{
    public void DoSomething()
    {
        ... do something the web way ...
    }
}

public IDoFunctions NonWebFunctionInterface
{
    public void DoSomething()
    {
        ... do something the non-web way ...
    }
}
using System.Diagnostics; 

if (Process.GetCurrentProcess().ProcessName == "w3wp")
    //ASP.NET

This is my answer to the question.

First, make sure your project references System.Web and that your code file is "using System.Web;".

public class SomeClass {

  public bool  RunningUnderAspNet    { get; private set; }


  public SomeClass()
    //
    // constructor
    //
  {
    try {
      RunningUnderAspNet = null != HttpContext.Current;
    }
    catch {
      RunningUnderAspNet = false;
    }
  }
}
If HttpContext Is Nothing OrElse HttpContext.Current Is Nothing Then
  'Not hosted by web server'
End If

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM