Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the best practices when running a process as a windows service?

Tags:

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 ?

like image 884
abmv Avatar asked Feb 17 '10 12:02

abmv


People also ask

What does it mean to run something as a Windows service?

Definition of Windows Services Unlike regular software that is launched by the end user and only runs when the user is logged on, Windows Services can start without user intervention and may continue to run long after the user has logged off.

When should you use Windows services?

You should create a Windows Service to run code in the background, without user interaction. For example, a Windows Service will run even if no-one is logged on. Any server that accepts connections (such as a mail, web, or FTP server) should usually be a Windows Service.

How do I make sure my Windows service is running?

To verify that this service is running as a Windows process, open Task Manager, select the Processes tab and select the Show processes from all users check box.

Can a Windows service start a process?

You can't. A Service doesn't run under any "real" user at all, so it can't start any processes which can in any way interact with the user - input or output - as there won't necessarily be any user logging in while the service is running.


2 Answers

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.
like image 57
adrianbanks Avatar answered Nov 03 '22 12:11

adrianbanks


  • Be sure to use a tracing/logging API to have diagnostic information. Eventlog, log files, database, whatever... Just have the troubleshooting info somewhere.
  • Trace/Log early and often. Nothing is more frustrating then having to make a code change which is just to add diagnostic tracing.
  • Be cognizant of memory leaking. When writing an app that will be restarted everyday or runs as a scheduled task, sometimes we get a bit lazy. Remember this service needs to clean up after itself thoroughly. Make proper use of the using clause with all IDisposables.
  • Do not forget the volatile keyword on your STOP variable! My God this gets me every time.
like image 38
TheSoftwareJedi Avatar answered Nov 03 '22 11:11

TheSoftwareJedi