I am debugging a Windows service (by hitting F5
in Visual Studio 2010) using the following code:
In Program.cs file:
static void Main() {
if (!Environment.UserInteractive) {
// We are not in debug mode, startup as service
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[] { new MyServer() };
ServiceBase.Run(ServicesToRun);
}
else {
// We are in debug mode, startup as application
MyServer service = new MyServer();
service.StartService();
System.Threading.Thread.Sleep(System.Threading.Timeout.Infinite);
}
}
And in MyServer.cs file:
public void StartService() {
this.OnStart(new string[0]);
}
In the past, I used the Debug.WriteLine("xxx")
line without any problem in all my service code, but now I noticed that all Debug.WriteLine()
lines are not called anymore.
I clearly see that the debugger is jumping over these lines when I debug with Step Into (F11
) - (thus, there is no output text on the output Window), whereas the service is correctly started in "debug mode".
I don't understand why the Debug
code won't be called. Suggestions?
I build my solution in debug mode (Define DEBUG constant
is checked), but I noticed that the code surrounded by #if DEBUG ... #endif
is not called. That is weird...