码迷,mamicode.com
首页 > Windows程序 > 详细

如何:调试 Windows 服务应用程序

时间:2021-03-31 12:23:33      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:effect   version   ntop   links   other   警告   你知道   tar   var   

如何:调试 Windows 服务应用程序

https://docs.microsoft.com/zh-cn/dotnet/framework/windows-services/how-to-debug-windows-service-applications 官网地址

				<main id="main" role="main" class="content " data-bi-name="content" lang="zh-cn" dir="ltr">



					<h1 id="how-to-debug-windows-service-applications"><span data-ttu-id="9309a-103">如何:调试 Windows 服务应用程序</span><span class="sxs-lookup"><span data-stu-id="9309a-103">How to: Debug Windows Service Applications</span></span></h1>

					<ul class="metadata page-metadata" data-bi-name="page info" lang="zh-cn" dir="ltr">
						<li>
							<time class="" data-article-date="" aria-label="文章评审日期" datetime="2017-03-30T00:00:00.000Z" data-article-date-source="ms.date">2017/03/30</time>
						</li>
							<li class="contributors-holder">
								<ul class="facepile has-margin-left-none is-small" data-bi-name="contributors">
													<li class="facepile-item">
														<a href="https://github.com/gewarren" title="gewarren" aria-label="gewarrenGithub 个人资料" data-src="https://github.com/gewarren.png?size=32" data-contributor-name="gewarren" class="facepile-item-coin">
															<img class="facepile-item-coin-image durable-image has-error" aria-hidden="true" src="https://github.com/gewarren.png?size=32" role="presentation" onerror="this.className=‘facepile-item-coin-image durable-image has-error‘;">
															<span class="facepile-item-coin-text durable-image-fallback facepile-item-teal" aria-hidden="true">g</span>
														</a>
													</li>
													<li class="facepile-item">
														<a href="https://github.com/olprod" title="olprod" aria-label="olprodGithub 个人资料" data-src="https://github.com/olprod.png?size=32" data-contributor-name="olprod" class="facepile-item-coin">
															<img class="facepile-item-coin-image durable-image has-error" aria-hidden="true" src="https://github.com/olprod.png?size=32" role="presentation" onerror="this.className=‘facepile-item-coin-image durable-image has-error‘;">
															<span class="facepile-item-coin-text durable-image-fallback facepile-item-red" aria-hidden="true">o</span>
														</a>
													</li>
													<li class="facepile-item">
														<a href="https://github.com/Saisang" title="Saisang" aria-label="SaisangGithub 个人资料" data-src="https://github.com/Saisang.png?size=32" data-contributor-name="Saisang" class="facepile-item-coin">
															<img class="facepile-item-coin-image durable-image has-error" aria-hidden="true" src="https://github.com/Saisang.png?size=32" role="presentation" onerror="this.className=‘facepile-item-coin-image durable-image has-error‘;">
															<span class="facepile-item-coin-text durable-image-fallback facepile-item-blue" aria-hidden="true">S</span>
														</a>
													</li>
								</ul>
							</li>
					</ul>

					<nav id="center-doc-outline" class="doc-outline is-hidden-desktop" data-bi-name="intopic toc" role="navigation" aria-label="文章大纲">
						<h3>本文内容</h3>
					<ol><li><a href="#debugging-tips-for-windows-services">Windows 服务的调试提示</a></li><li><a href="#see-also">请参阅</a></li></ol></nav>

					<!-- <content> -->
						<p><span data-ttu-id="9309a-104">必须从服务控制管理器的上下文中而不是 Visual Studio 中运行服务。</span><span class="sxs-lookup"><span data-stu-id="9309a-104">A service must be run from within the context of the Services Control Manager rather than from within Visual Studio.</span></span> <span data-ttu-id="9309a-105">因此,调试服务不像调试其他 Visual Studio 应用程序类型一样简单。</span><span class="sxs-lookup"><span data-stu-id="9309a-105">For this reason, debugging a service is not as straightforward as debugging other Visual Studio application types.</span></span> <span data-ttu-id="9309a-106">要调试服务,必须启动该服务,然后将调试器附加到该服务正在其中运行的进程中。</span><span class="sxs-lookup"><span data-stu-id="9309a-106">To debug a service, you must start the service and then attach a debugger to the process in which it is running.</span></span> <span data-ttu-id="9309a-107">然后你可以使用所有 Visual Studio 的标准调试功能来调试你的应用程序。</span><span class="sxs-lookup"><span data-stu-id="9309a-107">You can then debug your application by using all of the standard debugging functionality of Visual Studio.</span></span></p>

注意

除非你知道该进程是什么进程并了解进行附加和可能终止该进程的后果,否则你不应该附加到进程。You should not attach to a process unless you know what the process is and understand the consequences of attaching to and possibly killing that process. 例如,如果你附加到 WinLogon 进程然后停止调试,系统将停止,因为系统没有 WinLogon 无法运行。For example, if you attach to the WinLogon process and then stop debugging, the system will halt because it can’t operate without WinLogon.

你仅可将调试器附加到正在运行的服务。You can attach the debugger only to a running service. 附加过程会中断服务的当前运行;它实际不会停止或暂停服务的处理。The attachment process interrupts the current functioning of your service; it doesn’t actually stop or pause the service‘s processing. 也就是说,如果当你开始调试时你的服务正在运行,在你调试它时它在技术上仍处于启动状态,但是它的处理已经挂起。That is, if your service is running when you begin debugging, it is still technically in the Started state as you debug it, but its processing has been suspended.

附加到进程后,你可以设置断点并使用它们来调试你的代码。After attaching to the process, you can set breakpoints and use these to debug your code. 退出你用于附加到进程的对话框后,你实际上处于调试模式。Once you exit the dialog box you use to attach to the process, you are effectively in debug mode. 你可以使用服务控制管理器来启动、停止、暂停和继续你的服务,从而命中你设置的断点。You can use the Services Control Manager to start, stop, pause and continue your service, thus hitting the breakpoints you‘ve set. 调试成功后,你可稍后删除该虚拟服务。You can later remove this dummy service after debugging is successful.

本文介绍了调试在本地计算机上运行的服务,但你也可以调试在远程计算机上运行的 Windows 服务。This article covers debugging a service that‘s running on the local computer, but you can also debug Windows Services that are running on a remote computer. 请参阅远程调试See Remote Debugging.

备注

调试 OnStart 方法会比较困难,因为服务控制管理器对所有启动服务的尝试施加了 30 秒的限制。Debugging the OnStart method can be difficult because the Services Control Manager imposes a 30-second limit on all attempts to start a service. 有关详细信息,请参阅疑难解答:调试 Windows 服务For more information, see Troubleshooting: Debugging Windows Services.

警告

若要获取有意义的调试信息,Visual Studio 调试器需要找到正在调试的二进制文件的符号文件。To get meaningful information for debugging, the Visual Studio debugger needs to find symbol files for the binaries that are being debugged. 如果你正在调试你在 Visual Studio 中生成的服务,符号文件(.pdb 文件)就在与可执行文件或库相同的文件夹中,调试器会自动加载它们。If you are debugging a service that you built in Visual Studio, the symbol files (.pdb files) are in the same folder as the executable or library, and the debugger loads them automatically. 如果你正在调试你没有生成过的服务,你应该首先查找该服务的符号文件,确保调试器可找到它们。If you are debugging a service that you didn‘t build, you should first find symbols for the service and make sure they can be found by the debugger. 请参阅在 Visual Studio 调试器中指定符号 (.pdb) 和源文件See Specify Symbol (.pdb) and Source Files in the Visual Studio Debugger. 如果你正在调试系统进程,或想要你的服务中的系统调用符号,你应该添加 Microsoft 符号服务器。If you‘re debugging a system process or want to have symbols for system calls in your services, you should add the Microsoft Symbol Servers. 请参阅调试符号See Debugging Symbols.

调试服务To debug a service

  1. 在调试配置中生成你的服务。Build your service in the Debug configuration.

  2. 安装你的服务。Install your service. 有关详细信息,请参阅如何:安装和卸载服务For more information, see How to: Install and Uninstall Services.

  3. 从 “服务控制管理器”、“服务器资源管理器”或代码启动服务。Start your service, either from Services Control Manager, Server Explorer, or from code. 有关详细信息,请参阅如何:启动服务For more information, see How to: Start Services.

  4. 使用管理凭据启动 Visual Studio,那么你就能附加到系统进程。Start Visual Studio with administrative credentials so you can attach to system processes.

  5. (可选)在 Visual Studio 菜单栏上,选择“工具”、“选项” 。(Optional) On the Visual Studio menu bar, choose Tools, Options. 在“选项”对话框中,选择“调试”、“符号”,选择“Microsoft 符号服务器”复选框,然后选择“确定”按钮 。In the Options dialog box, choose Debugging, Symbols, select the Microsoft Symbol Servers check box, and then choose the OK button.

  6. 在菜单栏上,从“调试” 或“工具” 菜单选择“附加到进程” 。On the menu bar, choose Attach to Process from the Debug or Tools menu. (键盘:Ctrl+Alt+P)(Keyboard: Ctrl+Alt+P)

    这将显示“进程” 对话框。The Processes dialog box appears.

  7. 选择“显示所有用户的进程”复选框 。Select the Show processes from all users check box.

  8. 在“可用进程” 部分,为服务选择进程,然后选择“附加” 。In the Available Processes section, choose the process for your service, and then choose Attach.

    提示

    该进程的名称将与你的服务的可执行文件相同。The process will have the same name as the executable file for your service.

    出现 “附加到进程” 对话框。The Attach to Process dialog box appears.

  9. 选择相应的选项,然后选择“确定” 以关闭对话框。Choose the appropriate options, and then choose OK to close the dialog box.

    备注

    你现在处于调试模式。You are now in debug mode.

  10. 设置任意你想要在代码中使用的断点。Set any breakpoints you want to use in your code.

  11. 访问服务控制管理器并操作你的服务,发送停止、暂停和继续命令以命中你的断点。Access the Services Control Manager and manipulate your service, sending stop, pause, and continue commands to hit your breakpoints. 有关运行服务控制管理器的详细信息,请参阅如何:启动服务For more information about running the Services Control Manager, see How to: Start Services. 请参见疑难解答:调试 Windows 服务Also, see Troubleshooting: Debugging Windows Services.

Windows 服务的调试提示Debugging Tips for Windows Services

附加到服务的进程使你可调试该服务的大多数代码,但不是全部。Attaching to the service‘s process allows you to debug most, but not all, the code for that service. 例如,由于服务已经启动,你无法调试服务的 OnStart 方法中的代码或 Main 方法中用于在此处加载服务的代码。For example, because the service has already been started, you cannot debug the code in the service‘s OnStart method or the code in the Main method that is used to load the service this way. 避免这种限制的一种方法是在你的仅用于辅助调试的服务应用程序中创建一个临时辅助服务。One way to work around this limitation is to create a temporary second service in your service application that exists only to aid in debugging. 你可以安装两个服务,然后启动该虚拟服务来加载服务进程。You can install both services, and then start this dummy service to load the service process. 临时服务启动进程后,可以使用 Visual Studio 中的 “调试”菜单来附加到服务进程。Once the temporary service has started the process, you can use the Debug menu in Visual Studio to attach to the service process.

尝试添加对 Sleep 方法的调用延迟操作,直到你能够附加到该进程。Try adding calls to the Sleep method to delay action until you’re able to attach to the process.

尝试更改常规控制台应用程序的程序。Try changing the program to a regular console application. 为此,请按如下所示重写 Main 方法,这样它可以作为 Windows 服务和控制台应用程序运行,具体取决于启动方式。To do this, rewrite the Main method as follows so it can run both as a Windows Service and as a console application, depending on how it‘s started.

如何:将 Windows 服务作为控制台应用运行How to: Run a Windows Service as a console application

  1. 向你运行 OnStartOnStop 方法的服务添加一个方法:Add a method to your service that runs the OnStart and OnStop methods:

    C#
    internal void TestStartupAndStop(string[] args)  
    {  
        this.OnStart(args);  
        Console.ReadLine();  
        this.OnStop();  
    }  
    
  2. 按如下所示重写 Main 方法:Rewrite the Main method as follows:

    C#
    static void Main(string[] args)  
    {  
        if (Environment.UserInteractive)  
        {  
            MyNewService service1 = new MyNewService(args);  
            service1.TestStartupAndStop(args);  
        }  
        else  
        {  
            // Put the body of your old Main method here.  
        }  
    }
    
  3. 在项目属性的“应用程序” 选项卡中,将“输出类型” 设置为“控制台应用程序” 。In the Application tab of the project‘s properties, set the Output type to Console Application.

  4. 选择“启动调试” (F5)。Choose Start Debugging (F5).

  5. 若要将该程序再次作为 Windows 服务运行,请安装它并像通常启动 Windows 服务一样启动它。To run the program as a Windows Service again, install it and start it as usual for a Windows Service. 不必恢复这些更改。It‘s not necessary to reverse these changes.

在某些情况下,你必须使用 Windows 调试器,比如当你想要调试仅在系统启动时发生的问题时。In some cases, such as when you want to debug an issue that occurs only on system startup, you have to use the Windows debugger. 下载 Windows 驱动程序工具包 (WDK),并查看如何调试 Windows 服务Download the Windows Driver Kit (WDK) and see How to debug Windows Services.

请参阅See also

					<!-- </content> -->

					</main>

					<!-- page rating section -->
							<div class="is-hidden-desktop has-border-top has-margin-top-large has-padding-top-large binary-rating-holder">
								



<div class="feedback-verbatim has-border-bottom has-padding-bottom-large has-margin-bottom-small" data-bi-name="rating">
<div class="binary-rating">
    <div class="binary-rating-buttons">
			<h3 class="has-text-weight-semibold has-margin-top-none has-margin-bottom-small is-size-h4 has-margin-bottom-medium">此页面有帮助吗?</h3>
		<div>
            <button class="thumb-rating like has-inner-focus" title="是" data-bi-name="rating-yes" aria-expanded="false" data-bi-sat="1" aria-controls="rating-container-desktop">
                <span aria-hidden="true" class="icon docon docon-like"></span>
                <span>是</span>
            </button>
            <button class="thumb-rating dislike has-inner-focus" title="否" data-bi-name="rating-no" data-bi-sat="0" aria-expanded="false" aria-controls="rating-container-desktop">
                <span aria-hidden="true" class="icon docon docon-dislike"></span>
                <span>否</span>
            </button>
        </div>
    </div>
    <form class="feedback-verbatim-form is-hidden" id="rating-container-desktop">
        <div class="verbatim-textarea box is-relative has-box-shadow-none has-border has-margin-top-small has-padding-extra-small is-size-extra-small">
            <label for="rating-textarea-desktop" class="visually-hidden">还有其他反馈吗?</label>
            <textarea id="rating-textarea-desktop" rows="4" maxlength="999" placeholder="还有其他反馈吗?" required="" class="textarea has-border-none has-box-shadow-none has-inner-focus"></textarea>
        </div>
        <div class="buttons is-right has-margin-top-medium has-margin-right-extra-small">
            <button class="skip-rating button is-transparent has-text-primary is-small has-border-none" type="button">跳过</button>
            <button class="submit-rating button is-primary is-small" data-bi-name="rating-verbatim" disabled="" type="submit">提交</button>
        </div>
    </form>
</div>
<div class="thankyou-rating is-hidden" tabindex="-1">
    <p>谢谢。</p>
</div>
					<!-- recommendations section -->
					<!-- end recommendations section -->

					<!-- feedback section -->
					<!-- feedback report section -->
					<!-- end feedback report section -->

					<div class="footerContainer is-visible-interactive has-default-focus ">



<footer id="footer-interactive" data-bi-name="footer" class="footer-layout">

<div class="is-flex is-full-height has-padding-right-extra-large-desktop">
		<a data-mscc-ic="false" class="locale-selector-link has-flex-shrink-none" href="/zh-cn/locale?target=https://docs.microsoft.com/zh-cn/dotnet/framework/windows-services/how-to-debug-windows-service-applications" data-bi-name="select-locale"><span class="icon docon docon-world is-size-large has-margin-right-small" aria-hidden="true"></span><span class="local-selector-link-text">中文 (简体)</span></a>
	<div class="has-margin-left-medium has-margin-right-medium has-flex-shrink-none">

如何:调试 Windows 服务应用程序

标签:effect   version   ntop   links   other   警告   你知道   tar   var   

原文地址:https://www.cnblogs.com/sunny3158/p/14598806.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!