views:

24

answers:

1

I am setting up a project to run under the teamcity integration server on windows server 2003 (with sp2). The integration tests run using msbuild with the latest version of msbuildtasks v1.2.0.306. All code is build with .net 2.0.

In our build.xml file we remove all the services used by the systems, rebuild them all recreate them and then run a bunch of nunit tests to make sure they play well together.

To remove the services we use the UninstallAssembly task like this:

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

When I look at the build output this runs the installUtil command:

C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\InstallUtil.exe /uninstall ..\src\FolderName\ProjectName\bin\Debug\ProjectName.exe

We then re-install the service using the InstallAssembly task:

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

this is often fine but sometimes we get this error:

error MSB6006: "InstallUtil.exe"

exited with code -1.

I have a look and the service is there but it cannot be started/stoped/etc.

If I try to delete with sc delete servicename I get the error

[SC] DeleteService FAILED 1072:

The specified service has been marked for deletion.

When a service is in this "marked for deletion" state is there any way to get rid of it?

I found this note from microsoft that says to restart the computer, this works but we don't really want to restart our integration testing server between builds.

edit:

This problem seems to happen when on the previous run the service failed to start. eg. where we get the error:

ServiceName service is starting ...

f:\TeamCityUser\Checkout\trunk\dev\build.xml(187, 5): Cannot start service ServiceName on computer 'COMPUTERNAME'.

So it looks like it is after failing to start the service when we cannot remove the service until after a reboot. I believe in this instance the reason the service didn't start was because the constructor of the main service class threw a FileNotFoundException. The class is derived from ServiceBase.

I would still like to know how to delete a service without having to reboot.

+1  A: 

As indicated in the note, you should stop the service before uninstalling it. To do so you could use the ServiceController task to stop the service and ServiceQuery to check that it has been stopped.

<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>
madgnome
Thanks, we are stopping the services before shutting them down but not waiting for them to stop. After further investigation it seems that in the run before this problem the service failed to start - I'll add some details to my question shortly.
Adam Butler