I have created and installed a service a couple of times. Initially it was working fine, but after some changes in the service Code it start giving the error when I restart the service in Services.msc :
Error 1053: the service did not respond to the start or control request in a timely fashion
Code:
public partial class AutoSMS : ServiceBase
{
public AutoSMS()
{
InitializeComponent();
eventLog1.Clear();
if (!System.Diagnostics.EventLog.SourceExists("MySource"))
{
System.Diagnostics.EventLog.CreateEventSource(
"MySource", "MyNewLog");
}
eventLog1.Source = "MySource";
eventLog1.Log = "MyNewLog";
Timer checkForTime = new Timer(5000);
checkForTime.Elapsed += new ElapsedEventHandler(checkForTime_Elapsed);
checkForTime.Enabled = true;
}
protected override void OnStart(string[] args)
{
eventLog1.WriteEntry("In OnStart");
}
protected override void OnStop()
{
eventLog1.WriteEntry("In onStop.");
}
void checkForTime_Elapsed(object sender, ElapsedEventArgs e)
{
string Time = "15:05:00";
DateTime dateTime = DateTime.ParseExact(Time, "HH:mm:ss",
CultureInfo.InvariantCulture);
if (DateTime.Now == dateTime) ;
eventLog1.WriteEntry(Time);
}
}
Here is my main method code
static void Main()
{
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[]
{
new AutoSMS()
};
ServiceBase.Run(ServicesToRun);
}
I also tried the following steps :
I used to install and uninstall it with following command :
installutil AutoSMS.exe
installutil /u AutoSMS.exe
In my case, I was publishing service while it was in debug mode.
Solution was:
InstallUtil -u WindowsServiceName.exe
InstallUtil -i WindowsServiceName.exe
It worked perfectly after.
As others have pointed out, this error can have a multitude of causes. But in the hopes that this will help somebody out, I'll share what happened in our case. For us, our service had been upgraded to .NET 4.5, but the server did not have .NET 4.5 installed.
After spending some time on the issue, trying solutions that didn't work, I run into this blog. It suggests to wrap the service initialization code in a try/catch block, like this, and adding EventLog
using System;
using System.Diagnostics;
using System.ServiceProcess;
namespace WindowsService
{
static class Program
{
static void Main()
{
try
{
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[]
{
new Service1()
};
ServiceBase.Run(ServicesToRun);
}
catch (Exception ex)
{
EventLog.WriteEntry("Application", ex.ToString(), EventLogEntryType.Error);
}
}
}
}
Then, uninstall the old service, redeploy the service with these modifications. Start the service and check out the Event Viewer/Application logs. You'll see what the real problem is, which is the underlying reason for the timeout.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With