【问题标题】:How to determine if .NET code is running in an ASP.NET process?如何确定 .NET 代码是否在 ASP.NET 进程中运行?
【发布时间】:2008-10-16 18:56:40
【问题描述】:

我有一个通用类的实例,它将在以下两种情况下执行 ASP.NET 和一个独立的程序。此代码对其所在的过程很敏感 正在运行 - 也就是说,有一些不应该调用 if 的 certin 方法 在 ASP.NET 下运行。如何确定代码是否在 ASP.NET 中执行 过程?

下面回答我目前使用的解决方案。


我希望有人能就这个问题为何被否决和/或提出更好的提问方式发表评论!我只能假设至少有些人已经看过这个问题并说“真是个白痴,ASP.NET 代码就是 .NET 代码”。

【问题讨论】:

标签: c# .net asp.net


【解决方案1】:

如果您使用异步方法,则 ASP.NET 中的 HttpContext.Current 也可以为 null,因为异步任务发生在不共享原始线程的 HttpContext 的新线程中。这可能是也可能不是您想要的,但如果不是,那么我相信 HttpRuntime.AppDomainAppId 在 ASP.NET 进程中的任何地方都将为非空,而在其他地方则为空。

【讨论】:

  • 使用HttpRuntiime.AppDomainAppId 与使用HostingEnvironment.IsHostedghigad's answer 相比有什么优势?
【解决方案2】:

试试这个:

using System.Web.Hosting;

// ...

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

为我工作!

详情请见HostingEnvironment.IsHosted...

【讨论】:

    【解决方案3】:

    我认为您真正想做的是重新考虑您的设计。更好的方法是使用 Factory 类,该类根据应用程序的启动方式生成您需要的不同版本的类(旨在实现接口,以便您可以互换使用它们)。这会将代码本地化,以便在一处检测基于 Web 和非 Web 的使用情况,而不是将其分散在您的代码中。

    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 ...
        }
    }
    

    【讨论】:

    • 不错的想法和复杂的方法,我需要的是在 ASP.NET 下运行时调用少量方法时抛出异常。
    • 如果定义是“在 IIS 进程中运行”而不是“使用当前/有效的 IIS 请求”(这在某些情况下确实很重要)。所以最后有很多代码显示HttpContext.Current != null有上面提到的问题。我不反对这样的设计,但它与原始问题相切,需要考虑具体的上下文。
    【解决方案4】:
    using System.Diagnostics; 
    
    if (Process.GetCurrentProcess().ProcessName == "w3wp")
        //ASP.NET
    

    【讨论】:

    • 请使用edit链接解释这段代码是如何工作的,不要只给出代码,因为解释更有可能帮助未来的读者。另见How to Answersource
    【解决方案5】:

    这是我对这个问题的回答。

    首先,确保您的项目引用 System.Web 并且您的代码文件是“使用 System.Web;”。

    public class SomeClass {
    
      public bool  RunningUnderAspNet    { get; private set; }
    
    
      public SomeClass()
        //
        // constructor
        //
      {
        try {
          RunningUnderAspNet = null != HttpContext.Current;
        }
        catch {
          RunningUnderAspNet = false;
        }
      }
    }
    

    【讨论】:

      【解决方案6】:
      If HttpContext Is Nothing OrElse HttpContext.Current Is Nothing Then
        'Not hosted by web server'
      End If
      

      【讨论】:

      • HttpContext是一个类的名字,所以HttpContext不能为null。
      猜你喜欢
      • 1970-01-01
      • 2018-11-29
      • 2012-09-17
      • 2012-02-22
      • 2010-12-08
      • 1970-01-01
      • 2019-10-17
      • 1970-01-01
      相关资源
      最近更新 更多