MSBuild。检查是否安装了windows服务

发布于 2024-10-17 22:02:35 字数 226 浏览 1 评论 0原文

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

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

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

为人所爱 2024-10-24 22:02:35

您需要 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>

这些 MSBuild 任务只是 .Net 类 ServiceController。查看文档以了解其工作原理以及如何详细配置它。

第二部分包括安装服务。为此,sc.exe 适合很好

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.

雨落□心尘 2024-10-24 22:02:35

此处发布了完整的解决方案。可以帮助未来的访客。

更新:随着其他博客服务的关闭,链接已更新。

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文