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 ?
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. 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.
确保您具有某种形式的警报系统,以在服务发生故障时通知您,例如,向您自己或某个邮箱发送电子邮件。
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 (ie don't release debug code) because it does crash if there is no Visual Studio installed on the machine.
If it makes sense, don't forget to implement the Pause event. Handle all exceptions so it fails gracefully when it fails.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.