Debugging a service is not as straightforward as debugging other application types, because a service must be run from within the context of the Service Control Manager rather than from within Visual Studio. To debug a service, you must start the service and then attach a debugger to the process in which it is running. You can then debug the application using all of the standard debugging functionality of Visual Studio.
To avoid going through this extra effort, you may want to test most of the code in your service in a test application. This test-bed application can have the same components (FileSystemWatchers, EventLogs, Timers, and so on) as the Windows Service, and thus be able to run the same logic in events. Once you have checked out the logic in this context, you can just copy and paste it into a Windows Service application.
However, sometimes the service itself needs to be debugged directly, so it is important to understand how to attach to the service's process and do direct debugging. You can only debug a service when it is running. When you attach the debugger to the service, you are interrupting it. The service is suspended for a short period while you attach to it. It is also interrupted when you place breakpoints and step through your code.
Attaching to the service's process enables you to debug most, but not all, of the service's code. For instance, because the service has already been started, you cannot debug the code in the service's OnStart method this way, or the code in the Main method that is used to load the service. To debug the OnStart event or any of the Visual Studio designer code, you have to add a dummy service and start that service first. In the dummy service, you would create an instance of the service that you want to debug. You can place some code in a Timer object and create the new instance of the object that you want to debug after 30 seconds or so. Allow enough time to attach to the debugger before the new instance is created. Meanwhile, place breakpoints in your startup code to debug those events, if desired.
Aside from issues with an issue occurring at startup to debug a running service, you'll need to ensure you have the appropriate permissions. This means you need to have started Visual Studio with Administrator privileges. Once you have done that you can perform the following steps: