繁体   English   中英

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

[英]Windows Service won't start 'Error 1053: The service did not respond to the start or control request in timely fashion'

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

我写了一个服务,在Debug条件下,它完美地工作。 这意味着我通过使我的静态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条错误):

错误错误

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

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

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

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

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

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

希望这可以帮助!

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM