Compartilhar via


How to: Debug the OnStart Method

Applies to: yesVisual Studio noVisual Studio for Mac

Note

This article applies to Visual Studio 2017. If you're looking for the latest Visual Studio documentation, see Visual Studio documentation. We recommend upgrading to the latest version of Visual Studio. Download it here

You can debug a Windows service by starting the service and attaching the debugger to the service process. For more information, see How to: Debug Windows Service Applications. However, to debug the System.ServiceProcess.ServiceBase.OnStart method of a Windows service, you must launch the debugger from inside the method.

  1. Add a call to Launch at the beginning of the OnStart()method.

    protected override void OnStart(string[] args)
    {
        System.Diagnostics.Debugger.Launch();
    }
    
  2. Start the service (you can use net start, or start it in the Services window).

    You should see a dialog box like the following:

    Screenshot of a Visual Studio Just-In-Time Debugger dialog box that shows an unhandled .NET Framework exception occurred in WindowsService-Asis.exe.

  3. Select Yes, debug <service name>.

  4. In the Just-In-Time Debugger window, select the version of Visual Studio you want to use for debugging.

    Screenshot of a Visual Studio Just-In-Time Debugger window with 'New instance of Microsoft Visual Studio 2015' selected in the list of Possible Debuggers.

  5. A new instance of Visual Studio starts, and execution is stopped at the Debugger.Launch() method.

See also