简体   繁体   English

使用NAnt在构建脚本中安装/卸载Windows服务

[英]Install/Uninstall a Windows Service in a build script with NAnt

NAnt是否可以使用InstallUtil实用程序或其他工具来安装或卸载Windows服务?

You can call Nant's exec task to call InstallUtil and can pass parameters to install or uninstall a service easily 您可以调用Nant的exec任务来调用InstallUtil,并可以传递参数来轻松安装或卸载服务

 <target name="install-service">
    <exec program="${framework::get-framework-directory('net-2.0')}\InstallUtil.exe">
      <arg value="-i" />
      <arg value="/name=V1" />
      <arg value="C:\Service\SomeService.exe" />      
    </exec>
  </target>

Nant or MSBuild? Nant还是MSBuild? What's the problem with just running installutil yourself - that's what you'd do in MSBuild. 仅自己运行installutil会有什么问题-这就是您在MSBuild中所做的。 (In general, builds dont do the installs for things like this as rule as typically your build should be able to run on a random build server). (通常,构建不按照此类规则进行安装,因为通常您的构建应能够在随机构建服务器上运行)。

Another option, which would take installutil out of the equation is adding a self-install option to your service like this (have a search for more by looking for self install windows service ) 另一个使installutil不起作用的选项是像这样向您的服务中添加一个自安装选项 (可以通过查找self install windows service来搜索更多内容)

If your service can be installed at different places, you can also uninstall it through its name using SC.EXE, as follows: 如果您的服务可以安装在不同的位置,则还可以使用SC.EXE通过其名称将其卸载,如下所示:

<property name="serviceName" value="Name of the service"/>
<exec program="sc" failonerror="false" verbose="true" if="${service::is-installed(serviceName,'.')}">
 <arg value="delete"/>
 <arg value="${serviceName}"/>
</exec>

If you use the TopShelf Project in your application to host your services, you can get command-line based tools for installing / removing the services without needing InstallUtil. 如果您在应用程序中使用TopShelf项目来承载服务,则可以使用基于命令行的工具来安装/删除服务,而无需InstallUtil。

ServiceName.exe service install ServiceName.exe service uninstall ServiceName.exe服务安装ServiceName.exe服务卸载

And you can run the service directly and get a nice console window that you can CTRL+C to stop. 而且,您可以直接运行该服务,并获得一个漂亮的控制台窗口,您可以通过CTRL + C停止该窗口。 You can integrate this directly into nant or msbuild by executing the program. 您可以通过执行程序将其直接集成到nant或msbuild中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM