错误1053的服务没有启动或控制请求 [英] Error 1053 the service did not respond to the start or control request

查看:127
本文介绍了错误1053的服务没有启动或控制请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在C#中,基本上会检查我的分贝每分钟的订单,产生这些订单PDF和电子邮件它写一个Windows服务。



逻辑完美的作品在我的测试等。



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




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


回应< /块引用>

我的OnStart方法是这样的:

 保护覆盖无效的OnStart(字串[] args)
{
// writeToWindowsEventLog(服务启动,EventLogEntryType.Information);
timer.Enabled = TRUE;
}



基本上,只要启动定时器...所以世界上没有处理密集通话有



我在哪里去了?



我试过启动帐户设置为本地系统,网络服务等...没有什么工作



编辑:



下面是我的代码:(processPurchaseOrders是方法其中,DB查询和PDF的生成等)

 公共部分类PurchaseOrderDispatcher:ServiceBase 
$ { b $ b //这是服务
私人System.Timers.Timer定时器的主要定时器;

公共PurchaseOrderDispatcher()
{
的InitializeComponent();
}

//主入口点过程
静态无效的主要()
{
#如果(!DEBUG)
ServiceBase [] ServicesToRun;
ServicesToRun =新ServiceBase [] {新PurchaseOrderDispatcher()};
ServiceBase.Run(ServicesToRun);
的#else //调试代码
PurchaseOrderDispatcher服务=新PurchaseOrderDispatcher();
service.processPurchaseOrders();
#ENDIF
}

私人无效的Ini​​tializeComponent()
{
this.CanPauseAndContinue = TRUE;
this.ServiceName =Crocus_PurchaseOrderGenerator;
}

私人无效InitTimer()
{
定时器=新System.Timers.Timer();

//线了计时器事件
timer.Elapsed + =新ElapsedEventHandler(timer_Elapsed);

//设置计时器的时间间隔
VAR timeInSeconds = Convert.ToInt32(ConfigurationManager.AppSettings [TimerIntervalInSeconds]);
timer.Interval =(timeInSeconds * 1000); // timer.Interval是以毫秒为单位,因此,上述时间1000

timer.Enabled = TRUE;
}

保护覆盖无效的Dispose(BOOL处置)
{
如果(处置和放大器;&安培;!(组件= NULL))
组分。的Dispose();

base.Dispose(处置);
}

保护覆盖无效的OnStart(字串[] args)
{
//实例化定时器
线程t =新主题(新的ThreadStart(这.InitTimer));
t.Start();
}

保护覆盖无效调用OnStop()
{
//关闭计时器。
timer.Enabled = FALSE;
}

保护覆盖无效的onPause()
{
timer.Enabled = FALSE;

base.OnPause();
}

保护覆盖无效OnContinue()
{
timer.Enabled = TRUE;

base.OnContinue();
}

保护无效timer_Elapsed(对象发件人,ElapsedEventArgs E)
{
processPurchaseOrders();
}
}


解决方案

MSDN :结果
不要使用构造函数来进行处理,应该在OnStart中,使用的OnStart处理服务的所有初始化。当应用程序的可执行文件运行,而不是在服务运行时,的OnStart前的可执行文件运行时,当你继续调用构造函数例如,构造也不会再次调用,因为在SCM已持有存储器的对象。如果调用OnStop释放在构造而非的OnStart分配的资源,所需要的资源将不被再次创建的第二时间该服务被称为。搜索结果,
。如果您的计时器未在的OnStart呼叫初始化,这可能是一个问题。
我还要检查计时器的类型,确保它是一个System.Timers.Timer的服务。 这里是一个例子如何设置计时器在Windows服务。



// TODONT :使用Windows服务只是运行计划的过程



我想你的代码,它似乎确定。我唯一​​的区别是硬编码计时器值(Service1.cs)。让我知道如果下面不工作。



Service1.cs

 使用系统; 
使用System.Collections.Generic;
使用System.ComponentModel;
使用System.Data这;使用System.Diagnostics程序
;
使用System.ServiceProcess;
使用System.Text;使用System.Timers
;
使用的System.Threading;

命名空间WindowsServiceTest
{
公共部分类服务1:ServiceBase
{
私人System.Timers.Timer定时器;

公共服务1()
{
的InitializeComponent();
}

保护覆盖无效的OnStart(字串[] args)
{
//实例化定时器
线程t =新主题(新的ThreadStart(这.InitTimer));
t.Start();
}

保护覆盖无效调用OnStop()
{
timer.Enabled = FALSE;
}

私人无效InitTimer()
{
定时器=新System.Timers.Timer();
//线了计时器事件
timer.Elapsed + =新ElapsedEventHandler(timer_Elapsed);
//设置计时器的时间间隔
// VAR timeInSeconds = Convert.ToInt32(ConfigurationManager.AppSettings [TimerIntervalInSeconds]);
双timeInSeconds = 3.0;
timer.Interval =(timeInSeconds * 1000);
// timer.Interval是以毫秒为单位,因此,上述时间以1000
timer.Enabled = TRUE;
}

保护无效timer_Elapsed(对象发件人,ElapsedEventArgs E)
{
INT timer_fired = 0;
}
}
}



Service1.Designer.cs

 命名空间WindowsServiceTest 
{
部分类服务1
{
///< ;总结>
///必需的设计变量。
///< /总结>
私人System.ComponentModel.IContainer成分= NULL;

///<总结>使用
///清理的任何资源。
///< /总结>
///< PARAM NAME =处理>真要是托管资源应释放;否则为false< /参数>
保护覆盖无效的Dispose(BOOL处置)
{
如果(处置和放大器;及(成分= NULL)!)
{
components.Dispose();
}
base.Dispose(处置);
}

#地区的组件设计器生成的代码

///<总结>
///设计器支持所需的方法 - 不要修改
///此方法的代码编辑器的内容。
///< /总结>
私人无效的Ini​​tializeComponent()
{
组分=新System.ComponentModel.Container();
this.ServiceName =服务1;
this.CanPauseAndContinue = TRUE;
}

#endregion
}
}

我刚刚创建了一个空白的Windows服务项目,并添加下面,所以我可以installutil.exe运行,并连接到上面看到如果事件触发(和它做)。

 使用系统; 
使用System.Collections.Generic;
使用System.Text;
使用System.ComponentModel;
使用System.ServiceProcess;

命名空间WindowsServiceTest
{
[runInstaller的(真)]
公共类MyServiceInstaller:System.Configuration.Install.Installer
{
公MyServiceInstaller()
{
ServiceProcessInstaller过程=新ServiceProcessInstaller();

process.Account = ServiceAccount.LocalSystem;

的ServiceInstaller serviceAdmin =新的ServiceInstaller();

serviceAdmin.StartType = ServiceStartMode.Manual;
serviceAdmin.ServiceName =服务1;
serviceAdmin.DisplayName =服务1显示名称;
Installers.Add(过程);
Installers.Add(serviceAdmin);
}
}
}


I've written a Windows Service in C# that basically checks my db every minute for orders, generates a PDF from these orders, and emails it.

The logic works perfectly in my tests etc..

When i create the service, and install it using the setup project, when I go to start the service in the services mmc, I get:

error 1053 the service did not respond to the start or control request in a timely fashion

My OnStart method looks like this:

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

Basically, just enables the timer... so theres no process intensive call there.

Where am I going wrong?

I've tried setting the startup account to local system, network service etc... nothing works!

Edit:

Here is my code: (processPurchaseOrders is the method where the db is queried and pdf's are generated etc...)

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();
    }
}

解决方案

From MSDN:
"Do not use the constructor to perform processing that should be in OnStart. Use OnStart to handle all initialization of your service. The constructor is called when the application's executable runs, not when the service runs. The executable runs before OnStart. When you continue, for example, the constructor is not called again because the SCM already holds the object in memory. If OnStop releases resources allocated in the constructor rather than in OnStart, the needed resources would not be created again the second time the service is called."

If your timer is not initialized in the OnStart call, this could be a problem. I would also check the type of timer, make sure its a System.Timers.Timer for Services. Here is an example of how to setup the timer in a windows service.

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

I tried your code, and it seems ok. The only difference I had was to hard code the timer value (Service1.cs). Let me know if the below doesnt work.

Service1.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;
        }
    }
}

Service1.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
    }
}

I just created a blank Windows Service project and add the below so I could run installutil.exe and attach to the above to see if the event was firing (and it did).

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);
        }
    }
}

这篇关于错误1053的服务没有启动或控制请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆