What are the best practices when running a process as a windows service?
.net, c#, windows-services
Solution
For critical error reporting, you are constrained to using the standard Service settings (in the properties of the installed service), or doing something yourself. This could be as simple a log file that logs unexpected errors (by using AppDomain.UnhandledException to catch and log them), using the Windows event log to log similar info, or having another process watch the service for errors (ie. the service stopping) and alerting someone.
Microsoft has an article entitled "Introduction to Windows Service Applications" that is a good general introduction to making services in .Net.
Some other things about developing Windows services from my experience:
- A Windows service is allowed about 30 seconds to start. After that, Windows will report it as not having started correctly. This means that you need to ensure that your `OnStart` method of the service kicks off a new thread to run the service and then returns.
- Don't expect any user interaction (ie. message boxes, confirmations) because the service runs "headless" (ie. without a UI), so you cannot expect a user to interact with it.
- Check the account that the service will run as to ensure that you are not running it as a user with unnecessarily high security privileges.
- Make extensive use of logging (eg. log4net) so that you can see what the service is doing during runtime, as well as being able to diagnose any errors (by logging stack traces).
- Make sure you use the correct version of InstallUtil (ie. 32 or 64 bit) to install the service with. Even better, let the service install itself using the ManagedInstallerClass.InstallHelper.
Problem
Is there any things to take care of when running your process or executable as service.Things like silent logging.Critical error reporting scenarios? etc? How do you handle it ?