Archive
Unable to install ‘Windows Service’ using installutil.exe
The other day, I was using Installutil.exe from Command Prompt (refer below command), to install my Windows Service.
C:\Windows\Microsoft.NET\Framework\v4.0.30319>InstallUtil.exe -i “X:\XX\XXX.exe”
Although the tool was giving success response, Windows Service was not getting installed and not being listed in ‘Service Manager’ console.
Alternate Option:
- While troubleshooting the issue, we came across another option to install the Windows Service using “SC Create“
Using SC Create:
To install Windows Service,
- Open the Command Prompt in ‘Run as administrator’ mode.
- Below is the syntax using ‘SC Create’ to install the service.
C:\Windows\system32>SC Create “{Your WinService exe name}” binpath=”{Your WinService exe path}” displayname=”{Your desired name}“
Using SC Delete:
To Uninstall the ‘Windows Service’,
- Open the Command Prompt in ‘Run as administrator’ mode.
- Below is the syntax using ‘SC Delete’ to Uninstall the service.
C:\Windows\system32>SC Delete “{Your WinService exe name}“
đ
‘Duplicate record execution’ issue with System.Timer Elapsed event – Windows Service
We have a window service with System.Timer control ‘timerExecuteQuery’, which periodically polls the records from Data Base and process.
this.timerExecuteQuery= new System.Timers.Timer(3) { AutoReset = true };
this.timerExecuteQuery.Start();
this.timerExecuteQuery.Elapsed += this.TimerExecuteQuery_Elapsed;
Everything seems working fine until we noticed few records executing twice causing inconsistency.
The issue was random and unable to reproduce consistently.
Reason:
- In our scenario, the culprit was System.Timerâs Elapsed event
- Our timerâs duration is 3 seconds with ‘AutoReset=true’, so, every 3 seconds Elapsed event kick in and executes the logic.
- In few scenarios, business logic in âTimer_Elapsedâ was taking more than 3 seconds causing the trigger of new Elapsed event before completion of old thread. Hence records got executed more than once randomly.
Fix:
- We declared a global Boolean variable âexecutingâ and handled as below.
- Set variable âexecuting=trueâ before Business logic of âTimer_Elapsedâ event and set âexecuting=falseâ after execution of Business logic.
- Check the âexecutingâ variable value before the execution of business logic, if its True, return.
private bool executing = false;
protected void TimerExecuteQuery_Elapsed(object sender, System.Timers.ElapsedEventArgs e){
// ‘executing’ true denotes, previous elapsed event was not over
if (executing){
return;
}executing = true;
// Business logic
executing = false;
}
đ
Duplicate record execution with System.Timer Elapsed event
We have a windows service with System.Timer control which periodically polls the records from Data Base and processes in its Elapsed event as below.
protected void Timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e)Â Â Â Â {
// Business logic goes here
}
Everything seems working fine until we noticed few records executing twice causing inconsistency.
The issue was random and unable to reproduce consistently.
Reason â
- Culprit was System.Timerâs Elapsed event
- Assume the timerâs duration is 3 seconds, so every seconds Elapsed event kick in and executes the business logic. What happens if previous elapsed event would not complete before next Elapsed event ticks in.
- This is exactly what happened in our case and caused duplicate execution of records.
Fix â
- We restricted overlap of Elapsed event using a Static variable as below
    protected static bool isInExecution = false;
protected void Timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e)Â Â Â Â {
if (isInExecution) return;
isInExecution = true;
// Business logic goes here
isInExecution = false;
}
đ