Windows 服务无法启动(错误 1053) [英] Windows service will not start (Error 1053)

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

问题描述

我有一个正在尝试调试的 Windows 服务.现在即使当前代码可以工作,它也无法启动.错误是:

I have a Windows Service that I am trying to debug. Now it fails to start even though the current code used to work. The error is:

Windows 无法在本地计算机上启动 MyService 服务

Windows could not start the MyService service on Local Computer

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

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

为了隔离错误,我尝试注释掉所有内容.主要方法如下所示:

To isolate the error, I tried to comment out everything. The main method looks like this:

TextWriter tt = new StreamWriter(@"C:\startup.text", true);
tt.WriteLine("Starting up the service");
tt.Close();

ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[] 
   { 
       new MyService()
   };

TextWriter tt2 = new StreamWriter(@"C:\startup.text", true);
tt2.WriteLine("Run...");
tt2.Close();

它将启动服务"和运行..."打印到日志文件中.我还剥离了 MyService 的内部,因此它是空的.任何代码都有一个 try/catch,现在减少到一些像上面那样的日志行.我从不输入 catch 语句,它会记录它.

It prints out both "Starting up the service" and "Run..." to the log file. I also stripped the inside of MyService so it's empty. There is a try/catch around any code, which now is reduced to some log lines like above. I never enters the catch statement, which would have logged it.

OnStart 中的所有内容都已注释掉:

Everything in OnStart has been commented out:

protected override void OnStart(string[] args)
{
}

所以我基本上没有想法了.我认为错误是因为 Start 方法永远不会完成(或不会在 30 秒内完成).是否有其他方法被调用?任何想法表示赞赏.

So I'm basically out of ideas. I thought the error was because the Start method never finishes (or doesn't within 30 seconds). Is there some other method that is called? Any ideas are appreciated.

额外信息: MyService 中的构造函数为空.如果我插入一些 Thread.Sleep(5000) 行,则需要更长的时间才能弹出有关 Error 1053 的错误消息.Main 方法似乎必须退出(没有错误).

Extra info: The constructor in MyService is empty. If I insert some Thread.Sleep(5000) lines, then it takes longer beofre the error message about Error 1053 pops up. The Main method seems to have to exit (without error).

推荐答案

您缺少 ServiceBase.Run 调用:

ServiceBase[] servicesToRun = new ServiceBase[]
                                { 
                                    new MyService() 
                                };
ServiceBase.Run(servicesToRun);

订阅未处理的异常通知也可能是个好主意:

It might also be a good idea to subscribe to unhandled exceptions notification:

static void Main() {
    ...
    AppDomain.CurrentDomain.UnhandledException 
                                      += CurrentDomain_UnhandledException;
    ...
}

private static void CurrentDomain_UnhandledException(
                                                 Object sender, 
                                                 UnhandledExceptionEventArgs e) {

    if (e != null && e.ExceptionObject != null) {
        // log exception:
    }
}

并将以下 try/catch 添加到 OnStart 因为 .NET/SCM 吞下异常:

And add following try/catch to OnStart because .NET/SCM swallows exceptions:

protected override void OnStart(String[] args) {
    try {

    } catch(Exception e) {
        // log exception:
        throw;
    }
}

这篇关于Windows 服务无法启动(错误 1053)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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