You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Windows Service Error 1053求助:正式服务启动失败排查

Windows Service启动Error 1053问题排查求助

我正在开发一款Windows Service,功能是从API获取JSON结果,反序列化为对象列表后利用对象信息发送邮件。此前通过控制台模式开发测试时,服务可正常完成所有预期功能,但打包为正式服务安装后,启动时出现Error: 1053,服务卡在「正在启动」状态,需卸载并重启电脑才能修改测试。

已尝试的解决方法

  • 修改注册表中的超时设置
  • 确保服务使用机器上安装的最高版本.NET Framework
  • 创建不使用控制台模式的全新Windows Service项目
  • 将项目各处设置为Release模式
  • 移除EventLog的使用
  • 将OnStart中的大部分逻辑移至独立方法并通过线程调用

相关代码

OnStart方法

//prints function we are in to the debug console and sets the service
//status, as well as adds an entry to the event log
Debug.WriteLine("In OnStart.");
ServiceStatus serviceStatus = new ServiceStatus();
serviceStatus.dwCurrentState = ServiceState.SERVICE_START_PENDING;
serviceStatus.dwWaitHint = 100000;
SetServiceStatus(this.ServiceHandle, ref serviceStatus);
//eventLog1.WriteEntry("In OnStart.", EventLogEntryType.Information);

Thread t = new Thread(new ThreadStart(MainRunner));
t.Start();

// Update the service state to Running.
serviceStatus.dwCurrentState = ServiceState.SERVICE_RUNNING;
SetServiceStatus(this.ServiceHandle, ref serviceStatus);

MainRunner方法

//timer used for evaluating every hour from start time
System.Timers.Timer timer = new System.Timers.Timer();
timer.Elapsed += new ElapsedEventHandler(this.OnTimer1);
timer.Interval = /*3600000;*/ 60000;// for 1 minute testing

//starts the timer
timer.Start();

DateTime hour = new DateTime(1, 1, 1, 17, 0, 0);


//checks faulty vehicles for the day, everyday at 5pm
while (true) //infinite loop
{
    //gets the current DateTime
    DateTime Now = DateTime.Now;

    //verifies that the current DateTime is 5pm and not a Saturday or Sunday
    if (Now.Hour == hour.Hour && Now.Minute == hour.Minute && Now.Second == hour.Second
        && Convert.ToInt32(Now.DayOfWeek) != 0 && Convert.ToInt32(Now.DayOfWeek) != 6)
    {
        TwenFourHour(); //calls the daily check
        //prints to debugger that daily check is done
        Debug.WriteLine("Out of TwenFourHour");
        //eventLog1.WriteEntry("Out of TwenFourHour", EventLogEntryType.Information);
     }
}

SetServiceStatus方法

[DllImport("advapi32.dll", SetLastError = true)]
private static extern bool SetServiceStatus(System.IntPtr handl, ref ServiceStatus serviceStatus);

需求说明

必须使用Windows Service实现以下功能:

  • 每小时调用一次API GET接口(不发邮件),小时级任务的周末排除逻辑在Timer方法中处理
  • 每天17点(排除周末)调用另一个API GET接口并发送邮件

现求助排查Error 1053导致服务启动失败的原因及解决办法。

内容的提问来源于stack exchange,提问作者Dave

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.25 10:55:08