Windows无法在Win Server 2008 R2 SP1上启动服务(错误1053)

jwa*_*zko 13 .net windows-services windows-server-2008-r2

这个问题似乎被广泛讨论,但我在特定情况下找到解决方案时遇到了问题.

我的服务设置为在Local System帐户下运行.在我的第一台安装了Windows 7 SP1(64位)的计算机上,一切都按预期工作.但是,在我尝试使用Windows Server 2008 R2 SP1(64位)在我的第二台机器上启动服务之后,甚至没有一秒钟通过,我正面临着这个恼人的错误:

Windows could not start the ProService service on Local Computer
Error 1053: The service did not respond to the start or control request in a timely fashion
Run Code Online (Sandbox Code Playgroud)

System Log显示2项:

The ProService service failed to start due to the following error: 
The service did not respond to the start or control request in a timely fashion.
Run Code Online (Sandbox Code Playgroud)

和:

A timeout was reached (30000 milliseconds) while waiting for the ProService service to connect.
Run Code Online (Sandbox Code Playgroud)

实施如下:

Program.cs中:

static void Main()
{
    AppDomain.CurrentDomain.UnhandledException += CurrentDomainUnhandledException;
    ServiceBase.Run(new ServiceBase[] { new ProService() });
}

static void CurrentDomainUnhandledException(object sender, UnhandledExceptionEventArgs e)
{
    if (e != null && e.ExceptionObject != null)
    {
        Logger.Record(e.ExceptionObject);
    }            
}
Run Code Online (Sandbox Code Playgroud)

ProService.cs:

public ProService()
{
    InitializeComponent();
}

protected override void OnStart(string[] args)
{
    try
    {
        RequestAdditionalTime(10000);
        FireStarter.Instance.Execute();
    }
    catch (Exception e)
    {
        Logger.Record(e);
        throw;
    }            
}
Run Code Online (Sandbox Code Playgroud)

OnStart方法只是启动一个新线程,因此执行几乎没有时间加载.RequestAdditionalTime为了以防万一,我使用了陈述 - 拒绝这件事作为我问题的根源.此外,正如您所看到的,我正在处理所有异常,但在启动期间没有异常写入我的自定义服务事件日志(顺便说一句:日志记录正在第一台win7计算机上运行).如何调查发生了什么?

jwa*_*zko 10

我通过几个步骤弄清楚了发生了什么:

  1. 我已经编写了控制台应用程序 - 包装器,它基本上是OnStart从服务方法中完成的.
  2. 我已经执行了控制台应用程序 - 应用程序正常启动
  3. 我已将服务配置文件的内容复制到控制台应用程序配置文件中.
  4. 我再次执行了控制台应用程序 - 应用程序默默地立即中止(与我们的服务行为类似,出了点问题).
  5. 我比较了2个配置 - 来自服务,原始来自控制台应用程序.结果我发现了一些差异.其中,有这样一个条目 - 问题的根源(删除后,一切正常):

    <startup>
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>
    
    Run Code Online (Sandbox Code Playgroud)

所以基本上我有过时的配置文件.之前的服务是在.NET 4.5下编译的,然后我将框架更改为4.0并将文件部署到服务器,但是保留了以前的配置文件(我已经更改了目标框架,因为我的开发机器有.NET 4.5,而服务器则是不).我不会想到显示的行会在没有任何合理信息的情况下隐藏所有问题,这可能有助于跟踪混乱.