代码之家  ›  专栏  ›  技术社区  ›  Alex

错误1053服务没有响应启动或控制请求

  •  36
  • Alex  · 技术社区  · 15 年前

    我已经用C语言编写了一个Windows服务,它基本上每分钟检查我的数据库以获取订单,从这些订单生成一个PDF,然后通过电子邮件发送。

    逻辑在我的测试等中非常有效。

    当我创建服务并使用安装项目安装它时,当我在服务MMC中启动服务时,我得到:

    错误1053服务没有及时响应启动或控制请求

    我的OnStart方法如下:

    protected override void OnStart(string[] args)
    {
        //writeToWindowsEventLog("Service started", EventLogEntryType.Information);
        timer.Enabled = true;
    }
    

    基本上,只要启动计时器…所以这里没有流程密集型调用。

    我哪里出错了?

    我已尝试将启动帐户设置为本地系统、网络服务等…什么都不管用!

    编辑:

    这是我的代码:(processpurchaseorders是查询数据库和生成PDF等的方法…)

    public partial class PurchaseOrderDispatcher : ServiceBase
    {
        //this is the main timer of the service
        private System.Timers.Timer timer;
    
        public PurchaseOrderDispatcher()
        {
            InitializeComponent();
        }
    
        // The main entry point for the process
        static void Main()
        {
          #if (!DEBUG)
            ServiceBase[] ServicesToRun;
            ServicesToRun = new ServiceBase[] { new PurchaseOrderDispatcher() };
            ServiceBase.Run(ServicesToRun);
          #else //debug code
            PurchaseOrderDispatcher service = new PurchaseOrderDispatcher();
            service.processPurchaseOrders();
          #endif
        }
    
        private void InitializeComponent()
        {
            this.CanPauseAndContinue = true;
            this.ServiceName = "Crocus_PurchaseOrderGenerator";
        }
    
        private void InitTimer()
        {
            timer = new System.Timers.Timer();
    
            //wire up the timer event
            timer.Elapsed += new ElapsedEventHandler(timer_Elapsed);
    
            //set timer interval
            var timeInSeconds = Convert.ToInt32(ConfigurationManager.AppSettings["TimerIntervalInSeconds"]);
            timer.Interval = (timeInSeconds * 1000); // timer.Interval is in milliseconds, so times above by 1000
    
            timer.Enabled = true;
        }
    
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
                components.Dispose();
    
            base.Dispose(disposing);
        }
    
        protected override void OnStart(string[] args)
        {
            //instantiate timer
            Thread t = new Thread(new ThreadStart(this.InitTimer));
            t.Start();
        }
    
        protected override void OnStop()
        {
            //turn off the timer.
            timer.Enabled = false;
        }
    
        protected override void OnPause()
        {
            timer.Enabled = false;
    
            base.OnPause();
        }
    
        protected override void OnContinue()
        {
            timer.Enabled = true;
    
            base.OnContinue();
        }
    
        protected void timer_Elapsed(object sender, ElapsedEventArgs e)
        {
            processPurchaseOrders();
        }
    }
    
    12 回复  |  直到 6 年前
        1
  •  27
  •   SwDevMan81    15 年前

    MSDN :
    “不要使用构造函数来执行应在OnStart中进行的处理。使用onStart处理服务的所有初始化。构造函数在应用程序的可执行文件运行时调用,而不是在服务运行时调用。可执行文件在OnStart之前运行。例如,当您继续时,不会再次调用构造函数,因为SCM已经将对象保存在内存中。如果OnStop释放在构造函数中而不是在OnStart中分配的资源,则在第二次调用服务时,不会再次创建所需的资源。”

    如果计时器未在OnStart调用中初始化,这可能是一个问题。 我还将检查计时器的类型,确保它是用于服务的system.timers.timer。 Here 是如何在Windows服务中设置计时器的示例。

    //TODONT: Use a Windows Service just to run a scheduled process

    我试过你的密码,好像没问题。我唯一的区别是硬编码计时器值(service1.cs)。如果下面的不起作用,请告诉我。

    Service 1.CS

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Diagnostics;
    using System.ServiceProcess;
    using System.Text;
    using System.Timers;
    using System.Threading;
    
    namespace WindowsServiceTest
    {
        public partial class Service1 : ServiceBase
        {
            private System.Timers.Timer timer;
    
            public Service1()
            {
                InitializeComponent();
            }
    
            protected override void OnStart(string[] args)
            {
                //instantiate timer
                Thread t = new Thread(new ThreadStart(this.InitTimer)); 
                t.Start();
            }
    
            protected override void OnStop()
            {
                timer.Enabled = false;
            }
    
             private void InitTimer()  
             {     
                 timer = new System.Timers.Timer();  
                 //wire up the timer event 
                 timer.Elapsed += new ElapsedEventHandler(timer_Elapsed); 
                 //set timer interval   
                 //var timeInSeconds = Convert.ToInt32(ConfigurationManager.AppSettings["TimerIntervalInSeconds"]); 
                 double timeInSeconds = 3.0;
                 timer.Interval = (timeInSeconds * 1000); 
                 // timer.Interval is in milliseconds, so times above by 1000 
                 timer.Enabled = true;  
             }
    
            protected void timer_Elapsed(object sender, ElapsedEventArgs e) 
            {
                int timer_fired = 0;
            }
        }
    }
    

    服务1.designer.cs

    namespace WindowsServiceTest
    {
        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()
            {
                components = new System.ComponentModel.Container();
                this.ServiceName = "Service1";
                this.CanPauseAndContinue = true;
            }
    
            #endregion
        }
    }
    

    我刚刚创建了一个空白的Windows服务项目,并添加了下面的内容,这样我就可以运行installUtil.exe并附加到上面的内容,以查看事件是否正在触发(它确实触发了)。

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.ComponentModel;
    using System.ServiceProcess;
    
    namespace WindowsServiceTest
    {
        [RunInstaller(true)]
        public class MyServiceInstaller : System.Configuration.Install.Installer
        {
            public MyServiceInstaller()
            {
                ServiceProcessInstaller process = new ServiceProcessInstaller();
    
                process.Account = ServiceAccount.LocalSystem;
    
                ServiceInstaller serviceAdmin = new ServiceInstaller();
    
                serviceAdmin.StartType = ServiceStartMode.Manual;
                serviceAdmin.ServiceName = "Service1";
                serviceAdmin.DisplayName = "Service1 Display Name";
                Installers.Add(process);
                Installers.Add(serviceAdmin);
            }
        }
    }
    
        2
  •  27
  •   SwDevMan81    9 年前

    我也有同样的问题。

    结果是因为我在调试模式下把它作为控制台运行,就像上面的代码一样。

    #if (!DEBUG)
    
    #else //debug code
    
    #endif
    

    我在调试模式下编译了它并安装了服务

    当我以发布模式编译它时,它按预期工作

    希望这有帮助

        3
  •  11
  •   Community CDub    9 年前

    如果以后有其他人遇到这种情况,我在Windows Server 2012上尝试启动Windows服务时收到相同的错误1053。

    最终问题是,该服务是针对.NET Framework 4.5.1开发的,但Windows Server 2012实例没有安装该版本的.NET Framework。将服务备份到目标.NET 4.0修复了该错误。

        4
  •  2
  •   SwDevMan81    9 年前

    这对我有用。基本上确保登录用户设置为正确的用户。但是,这取决于如何设置帐户基础结构。在我的示例中,它使用的是广告帐户用户凭据。

    在“启动”菜单搜索框中搜索“服务” -在服务中找到所需的服务 -右键单击并选择“登录”选项卡 -选择“此帐户”并输入所需的内容/凭据 -好的,照常开始服务

    enter image description here

        5
  •  1
  •   vnRock    14 年前

    我到服务器控制台(在服务器室)从那里开始服务。遥控器不工作。

        6
  •  1
  •   AndyClaw    12 年前

    建造师是我的问题。构造函数必须已针对缺少的DLL抛出了一个异常。

    我的问题是:我缺乏创建安装程序的经验。我没有将依赖DLL复制到安装文件夹中(在创建主项目输出时,我需要选择发布生成配置)。

        7
  •  1
  •   SyntaxError    12 年前

    在我发现.config文件中有多余的“>”字符之前,也会出现此错误。

    因此,在对计算机进行穿孔之前,请先尝试再次检查.config文件;)

        8
  •  0
  •   Orcun    10 年前

    在我的例子中,我尝试将.NET 3.5服务安装到Windows2012服务器。在服务器中安装了.NET 4.0框架。

    我将服务的目标框架更改为.NET 4.0。现在它工作得很好。

        9
  •  0
  •   Community CDub    8 年前

    从包含服务的程序集执行的第一件事是 Main 方法。它必须采取特别行动,或至少采取一项这样的行动:

    public static int Main()
    {
      Run(new System.ServiceProcess.ServiceBase[] { new YourServiceClass() });
    }
    

    这就是我在创建我的第一个服务时在测试和错误会话之后发现的。我没有使用vs.我使用vs.指南( Walkthrough: Creating a Windows Service Application in the Component Designer ,而我更应该使用这个: Creating a C# Service Step-by-Step: Lesson I .

    如果没有合适的“main”方法,可执行文件将立即完成,系统报告超过30秒超时:)

        10
  •  0
  •   Ruben Collazos    7 年前

    像我一样。在4.6.1中不工作(我有相同的信息)。 然后我试着用4.5,工作得很好。

        11
  •  0
  •   David Lindon    7 年前

    如果服务的名称与实际的.exe文件名不同,请确保没有与位于同一文件夹中的服务同名的.exe,否则会导致它失败。

    在我的状态中,有一个名为“index reader”的服务指向“index reader service.exe”,在同一个文件夹中有一个名为“index reader.exe”的exe。删除此项解决了问题。

    enter image description here

    enter image description here

        12
  •  0
  •   Misha Beskin    6 年前

    我也有同样的问题 但在我的例子中,当我在发布时重新构建安装程序服务时。 重新安装服务并运行, 服务已开始运行,没有任何问题