Pre-launch task for debugging system service

PeterS 0 Reputation points
2026-01-26T13:30:53.0033333+00:00

Hello,
last days I had to remotely debug early start of system service with Visual Studio 2022.
It seems that only ways how to do it are: running target in console mode - sometimes unusable, or putting Sleep() into service_main and manually attaching debugger.

Debugging properties contains remote command which I can setup like c:\windows\system32\net.exe
and it's arguments: start MyService
but debugger attaches to net.exe process and not to MyService.exe process.

If I remember good some of very old Visual Studio versions was capable for it - it stopped service, deploy it and then started service and attached to service process, but I am not sure how I did it, it seems that debug settings in VS 2017 - VS 2022 does not contain support for it.
It should be good to resolve it by adding pre-launch setting (starting system service) and attaching to started process by it's name.

Does exist some trick how to do it in VS2022 ?

Developer technologies | Visual Studio | Debugging
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Q&A Assist
    2026-01-26T13:31:36.8166667+00:00
    AI generated content. This question contains an answer created with AI from Q&A Assist. Learn more

    To debug a system service in Visual Studio 2022, especially during its early startup, you can consider a few approaches:

    1. Attach to Process: Since you mentioned that the debugger attaches to net.exe instead of your service, you might want to manually attach the debugger to the correct process after starting the service. You can do this by using the Attach to Process feature in Visual Studio, which allows you to select the specific service process once it is running.
    2. Using Sleep(): As you noted, inserting a Sleep() call in the service_main function can provide you with the time needed to attach the debugger manually. This is a common workaround when debugging services.
    3. Console Mode: Running the service in console mode is another option, as it allows for easier debugging. You can modify your service's Main method to check if it is running in an interactive mode and then call the OnStart method directly.
    4. Pre-launch Command: While Visual Studio 2022 may not have the exact pre-launch capabilities you remember from older versions, you can still set up a remote command in the debugging properties. However, ensure that you are attaching to the correct process after the service starts.
    5. Remote Debugging: If you're debugging remotely, ensure that the setup allows for the correct connection to the service process. You might need to check the remote debugging configuration to ensure it's set up correctly.

    Unfortunately, there may not be a direct way in Visual Studio 2022 to automatically stop, deploy, and start the service while attaching to the service process as you described. You may need to rely on the manual methods mentioned above.


    References:

    0 comments No comments

  2. Varsha Dundigalla(INFOSYS LIMITED) 4,500 Reputation points Microsoft External Staff
    2026-01-27T10:38:46.5266667+00:00

    Thank you for reaching out.

    What you are trying to do is start a Windows service as part of debugging and have Visual Studio automatically attach to the service process so you can debug early startup code.

    In Visual Studio 2022, this is not supported. When you configure debugging to run net.exe start MyService, Visual Studio correctly attaches to net.exe, because it always attaches to the process it launches. It does not automatically attach to a different process (your service executable) that net.exe starts.

    Because of this design, Visual Studio cannot start a Windows service and then switch the debugger to the service process by name. That is why your current setup always ends up attached to net.exe.

    Microsoft documents that Windows services must be started by the Service Control Manager, and the normal debugging flow is to start the service first and then attach to its process. Debugging early startup code (like OnStart) is difficult when attaching after the service is already running. https://learn.microsoft.com/dotnet/framework/windows-services/how-to-debug-windows-service-applications

    For debugging early service startup, the documented way is to trigger the debugger from inside the service itself. You can do this by calling System.Diagnostics.Debugger.Launch() at the beginning of OnStart. When the service starts, Windows will prompt you to choose a debugger, and Visual Studio will attach at that point.

    https://learn.microsoft.com/visualstudio/debugger/how-to-debug-the-onstart-method

    Please let us know if you require any further assistance, we’re happy to help. If you found this information useful, kindly mark this as "Accept Answer". So that others in the community facing similar issues can easily find the solution. Your contribution is highly appreciated.


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.