开发者

Windows Service Error 1053

I'm currently writing a windows service which connects to a crm system to pull down a schedule which then runs various datafeeds etc. I've got everything working except when I install everything and try to run start the service I get the following error :

"Error 1053: The service did not respond to the start or control request in a timely fashion"

Here's the code I'm using in my Service1.cs;

namespace FeedManagementService
{
  public partial class Service1 : ServiceBase
  {
    private System.Timers.Timer timer;

public Service1()
{
  InitializeComponent();
}

protected override void OnStart(string[] args)
{
  // Instantiate the timer
  Thread t = new Thread(new ThreadStart(this.InitTimer));
  t.IsBackground = true;
  t.Start();
} // OnStart

protected override void OnStop()
{
  timer.Enabled = false;
} // OnStop

private void InitTimer()
{
  tim开发者_开发知识库er = new System.Timers.Timer();

  // Add the timer event
  timer.Elapsed += new ElapsedEventHandler(timerTick);

  // Set the interval
  double timeInSeconds = 6.0;
  timer.Interval = (timeInSeconds * 1000);
  timer.Enabled = true;
} // InitTimer()

private void timerTick(object sender, EventArgs e)
{
  // CRM connection stuffhere
} // timerTick
  }
}

Then the following in the Service1.Designer.cs

namespace FeedManagementService
{
  partial class Service1
  {
    /// <summary> 
    /// Required designer variable.
    /// </summary>
    private System.ComponentModel.IContainer components = null;

    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
    protected override void Dispose(bool disposing)
    {
      if (disposing && (components != null))
      {
        components.Dispose();
      }
      base.Dispose(disposing);
    }

    #region Component Designer generated code

    /// <summary> 
    /// Required method for Designer support - do not modify 
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
      this.components = new System.ComponentModel.Container();
      this.ServiceName = "Feed Management Service";
      this.CanPauseAndContinue = true;
    } // InitializeComponent()

    #endregion
  }
}

And lastly the following in ProjectInstaller.cs

namespace FeedManagementService
{
  [RunInstaller(true)]
  public partial class ProjectInstaller : System.Configuration.Install.Installer
  {
    public ProjectInstaller()
    {
      ServiceProcessInstaller process = new ServiceProcessInstaller();

      process.Account = ServiceAccount.LocalSystem;

      ServiceInstaller serviceAdmin = new ServiceInstaller();

      serviceAdmin.StartType = ServiceStartMode.Manual;
      serviceAdmin.ServiceName = "Service1";
      serviceAdmin.DisplayName = "Feed Management Service";
      Installers.Add(process);
      Installers.Add(serviceAdmin);
    }
  }
}


After a lot of investigation and fixing a whole bunch of "problems" which turned out to be nothing to do with the issue, I discovered that I needed the following in the Main() method of the service;

ServiceBase[] ServicesToRun;
  ServicesToRun = new ServiceBase[] 
        { 
            new Service1() 
        };
  ServiceBase.Run(ServicesToRun);

As a result of adding this everything seems to work perfectly now, the service starts as expected.


It seems you OnStart() method is not returnig within the allowed timeout. Does the message appears at once, or does it take some 30 seconds to show up? Does your CRM Connection Stuff(tm) takes a while to run?

A longshot: do you use any Windows.Forms stuff in your app? These should not be used on a service, and can interact in strange and mysterious ways.


I encountered the same issue and was not at all sure how to resolve it. Yes this occurs because an exception is being thrown from the service, but there are a few general guidelines that you can follow to correct this:

  • Check that you have written the correct code to start the service:

    ServiceBase[] ServicesToRun; ServicesToRun = new ServiceBase[] { new WinsowsServiceToRun() }; ServiceBase.Run(ServicesToRun);

  • You need to ensure that there is some kind of infinite loop running in the class WinsowsServiceToRun

  • Finally, there may be some code which is not logging anything and closing the program abruptly (which was the case with me), in this case you will have to follow the old school of debugging which needed to write a line to a source (text/db/wherever). What I faced was that since the account running the service was not "Admin", the code was just falling off and not logging any exceptions in case it was trying to write to "Windows Event Log" even though the code was there to log exceptions. Admin privilege is actually not needed for logging to Even Log but it is needed to define the source. In case source of the event is not already defined in the system and the service tries to log it for the first time without admin privilege it fails. To solve this follow below steps:

  1. Open command prompt with admin privilege
  2. Paste the command : eventcreate /ID 1 /L APPLICATION /T INFORMATION /SO <> /D "<>"
  3. Press enter
  4. Now start the service
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜