【问题标题】:Windows Service won't start 'Error 1053: The service did not respond to the start or control request in timely fashion'Windows 服务无法启动“错误 1053:服务未及时响应启动或控制请求”
【发布时间】:2014-07-02 12:07:53
【问题描述】:

我目前正在开发 Windows 服务作为我们应用程序的非 GUI 部分。

我编写了一个服务,它在调试条件下完美运行。这意味着我通过如下方式调试我的静态 void Main():

#if(!DEBUG)
    ServiceBase[] ServicesToRun;
    ServicesToRun = new ServiceBase[] 
    { 
        new TRAservice() 
    };
    ServiceBase.Run(ServicesToRun);
#else
    TRAservice Service = new TRAservice();
    Service.CanHandlePowerEvent = false;
    Service.CanHandleSessionChangeEvent = false;
    Service.CanPauseAndContinue = true;
    Service.CanShutdown = true;
    Service.CanStop = true;
    Service.ServiceName = "TRAservice";
    Service.AutoLog = false;
    Service.SetMethod();
    Service.TRAmethod();
#endif

为了进行一些实施测试,我添加了一个 ProjectInstaller,并通过 VStools cmd 提示符 installutil servicename.exe 安装我的服务。 (作为参考,我在这篇文章的底部包含了带有 OnStart 的服务的构造函数。)

但是,当我尝试启动服务时,我收到以下错误:

在我的事件查看器中,我收到 4 条消息(2 条信息(Windows 错误报告),2 条错误):

我尝试了很多东西,包括但不限于从构造函数中删除调试行(如其他地方所建议的那样)、重新启动、从 Release 安装(而不是 Debug)...

我会接受任何建议来解决这个问题。提前致谢。

public TRAservice()
{
    InitializeComponent();

    CanHandlePowerEvent = false;
    CanHandleSessionChangeEvent = false;
    CanPauseAndContinue = true;
    CanShutdown = true;
    CanStop = true;
    ServiceName = "TRAservice";
    AutoLog = false;

    sSource = "TRAservice";
    sLog = "TRAlog";

    if (!EventLog.SourceExists(sSource))
    {
        EventLog.CreateEventSource(sSource, sLog);
    }

    eventLog1 = new System.Diagnostics.EventLog();
    eventLog1.Source = sSource;
    eventLog1.Log = sLog;

    eventLog1.WriteEntry("Service started", EventLogEntryType.Information, 1, 100);

    scheduleTimer = new Timer();
    scheduleTimer.Interval = 10;
    scheduleTimer.Elapsed += new ElapsedEventHandler(scheduleTimer_Elapsed);;
}

protected override void OnStart(string[] args)
{
    eventLog1.WriteEntry("Service started", EventLogEntryType.Information, 1, 100);
    flag = true;
    sw = true;
    lastRun = DateTime.Now;
    scheduleTimer.Start();
}

【问题讨论】:

  • 创建事件源应该在您的安装程序类中完成,而不是在您的服务类的构造函数中 - 服务将被 安装 并具有管理员权限,但它可能并不总是 在管理员权限下运行
  • 谢谢。当我实施您的建议时,我在安装服务时收到以下错误:安装阶段发生异常。 System.ArgumentException: Source TRAservice already exists on the local computer. 但是我将其从寄存器中删除,此错误不断弹出。有什么建议吗?

标签: c# .net service windows-services


【解决方案1】:

在我的 Windows 服务中,我添加了一个控制台应用程序项目并将其设为默认启动项目。我使用它来调试 Windows 服务启动的相同代码库。我在一个单独的类库项目中有工作代码。

我的 Windows 服务项目只有一个 service.cs 文件、projectinstaller.cs 和我已复制到我的控制台应用程序的 app.config 文件。我没有使用任何编译器指令来运行不同的代码集。

请参阅this MSDN page。此页面和子页面对于安装和运行我的 Windows 服务非常有帮助。它引用了 Visual Studio 2003,但适用于我的 Visual Studio 2013、.net 4.5.1 Windows 服务应用程序。

我所做的配置是创建一个名为 Debug - Local 的新解决方案配置。我还创建了一个 powershell 脚本来卸载服务、构建解决方案并将服务安装为此配置。这样我就可以在日常工作中使用 Debug 配置,而不必担心文件被锁定。

脚本的文件位置在~/repoRoot/Tools,解决方法在~/repoRoot/src 这是我的PowerShell脚本: p.s.我只在以管理员身份运行的 powershell 窗口中运行它,并没有尝试以管理员身份运行部分...

#######################
# Run as Administrator
#######################
If (-NOT ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator"))
{   
#"No Administrative rights, it will display a popup window asking user for Admin rights"

$arguments = "& '" + $myinvocation.mycommand.definition + "'"
Start-Process "$psHome\powershell.exe" -Verb runAs -ArgumentList $arguments

break
}

cls

#############################################################
# Set environment variables for Visual Studio Command Prompt
#############################################################
function Set-VsCmd
{
    param(
        [parameter(Mandatory, HelpMessage="Enter VS version as 2010, 2012, or 2013")]
        [ValidateSet(2010,2012,2013)]
        [int]$version
    )
    $VS_VERSION = @{ 2010 = "10.0"; 2012 = "11.0"; 2013 = "12.0" }
    $targetDir = "c:\Program Files (x86)\Microsoft Visual Studio $($VS_VERSION[$version])\VC"
    if (!(Test-Path (Join-Path $targetDir "vcvarsall.bat"))) {
        "Error: Visual Studio $version not installed"
        return
    }
    pushd $targetDir
    cmd /c "vcvarsall.bat&set" |
    foreach {
      if ($_ -match "(.*?)=(.*)") {
        Set-Item -force -path "ENV:\$($matches[1])" -value "$($matches[2])"
      }
    }
    popd
    write-host "`nVisual Studio $version Command Prompt variables set." -ForegroundColor Yellow
}

Set-VsCmd 2013

#######################
# Stop windows service
#######################
$srvName = "Enterprise CQRS Service"
$serviceStop = Get-Service $srvName
"$srvName is now " + $serviceStop.status
Stop-Service $srvName

$TestService = Get-Service $srvName | Select-Object 'Status'
While($TestService | where {$_.Status -eq 'Running'}) {
    Write-Host '.'-NoNewLine 
  Sleep 2   
}
Write-Host "$srvName is now" $TestService.status -ForegroundColor Yellow

#################################
# Build Service as Debug - Local
#################################

# Set the solution file path
$scriptPath = Split-Path -parent $MyInvocation.MyCommand.Definition

$solutionFile = Join-Path $scriptPath "../src/Enterprise CQRS Service.sln"

# Build the Debug - Local configuration
msbuild $solutionFile /p:Configuration='Debug - Local'

##############################
# Uninstall & Instal Service
##############################

# Set the exe file path
$exePath = Join-Path $scriptPath "../src/Enterprise.Services.Windows/bin/Debug - Local/Enterprise.Services.Windows.exe"

# Uninstall the service
installutil /u $exePath

# Install the service
installutil $exePath

# Start the service
$servicePrior = Get-Service $srvName
"$srvName is now " + $servicePrior.status
Start-Service $srvName
$serviceAfter = Get-Service $srvName
$foregroundColor = "Green"
if($serviceAfter.status -ne "Running")
{
    $foregroundColor = "Red"
}
Write-Host "$srvName is now" $serviceAfter.status -ForegroundColor $foregroundColor

希望这会有所帮助!

【讨论】:

  • 非常感谢您的意见!既然我终于可以收工了,明天我会调查一下。
【解决方案2】:

几天前我遇到了这个问题,我试图在 Windows 2012 服务器上安装 .Net 4.5 服务。 反正, 我将 .Net 3.5 框架安装到 Windows Service 2012,它运行良好。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-12-20
    • 1970-01-01
    • 1970-01-01
    • 2011-03-28
    • 2014-08-05
    • 2011-01-05
    • 2011-12-29
    相关资源
    最近更新 更多