4

我是 msbuild 的新手,目前我正在尝试创建 msbuild 脚本,它将我的 C# windows 服务部署到远程测试服务器。

我正在考虑为此目的使用 sc.exe 实用程序。阅读它我没有找到一种方法来检查远程服务器上是否安装了 Windows 服务。如果该服务已安装,那么我需要停止它并更新必要的文件,否则我需要注册该服务。

PS 对于发布版本,我计划使用 WiX 创建 MSI 包。

4

2 回答 2

9

您需要MSBuild 社区任务。在最新版本中,MSBuild.Community.Tasks.v1.2.0.306\Source\Services.proj中有一个示例。它将解决您问题的第一部分:

<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.

于 2011-02-17T09:52:35.680 回答
0

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

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

于 2013-02-08T04:35:38.830 回答