Windows services can be the most frustrating things to develop. You carefully write all your code in a library separate from your service facade. Maybe you even write a console facade for debugging. You create an army of unit tests which all pass. The only thing left is this small bit of code in the Windows Service project which instantiates and runs your library... and it crashes.
This isn't intended to be a lengthy tutorial on Windows service development, rather a reminder of one very common reason services fail: The Event Log is full. In fact, if your service fails immediately for this reason, consider yourself lucky. Most people don't run into this issue until their service has been in production for weeks or months. At that point, the cause is extremely mysterious.
To see if your issue may be related to the Event Log, check for the following - probably last - entry in the log:
"EventType clr20r3, P1 myservice.exe, P2 1.0.0.0, P3 4aca32b4, P4 system, P5 2.0.0.0, P6 4889de7a, P7 3839, P8 131, P9 system.componentmodel.win32, P10 NIL."
Or you may observe a pop-up similar to the following:
An unhandled exception ('System.ComponentMdel.Win32Exception') occurred in myservice.exe [1234].
The easiest way to verify if the log is at fault is to clear the log contents and restart the service. If that fixes the issue, then change the properties on the event log your service writes to as in the following image. The default 512k Log size and "Overwrite every 7 days" behavior is what causes this issue. Consider changing the behavior to overwrite as needed and increase the log size.

Note: Depending on your environment, the IT administrator may be using third-party tools to manage the Event Log. If that is the case, those tools may not be properly configured as they should eliminate this issue.
Note: If your product is distributed for installation on the customer's hardware, consider using a custom event log for sole use by your service (always a good idea) and configuring it appropriately in the installer.

Recent comments
31 weeks 6 hours ago
31 weeks 7 hours ago
34 weeks 3 days ago
40 weeks 1 day ago
48 weeks 6 days ago