Question

I'm new to msbuild and currently I'm trying to create msbuild script that will deploy my C# windows service to remote test server.

I'm thinking about using sc.exe utility for this purpose. Reading about it I didn't find a way to check whether windows service is installed on a remote server. If the service is installed then I need to stop it and update necessary files, otherwise I need to register the service.

P.S. For release builds I plan to use WiX to create MSI package.

Was it helpful?

Solution

You need MSBuild Comminity Tasks. In latest build exists an example in MSBuild.Community.Tasks.v1.2.0.306\Source\Services.proj. It will solve first part of your question:

<PropertyGroup>
    <MSBuildCommunityTasksPath>$(MSBuildProjectDirectory)\MSBuild.Community.Tasks\bin\Debug</MSBuildCommunityTasksPath>
</PropertyGroup>

<Import Project="$(MSBuildProjectDirectory)\MSBuild.Community.Tasks\MSBuild.Community.Tasks.Targets"/>

<Target Name="Test">
    <CallTarget Targets="DoesServiceExist" />
    <CallTarget Targets="GetServiceStatus" />
    <CallTarget Targets="ServiceControllerStuff" />
</Target>

<Target Name="DoesServiceExist">
    <ServiceQuery ServiceName="MSSQLServer123" MachineName="127.0.0.1" >
        <Output TaskParameter="Exists" PropertyName="Exists" />
        <Output TaskParameter="Status" PropertyName="ServiceStatus" />
    </ServiceQuery>
    <Message Text="MSSQLServer Service Exists: $(Exists) - Status: $(ServiceStatus)"/>
</Target>

<Target Name="GetServiceStatus">
    <ServiceQuery ServiceName="MSSQLServer" MachineName="127.0.0.1">
        <Output TaskParameter="Status" PropertyName="ResultStatus" />
    </ServiceQuery>
    <Message Text="MSSQLServer Service Status: $(ResultStatus)"/>
</Target>

<Target Name="ServiceControllerStuff">
    <ServiceController ServiceName="aspnet_state" MachineName="127.0.0.1" Action="Start" />
    <ServiceController ServiceName="aspnet_state" MachineName="127.0.0.1" Action="Stop" />
</Target>

Those MSBuild task is just a wrapper around .Net class ServiceController. Take a look for documentation to understand how it works and how you can configure it in details.

Second part includes installing service. For that purpose sc.exe suits very well.

OTHER TIPS

A complete solution is posted here. May help future visitors.

Update: Link updated as the other blogging service went down.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top