【问题标题】:ASP.Net Core, detecting debugging vs. not debugging in a controllerASP.Net Core,检测调试与不在控制器中调试
【发布时间】:2017-03-14 01:01:53
【问题描述】:

我正在编写我的第一个 ASP.Net 代码 Web 应用程序,并且在我的控制器中我希望有一个 if 语句来检查我是否处于调试模式。我知道在 Startup.cs 文件中我可以检查 env.IsDevelopment() 但那是因为 IHostingEnvironment 被传递到其中。我无法找到在普通控制器中检查此状态的方法。 ASP.Net Core 中是否有办法检测我何时在我丢失的控制器内处于调试模式?

【问题讨论】:

    标签: c# asp.net-core asp.net-core-mvc


    【解决方案1】:

    更新:@Pradeep Kumar 的 post below 是这里更正确的答案。此答案仅指示如何通过控制器中的依赖注入访问 IsDevelopment() 环境标志。

    更新: IHostingEnvironment 在 .Net Core 3.1 中已过时,请参阅以下 .Net Core 3.1+ https://stackoverflow.com/a/61703339/2525561

    您应该能够将 IHostingEnvironment 注入到您的控制器构造函数中。

    protected readonly IHostingEnvironment HostingEnvironment;
    
    public TestController(IConfiguration configuration, IHostingEnvironment hostingEnv){
        this.Configuration = configuration;
        this.HostingEnvironment = hostingEnv;
    }
    
    [HttpGet]
    public IActionResult Test(){
        if(this.HostingEnvironment.IsDevelopment()){
            // Do something
        }
    
        return View();
    }
    

    【讨论】:

    • 感谢工作。我是 Core 和 DI 的新手,所以我什至没有想到。
    • 哇,灯泡终于亮了,内置了 DI。我仍然不清楚我是如何知道可以注入托管环境的(我知道,RTFM)。感谢您提供这个直截了当的示例。
    • 这只告诉你环境..如果你有一个开发服务器,你将代码部署到它会显示正确,但它不是最初问题的调试环境..
    【解决方案2】:

    IHostingEnvironment 让您了解应用程序运行的环境。看起来您需要的是用于构建应用程序的构建配置,即调试/发布。在 ASP.NET Core Web 应用程序中,为了在编译时获取此信息,没有直接的方法,但是您可以使用编译器指令获得具有条件编译的属性,例如

    public static bool IsDebug
    {
      get
         {
          bool isDebug = false;
        #if DEBUG
           isDebug = true;
        #endif
           return isDebug;
          }
    }
    

    在运行时,您可以检查 IsDebug 属性的值以确定构建配置。我建议将此属性添加到可以从所有控制器访问的通用静态或实用程序类中。

    【讨论】:

    • 这是正确的方法。谢谢
    【解决方案3】:

    现在不是IHostingEnvironment,而是IWebHostingEnvironment。 在 ASP.NET Core 3.1 中,IHostingEnvironment 会导致警告

    CS0618  'IHostingEnvironment' is obsolete: 'This type is obsolete 
    and will be removed in a future version. 
    The recommended alternative is 
    Microsoft.AspNetCore.Hosting.IWebHostEnvironment.'
    

    因此,接受的答案应更新如下:

    using Microsoft.AspNetCore.Hosting;
    using Microsoft.Extensions.Hosting;
    
    
    public class TestController : Controller
    {
        protected readonly IWebHostEnvironment HostEnvironment;
    
        public TestController(IWebHostEnvironment hostEnv) {
            this.HostEnvironment = hostEnv;
        }
    
        [HttpGet]
        public IActionResult Test(){
            if (this.HostEnvironment.IsDevelopment()){
                // Do something
            }
    
            return View();
        }
    }
    

    【讨论】:

    • 对我来说很好:)
    • Optimax 的解决方案只告诉您环境。如果正在调试,则不会。
    • @roblem 添加了替代答案。
    【解决方案4】:

    由用户@roblem 的评论提示并基于对 OP 问题的另一种解读,我自己的答案的替代方案如下:

    托管调试器

    要检测托管 (.NET) 调试器是否实际上附加到进程,可以使用System.Diagnostics.Debugger.IsAttached 属性。当您需要以编程方式确定您的代码是从 IDE 运行还是独立运行时,这在开发过程中很有用。

    using System.Diagnostics;
    
    if (Debugger.IsAttached)
    {
        // Debugger is attached, we are probably running from the IDE
    }
    else
    {
        // We are probably running standalone
    }
    

    请注意,上述内容仅适用于托管调试器,例如内置于 IDE 中的调试器。

    非托管调试器

    要检测是否附加了非托管调试器,需要调用CheckRemoteDebuggerPresent WinAPI 函数:

    using System;
    using System.Diagnostics;
    using System.Runtime.InteropServices;
    
    public class DetectDebugger
    {
        [DllImport("kernel32.dll", SetLastError = true, ExactSpelling = true)]
        static extern bool CheckRemoteDebuggerPresent(IntPtr hProcess, ref bool isDebuggerAttached);
    
        public static void Main()
        {
            bool isDebuggerAttached = false;
            CheckRemoteDebuggerPresent(Process.GetCurrentProcess().Handle, ref isDebuggerAttached);
    
            Console.WriteLine("Debugger is attached: " + isDebuggerAttached);
            // Prevent the console window from immediately closing:
            Console.ReadLine();
        }
    }
    

    总结

    总结(发现here):

    • CheckRemoteDebuggerPresent - 适用于任何正在运行的进程,也可以检测本机调试器。
    • Debugger.IsAttached - 仅适用于当前进程并仅检测托管调试器。例如,OllyDbg 不会被此检测到。

    【讨论】:

      猜你喜欢
      • 2017-12-12
      • 1970-01-01
      • 2020-05-22
      • 1970-01-01
      • 2018-05-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-29
      相关资源
      最近更新 更多