Archive

Posts Tagged ‘Windows service’

Unable to install ‘Windows Service’ using installutil.exe

February 4, 2019 Leave a comment

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}

SCCreate_1

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;

}

🙂