Can't debug Windows Service -"attach to process"

23,496

Solution 1

You must do the following:

  1. Open your solution in Visual Studio Administrator mode.
  2. Make sure your service is running.
  3. Open the "Attach to process window"
  4. Make sure both checkboxes are checked (all users, all sessions).
  5. Find the name of your executable in the list.

If the above doesn't work, you will need to provide some additional details about your setup. :-)

Solution 2

You may add somewhere in service, for example in OnStart method this code:

#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

Share:
23,496
boruchsiper
Author by

boruchsiper

ASP.NET Developer

Updated on December 24, 2020

Comments

  • boruchsiper
    boruchsiper over 3 years

    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.

    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.

    enter image description here

    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.

  • boruchsiper
    boruchsiper over 11 years
    I started VS as administrtor but unfortunately I still couldn't see the service in the list.
  • adnan
    adnan over 8 years
    this works me to debug the function in service start, i just open the solution in visual studio to run as Administrator. then i add System.Diagnostics.Debugger.Launch(); in Service Start function. thanks @Yuriy