作法:偵錯 Windows 服務應用程式How to: Debug Windows Service Applications

服務必須從服務控制管理員內容之中執行,而不是從 Visual Studio 之中執行。A service must be run from within the context of the Services Control Manager rather than from within Visual Studio. 因此,對服務進行偵錯不像是對其他 Visual Studio 應用程式類型進行偵錯那樣簡單直接。For this reason, debugging a service is not as straightforward as debugging other Visual Studio application types. 若要對服務進行偵錯,您必須啟動服務,然後將偵錯工具附加至執行中的處理序。To debug a service, you must start the service and then attach a debugger to the process in which it is running. 之後就可以使用 Visual Studio 所有的標準偵錯功能,對應用程式進行偵錯。You can then debug your application by using all of the standard debugging functionality of Visual Studio.

警告

除非您知道處理序的內容,並了解附加至處理序以及可能會刪除該處理序的後果,否則就不應該附加至處理序。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:

    internal void TestStartupAndStop(string[] args)  
    {  
        this.OnStart(args);  
        Console.ReadLine();  
        this.OnStop();  
    }  
    
  2. 依照下列方式重寫 Main 方法:Rewrite the Main method as follows:

    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