【问题标题】:nant vs. msbuild: stopping a servicenant vs. msbuild:停止服务
【发布时间】:2011-01-12 21:49:40
【问题描述】:

我正试图决定在 MsBuild 与 Nant 战争中我站在哪一边。我开始:停止服务,部署一些文件,重新启动服务。只需查看这两个链接,在 Nant 中就更容易做到这一点。

MSBuild:Example of using Service Exists MSBuild task in Microsoft.Sdc.Tasks?

<target name="service_exists"> 
        <script language="C#"> 
                <references> 
                        <include name="System.ServiceProcess.dll" /> 
                </references> 
                <code><![CDATA[ 
                        public static void ScriptMain(Project project) { 
                                String serviceName = project.Properties["service.name"]; 
                                project.Properties["service.exists"] = "false"; 
                                project.Properties["service.running"] = "false"; 

                                System.ServiceProcess.ServiceController[] scServices; 
                                scServices = System.ServiceProcess.ServiceController.GetServices(); 

                                foreach (System.ServiceProcess.ServiceController scTemp in scServices) 
                                { 
         etc... 

南特:http://ryepup.unwashedmeme.com/blog/2007/01/04/restart-a-windows-service-remotely/

<!-- Send the stop request -->
<exec program="sc.exe">
  <arg line="\\server stop shibd_Default"/>
</exec>
<!-- Sleep a little bit, to give the service a chance to stop -->
<sleep seconds="5"/>
<!-- Send the start request -->
<exec program="sc.exe">
  <arg line="\\server start shibd_Default"/>
</exec>

我想知道 SO 社区是否同意我的观点。在 Nant 中完成类似这样的基本工作是否容易得多?当然看起来是这样的。 CDATA 块中的 C# 代码?怎么回事?

我们当前的构建过程是 a) 大量 bat 文件 b) 大量诅咒。我真的很想找到一个好的替代品,但是 MsBuild 的东西在我看来就像一个痛苦的世界。我认为要走的路是在 Nant 中构建脚本,然后使用 MsBuild 进行任何需要完成的 .NET 构建。

一个重要问题:在脚本运行之前,哪一个更擅长捕捉脚本中的错误?我正在考虑在这里推出自己的产品,这是其中非常重要的一部分:排列所有数据并确保在尝试运行之前它是有意义的。

【问题讨论】:

    标签: msbuild build-process nant


    【解决方案1】:

    在 msbuild 中,您还可以使用封装在 msbuild community tasks 中的 ServiceController 任务。

    【讨论】:

      【解决方案2】:

      您可以使用 MSBuild 轻松执行sc.exe ...

      <Exec Command="sc.exe \\server stop shibd_Default" />
      

      默认情况下,如果退出代码(sc.exe)不为零,这将“失败”,但可以自定义。

      【讨论】:

      • 你能把那个 Exec 包装在一个 foreach 中吗?也许这就是我帖子中奇怪的命令脚本的原因?
      • MSBuild 并没有真正的 foreach,因为它是声明性的,而不是命令性的。您需要执行类似&lt;Exec Command="sc \\server stop %(ServiceName.Identity)" /&gt; 之类的操作,其中您有一个&lt;ItemGroup&gt;&lt;ServiceName Include="service1;service2;service3" /&gt;...
      【解决方案3】:

      使用 Nant,还有 2 种其他方法可以停止服务,其中一种可以跟踪错误。

      第一个(使用 Net Stop):

      <exec program="net" failonerror="false"><arg value="stop"/><arg value="${serviceName}"/></exec>
      

      第二个(更干净):

      <servicecontroller action="Stop" service="${serviceName}" if="${service::is-installed(serviceName,'.') and service::is-running(serviceName,'.')}" />
      

      请注意,第二行验证服务已经存在并且正在运行,这允许跟踪任何奇怪的错误。

      【讨论】:

        【解决方案4】:

        除了针对 MSBuild 的@nulpptr's 答案之外,如果您没有使用社区任务的选项,您可能不得不求助于黑客来等待您的服务停止,然后再继续。如果您有资源工具包,则可以将EXEC 任务与sleep 命令一起使用。

        没有资源包?使用 ping 技巧...

        但是,如果您没有资源工具包,您可以使用 ping 技巧来强制延迟。例如,以下将使用sc 命令停止您的服务,然后暂停大约 5 秒:

        <Exec Command="sc.exe \\server stop shibd_Default" ContinueOnError="true" />
        <Exec Command="ping 127.0.0.1 -n 5 > nul" ContinueOnError="true" />
        

        【讨论】:

        • 感谢 ping 提示!
        猜你喜欢
        • 2010-11-06
        • 1970-01-01
        • 2012-06-20
        • 2010-11-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-05-05
        • 1970-01-01
        相关资源
        最近更新 更多