如何在启动之前附加到Windows服务?

anm*_*rti 1 .net windows-services visual-studio-2010

我需要调试Windows服务的初始化.我有一个在服务初始化期间调用的方法,但由于它在启动时发生,我没有足够的时间使用Visual Studio附加进程.此外,由于服务未启动,因此VS2010的"附加过程"窗口对话框中没有显示.

所以我需要的是通过在启动之前附加到它来调试服务.可能吗?

Joe*_*zer 5

添加设置文件或配置设置以切换调试并尝试以下操作:

protected override void OnStart(string[] args)
{
    if (Settings.Default.DebugApplication)
    {
        System.Diagnostics.Debugger.Launch();
    }
    // the rest of the code goes here
}
Run Code Online (Sandbox Code Playgroud)