[Solved] C# Windows service reports an error when the service is started. The service did not respond to the start or control request in time, error code 1053

There are many reasons:

1. There are bugs in the service program, so write your own test methods to troubleshoot.

2. The service program uses the System.Configuration.ConfigurationManager class library to try to read the configuration items under app.config, etc.

After installing the service, the path found by System.Configuration.ConfigurationManager is no longer the path where the original exe is located, so it cannot be used;

You can also write xml or Json configuration file absolute path to read.

3. The OnStart method takes too long to respond to the logic. It is recommended to make asynchronous calls, such as opening a child thread or using a timer. For example:

 1 System.Timers.Timer _timer = new System.Timers.Timer();
 2 protected override void OnStart(string[] args)
 3         {
 4             try
 5             {
 6                 int _interval = 1000 * 60 * interval;
 7                 _timer.Interval = _interval;
 8                 _timer.AutoReset = true;
 9                 _timer.Elapsed += new System.Timers.ElapsedEventHandler(Time_Elapsed);
10                 _timer.Enabled = true;
11                 WriteLog.SaveInfoLog("服务已启动");
12             }
13             catch (Exception ex)
14             {
15                 WriteLog.SaveExceptionLog(ex.Message);
16             }
17 
18         }
19 private void Time_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
20         {
21             try
22             {
23                 if (!isRun)
 24                  {
 25                      // Call business method 
26                  }
 27              }
 28              catch (Exception ex)
 29              {
 30                  WriteLog.SaveExceptionLog( " Error: " + ex.Message);
 31              }
 32          }

4. Check the .Net FrameWork version.

5. Check whether the service name in the serviceInstaller control property of ProjectInstaller is consistent with the started service name.

Similar Posts:

Leave a Reply

Your email address will not be published. Required fields are marked *