Reputation: 7098
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 ?
Upvotes: 25
Views: 14561
Reputation: 6435
I have a good one, add the following code to your OnStart method or even better before it in your Main method.
#if DEBUG
//LogMessage("Service starting");
#warning The service has been set to break on debug. Only used for debugging.
//LogMessage("DEBUG MODE - If the service crashed after this then your problem is that you're running in DEBUG mode without a Visual Studio installed.");
if (Debugger.IsAttached == false) Debugger.Launch();
#endif
Basically the important part if the Debugger.Launch()
which will pop open a window when you start the service and ask you whether you would like to debug the service and with which Visual Studio. It makes working with services awesome and easy. I put the #warning
there just so it shows up in warning list to remind me it's there, although the #if DEBUG
should prevent most issues.
Just remember not to deploy with this code running (i.e. don't release debug code) because it does crash if there is no Visual Studio installed on the machine.
Upvotes: 3
Reputation: 82944
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:
OnStart
method of the service kicks off a new thread to run the service and then returns.Upvotes: 23
Reputation: 35196
Upvotes: 15
Reputation: 340271
If it makes sense, don't forget to implement the Pause event. Handle all exceptions so it fails gracefully when it fails.
Upvotes: 1
Reputation: 82096
Make sure you have some form of alert system to inform you if the service falls over e.g. send an email to yourself or some mailbox.
Upvotes: 3
Reputation: 96571
Don't show any message boxes / dialogs.
Be aware that your app will usually not run under the same account as a logged-in user. So if a user is able to access some file/directory, this does not mean that the service can do as well.
Upvotes: 3