Create Event Log & Source
If you need to write logs into the event log with custom log and source, it is advisable to create them during windows service installation. This is because writing log into event log requires the log and source to be there. The good thing is that if it does not exist, the code will attempt to create the log and source. But if the Windows Service is launched with a user with low-privilege, then the service will throw an exception due to the user did not have the permission to creation of log and source. For more detail on dealing with Event Log, you can refer to the following link. http://jaryl-lan.blogspot.com/2015/09/create-configure-and-write-to-event-log.html
Automatically Start / Stop the Windows Service During Installation & Uninstall
To simplify the installation, you may want to consider starting the service for the user after installation. Otherwise the user need to manually start the windows service by launching the Services window to find the installed windows service. Also, there's a high chance that the user is not aware of the windows service name.
[C#]
using (var serviceController = new ServiceController(_serviceName))
{
if (serviceController.Status != ServiceControllerStatus.Stopped)
return;
serviceController.Start();
serviceController.WaitForStatus(ServiceControllerStatus.Running);
}
[VB]
Using ServiceController As ServiceController = New ServiceController(_serviceName)
If Not ServiceController.Status = ServiceControllerStatus.Stopped Then
Exit Sub
End If
ServiceController.Start()
ServiceController.WaitForStatus(ServiceControllerStatus.Running)
End Using
Other than that, you may want to stop the service before uninstall the windows service. When the windows service is stopping, the OnStop method will be executed, so you can write the necessary code to do some cleanup in the method.
[C#]
using (var serviceController = new ServiceController(_serviceName))
{
if (serviceController.Status != ServiceControllerStatus.Running)
return;
serviceController.Stop();
serviceController.WaitForStatus(ServiceControllerStatus.Stopped);
}
[VB]
Using ServiceController As ServiceController = New ServiceController(_serviceName)
If Not ServiceController.Status = ServiceControllerStatus.Running Then
Exit Sub
End If
ServiceController.Stop()
ServiceController.WaitForStatus(ServiceControllerStatus.Stopped)
End Using
Windows Service Recovery
For those who are unaware about Windows Service Recovery, you can actually configure them in the Recovery Tab by navigating to the Windows Service's properties and look for the Recovery Tab. But it can be tedious to configure each and every Windows Service that you have installed. To simplify this, recovery settings should be set during Windows Service installation. There are couple of ways to do it, but I will only demonstrate how to set it using command line with Process class.
The arguments specified in the code below will do the following:
[C#]
[VB]
Display Error Message for Failed Installation / Uninstall
It is best to show an error message whenever an installation or uninstall for the windows service is being performed, otherwise it will be hard and tedious to trace the problem (Check installation log and Event Log) & the end user that does the installation or uninstall may not aware on how to trace the problem. With a proper and meaningful error message being displayed, the user will be aware of the error and might be able to perform the necessary action or amendment to fix the problem and then retry the installation or uninstall of the windows service.
The sample code can be obtained here. https://onedrive.live.com/redir?resid=E6612168B803803D!345&authkey=!AOIm5LtxJbwXoQo&ithint=file%2czip
The arguments specified in the code below will do the following:
- The error count will be reset after 3600 seconds.
- The Windows Service will restart itself when it gets terminated unexpectedly for the first and second failure (After 5 minutes).
- The subsequent failure will attempt to execute the windows service with the argument "-email" (After 30 seconds).
[C#]
using (var process = new Process())
{
var startInfo = process.StartInfo;
startInfo.FileName = SC_COMMAND;
startInfo.Arguments = string.Format("failure \"{0}\" reset= 3600 actions=
restart/300000/restart/300000/run/30000 command= \"\\\"{1}\\\"
-email\"", _serviceName,
executableLocation);
process.Start();
process.WaitForExit();
}
[VB]
Using process As Process = New Process()
Dim startInfo = process.StartInfo
startInfo.FileName = SC_COMMAND
startInfo.Arguments = String.Format("failure ""{0}"" reset= 3600
actions= restart/300000/restart/300000/run/30000 command=
""\""{1}\"" -email""", _serviceName, executableLocation)
process.Start()
process.WaitForExit()
End Using
Display Error Message for Failed Installation / Uninstall
It is best to show an error message whenever an installation or uninstall for the windows service is being performed, otherwise it will be hard and tedious to trace the problem (Check installation log and Event Log) & the end user that does the installation or uninstall may not aware on how to trace the problem. With a proper and meaningful error message being displayed, the user will be aware of the error and might be able to perform the necessary action or amendment to fix the problem and then retry the installation or uninstall of the windows service.
The sample code can be obtained here. https://onedrive.live.com/redir?resid=E6612168B803803D!345&authkey=!AOIm5LtxJbwXoQo&ithint=file%2czip
No comments:
Post a Comment