OnStart method for debugging Windows Services
1. Add the following code within the OnStart method
#if DEBUG
System.Diagnostics.Debugger.Launch ();
#endif
The complete code is as follows
protected override void OnStart (string[] args)
{
#if DEBUG
System.Diagnostics.Debugger.Launch ();
#endif
//add method code
}
2. Strike a breakpoint within the OnStart method
3. Start the service and hit the breakpoint within the OnStart method.
Note: Debugger.launch () is in static void Main (), as follows
static void Main ()
{
#if DEBUG
System.Diagnostics.Debugger.Launch ();
#endif
servicebase[] ServicesToRun;
ServicesToRun = new servicebase[]
{
new Serviceemail ()
};
Servicebase.run (ServicesToRun);
}
Note: Because of permissions issues, you may not be able to select the Visual Studio you are opening, and a new Visual Studio will open, the new interface cannot find the code layer and cannot be interrupted, and the solution is to run Visual Studio as an administrator, Then start the service, and in the pop-up select the Visual Studio page, select the Visual Studio you are debugging, and never create a new one.