繁体   English   中英

Windows服务调试问题

[英]Windows service Debug Issue

我已经创建了Windows服务,我已经尝试对其进行调试。在“调试”选项卡上,单击“附加到进程”以选择Myservice.exe,它将不会通过断点。

在启动事件服务中,我正在编写以下代码

     protected override void OnStart(string[] args)
    {

        Console.WriteLine("Press Enter to terminate ...");
    }

请帮助我如何解决此问题。

使用以下方法。在您的代码上。 到目前为止,这是在服务库上设置断点的最简单方法。

Debugger.Break();

protected override void OnStart(string[] args)
    {
        Debugger.Break();
        Console.WriteLine("Press Enter to terminate ...");
    }

您还可以使用如下所示的提示来提示在调试模式下附加调试器:

#if DEBUG    
if (!Debugger.IsAttached)
{
    Debugger.Launch();
}    
#endif

您甚至可以在main方法中使用此代码,它将在调试模式下将服务作为普通应用程序运行:

public static void Main()
{
    var service = new YourService();
#if DEBUG
    service.Start();
    Console.ReadLine();
    service.Stop();
#else
    var ServicesToRun = new System.ServiceProcess.ServiceBase[] { service };
    System.ServiceProcess.ServiceBase.Run(ServicesToRun);
#endif
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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