我目前正在编写一个windows服务,它连接到一个crm系统,以拉下一个时间表,然后运行各种datafeeds等。我已经让一切正常工作,除了当我安装所有东西并尝试运行启动服务时,我得到了以下错误:
“错误1053:服务未及时响应启动或控制请求”
下面是我在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()
{
  timer = 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
  }
}然后是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
  }
}最后,下面是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);
    }
  }
}发布于 2011-04-09 00:45:19
经过大量的调查和修复一大堆与问题无关的“问题”后,我发现我需要在服务的Main()方法中使用以下代码;
ServiceBase[] ServicesToRun;
  ServicesToRun = new ServiceBase[] 
        { 
            new Service1() 
        };
  ServiceBase.Run(ServicesToRun);由于添加了这一点,现在一切似乎都很完美,服务按预期启动。
https://stackoverflow.com/questions/5356360
复制相似问题