【发布时间】:2016-09-10 20:05:19
【问题描述】:
我目前正在 Linux 上开发单声道服务应用程序。现在我想将单声道调试器附加到我正在运行的服务中。如何找到服务流程?如何将调试器附加到它?
问候
【问题讨论】:
我目前正在 Linux 上开发单声道服务应用程序。现在我想将单声道调试器附加到我正在运行的服务中。如何找到服务流程?如何将调试器附加到它?
问候
【问题讨论】:
Mikael Chudinov 有一篇很棒的文章在这里解释它:http://blog.chudinov.net/demonisation-of-a-net-mono-application-on-linux/
从它作为基础开始,我创建了一个通用基类,它可以让您动态检查是否附加了调试器,或者让您调试代码,或者在未调试时正常启动单声道服务。
基类是:
using System;
using NLog;
using System.ServiceProcess;
public class DDXService : ServiceBase
{
protected static Logger logger = LogManager.GetCurrentClassLogger();
protected static void Start<T>(string[] args) where T : DDXService, new()
{
if (System.Diagnostics.Debugger.IsAttached)
{
logger.Debug("Running in DEBUG mode");
(new T()).OnStart(new string[1]);
ServiceBase.Run(new T());
}
else
{
logger.Debug("Running in RELEASE mode");
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[] { new T() };
ServiceBase.Run(ServicesToRun);
} //if-else
}
}
要使用这个基类,只需从它继承并覆盖 System.ServiceProcess.ServiceBase 方法 OnStart 和 OnStop。将您的 Main 方法放在这里以启动 init 序列:
class Service : DDXService
{
protected override void OnStart(string[] args)
{
//Execute your startup code
//You can place breakpoints and debug normally
}
protected override void OnStop()
{
//Execute your stop code
}
public static void Main(string[] args)
{
DDXService.Start<Service>(args);
}
}
希望这会有所帮助。
【讨论】: