Windows服务无法启动错误1053:服务未及时响应启动或控制请求'

时间:2014-05-14 12:20:32

标签: c# .net service windows-services

我目前正在使用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提示符installlutil servicename.exe安装我的服务。 (作为参考,我在本文的底部包含了OnStart服务的构造函数。)

然而,当我尝试启动该服务时,我收到以下错误: error

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

Error Error

我已经尝试了很多东西,包括但不限于从构造函数中移除调试行(如其他人所建议的那样),重新启动,从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();
}

2 个答案:

答案 0 :(得分:4)

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

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

请参阅this MSDN page。这个页面和子页面非常有助于我的Windows服务安装和运行。它引用了Visual Studio 2003,但对我的Visual Studio 2013,.net 4.5.1 windows服务应用程序来说效果很好。

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

脚本的文件位置是〜/ repoRoot / Tools ,解决方案位于〜/ repoRoot / src 这是我的powershell脚本: 附:我只在以管理员身份运行的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

希望这有帮助!

答案 1 :(得分:0)

我几天前遇到这个问题,我试图在Windows 2012服务器上安装.Net 4.5服务。 无论如何, 我安装到.Net 3.5框架到Windows Service 2012它工作正常。