代码之家  ›  专栏  ›  技术社区  ›  Adam Butler

使用msbuildtasks UninstallAssembly时,服务仅排队等待删除,服务器需要重新启动

  •  1
  • Adam Butler  · 技术社区  · 15 年前

    在我们的生成.xml我们删除系统使用的所有服务,重建它们,重新创建它们,然后运行一系列nunit测试,以确保它们能够很好地协同工作。

    要删除服务,我们使用UninstallAssembly任务,如下所示:

    <UninstallAssembly
       AssemblyFiles='..\src\FolderName\ProjectName\bin\Debug\ProjectName.exe'
       ContinueOnError='true'>
    </UninstallAssembly>
    

    C:\窗口\微软.NET\框架\v2.0.50727\InstallUtil.exe/卸载..\src\FolderName\ProjectName\bin\Debug\项目名称.exe

    然后使用InstallAssembly任务重新安装服务:

    <InstallAssembly
      AssemblyFiles='..\src\FolderName\ProjectName\bin\Debug\ProjectName.exe'>
    </InstallAssembly>
    

    这通常很好,但有时会出现以下错误:

    错误MSB6006:“InstallUtil.exe"

    我看了一下,服务在那里,但不能启动/停止/等等。

    sc delete servicename 我得到了错误

    [SC]DeleteService失败1072:

    已标记指定的服务

    this note

    编辑:

    此问题似乎发生在上一次运行服务未能启动时。我们得到错误的地方:

    ServiceName服务正在启动。。。

    5) :无法启动服务ServiceName 在计算机“COMPUTERNAME”上。

    因此,它看起来像是在启动服务失败之后,我们无法删除服务,直到重新启动之后。我相信在这个实例中,服务没有启动的原因是因为主服务类的构造函数抛出了 FileNotFoundException . 类派生自 ServiceBase

    我仍然想知道如何删除服务而不必重新启动。

    1 回复  |  直到 15 年前
        1
  •  2
  •   Julien Hoarau    15 年前

    如图所示 note ,您应该在卸载服务之前停止它。要做到这一点,你可以使用 ServiceController ServiceQuery 检查是否已停止。

    <PropertyGroup>
      <ServiceName>Service</ServiceName>
    </PropertyGroup>
    
    <Target Name="StopService">
      <ServiceController ServiceName="$(ServiceName)" Action="Stop" />
    
      <CallTarget Targets="WaitStop"/>
    </Target>
    
    <Target Name="WaitStop">
      <Sleep Milliseconds="1000" />
    
      <ServiceQuery ServiceName="$(ServiceName)">
        <Output TaskParameter="Status" PropertyName="ServiceStatus" />
      </ServiceQuery>
    
      <!-- If the service isn't stopped we execute WaitStop again -->
      <MSBuild Condition="'$(ServiceStatus)' != 'Stopped'"
               Projects="$(MSBuildProjectFile)"
               Targets="WaitStop"/>
    </Target>
    
    <Target Name="UninstallService" DependsOnTargets="StopService">
      <UninstallAssembly
        AssemblyFiles="..\src\FolderName\ProjectName\bin\Debug\ProjectName.exe"
        ContinueOnError="false">
      </UninstallAssembly>
    </Target>