.net 无法调试 Windows 服务 -“附加到进程”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12960268/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can't debug Windows Service -"attach to process"
提问by boruchsiper
Microsoft instructions state the following
In Visual Studio, choose Attach to Process from the Debug menu.
The Processes dialog box appears.
Click Show system processes.
In the Available Processes section, click the process for your service, and then click Attach.
在 Visual Studio 中,从“调试”菜单中选择“附加到进程”。
出现进程对话框。
单击显示系统进程。
在“可用进程”部分中,单击您的服务的进程,然后单击“附加”。
However, I don't see the "Show system processes." option. I've tried checking off the other options but my service doesn't show up in the list.
但是,我没有看到“显示系统进程”。选项。我试过检查其他选项,但我的服务没有显示在列表中。


Yes, I've installed and started the services and can see it in the in service manager.
是的,我已经安装并启动了服务,并且可以在服务管理器中看到它。
I'm also aware that It's not possible to debug onstart. However, I have a continuous loop running.
我也知道不可能在开始时调试。但是,我有一个连续循环运行。
回答by theMayer
You must do the following:
您必须执行以下操作:
- Open your solution in Visual Studio Administrator mode.
- Make sure your service is running.
- Open the "Attach to process window"
- Make sure both checkboxes are checked (all users, all sessions).
- Find the name of your executable in the list.
- 在 Visual Studio 管理员模式下打开您的解决方案。
- 确保您的服务正在运行。
- 打开“附加到进程窗口”
- 确保选中两个复选框(所有用户、所有会话)。
- 在列表中找到可执行文件的名称。
If the above doesn't work, you will need to provide some additional details about your setup. :-)
如果上述方法不起作用,您将需要提供一些有关您的设置的其他详细信息。:-)
回答by Yuriy Rozhovetskiy
You may add somewhere in service, for example in OnStart method this code:
您可以在服务中的某处添加,例如在 OnStart 方法中,此代码:
#if DEBUG
System.Diagnostics.Debugger.Launch();
#endif
On service start, you will get a prompt dialog to debug process. Don't forget to build in Debug mode and switch to Release in production
在服务启动时,您将看到一个提示对话框来调试过程。不要忘记在调试模式下构建并在生产中切换到发布
回答by AgentFire
Actually to allow the program to get the list of the processes of every user on the machine, that program must have administrative rights. Ensure you start visual studio with the corresponding account.
实际上,要允许程序获取机器上每个用户的进程列表,该程序必须具有管理权限。确保使用相应的帐户启动 Visual Studio。
回答by Mike de Klerk
Make sure selected code typesettings are correct. You manage them by clicking the [Select...]button next to "Attach to:". In the image you attached, you have set it to "Automatic: Native code". Try some other settings. Note:Press [Refresh]button after you have changed the setting to update the list of processes shown.
确保选择的代码类型设置正确。您可以通过单击“附加到:”旁边的[选择...]按钮来管理它们。在您附加的图像中,您已将其设置为“自动:本机代码”。尝试一些其他设置。 注意:更改设置后按[Refresh]按钮以更新显示的进程列表。
回答by Steve
The name of the service might be different with the name of the process. The name of the process is probably the name of the service exe file. you can check this in task manager or in your windows service vs project. (the name that appears in the Attach Process window is the name of the process not the name of the service)
服务名称可能与进程名称不同。进程的名字大概就是服务exe文件的名字。您可以在任务管理器或 Windows 服务与项目中进行检查。(Attach Process 窗口中出现的名称是进程的名称而不是服务的名称)

