活动筹备及筹备发布停止并重新启动 Windows 服务

发布于 2024-10-21 14:24:56 字数 601 浏览 6 评论 0原文

我一直在尝试使用构建事件来启动和停止我的项目中正在构建的 Windows 服务。然而对于预&后期构建失败,错误级别为 255。我尝试用预构建捕获此问题,但没有成功。

构建前

if "$(ConfigurationName)" == "Debug"
(
 net stop myService
 if errorlevel 2 
    if errorlevel 255 
        :exit

   :exit
)

构建后

if "$(ConfigurationName)" == "Release"
(
   copy $(TargetDir) C:\Media\Bin\$(ProjectName)
   if errorlevel 1 BuildEventFailed

   :BuildEventFailed
   mkdir C:\Media\Bin\$(ProjectName)

   copy $(TargetDir) C:\Media\Bin\$(ProjectName)
)
else if "$(ConfigurationName)" == "Debug"
(
   net start myService
)

I've been experimenting with using build events to start and stop Windows service that are being built in my project. However for the pre & post builds fail with an error level 255. I've tried catching this with the pre-build with no luck.

Pre-build

if "$(ConfigurationName)" == "Debug"
(
 net stop myService
 if errorlevel 2 
    if errorlevel 255 
        :exit

   :exit
)

Post-build

if "$(ConfigurationName)" == "Release"
(
   copy $(TargetDir) C:\Media\Bin\$(ProjectName)
   if errorlevel 1 BuildEventFailed

   :BuildEventFailed
   mkdir C:\Media\Bin\$(ProjectName)

   copy $(TargetDir) C:\Media\Bin\$(ProjectName)
)
else if "$(ConfigurationName)" == "Debug"
(
   net start myService
)

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

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

发布评论

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

评论(5

好多鱼好多余 2024-10-28 14:24:56

Joel Varty 的以下博客提供了我使用的解决方案:
使用构建事件重建 Windows 服务,而无需手动停止/启动它

唯一的问题是当您进行重建时。 Visual Studio 会在预生成事件触发之前清理文件。这当然会失败,因为服务仍在运行。但常规构建效果很好。希望这有帮助。

The following weblog by Joel Varty has a solution which I use:
Use Build Events to rebuild a Windows Service without having to manually stop/start it

The only problem is when you do a rebuild. Visual Studio cleans up the files before the pre-build event fires. This then off course fails because the service is still running. But regular builds work great. Hope this helps.

无言温柔 2024-10-28 14:24:56

尝试在预构建代码的第一行使用左括号

Try using the opening parenthese on the first line of your pre-build code

空‖城人不在 2024-10-28 14:24:56

条件语句不需要双引号(“”)

它应该是这样的

if $(ConfigurationName) == Debug (
 net stop myService
 ...
)

The conditional statement doesn't require double-qoute ("")

It should be like

if $(ConfigurationName) == Debug (
 net stop myService
 ...
)
风吹雨成花 2024-10-28 14:24:56

这就是我让它工作的方式:

(此解决方案是企业软件的一部分,其中一些 dll 文件被另一个应用程序重用)

模型是一个在服务项目中引用的项目,并且是构建的服务前。这就是为什么我们在模型的预构建事件中编写这些代码的原因:


模型预构建事件:

if not exist "$(SolutionDir)UI\bin\Debug\ServiceFolder" mkdir "$(SolutionDir)UI\bin\Debug\ServiceFolder"

net start | find "[Service Name]"

if ERRORLEVEL 0 (
net stop "Service Name"
"C:\Windows\Microsoft.NET\Framework\v4.0.30319\InstallUtil.exe" -u "$(SolutionDir)UI\bin\Debug\ServiceFolder\Service.exe"
)

exit 0
  • 在输出文件夹中创建一个目录
  • 按名称查找服务
  • 停止它
  • 卸载它
  • 退出 0 如果此处发生错误,则导致构建过程继续

服务后-构建事件:

xcopy /E /Y "$(ProjectDir)bin\Debug\*" "$(SolutionDir)UI\bin\Debug\ServiceFolder"
"C:\Windows\Microsoft.NET\Framework\v4.0.30319\InstallUtil.exe" "$(SolutionDir)UI\bin\Debug\ServiceFolder\Service.exe"
net start "Service Name"
  • 将服务所需的所有内容复制到不同的文件夹
  • 安装服务
  • 启动服务

关于权限?

  • Visual Studio 将自动请求提升权限

This is how I got it to work:

(this solution was a part of an enterprise software where some dll files are reused by another app)

Model is a project which is referenced in Service project and it is built before Service. Which is why we write these codes in Model's Pre-Build Events:


Model Pre-Build Event:

if not exist "$(SolutionDir)UI\bin\Debug\ServiceFolder" mkdir "$(SolutionDir)UI\bin\Debug\ServiceFolder"

net start | find "[Service Name]"

if ERRORLEVEL 0 (
net stop "Service Name"
"C:\Windows\Microsoft.NET\Framework\v4.0.30319\InstallUtil.exe" -u "$(SolutionDir)UI\bin\Debug\ServiceFolder\Service.exe"
)

exit 0
  • creates a directory in output folder
  • finds the service by name
  • stops it
  • uninstalls it
  • exit 0 causes the build process to continue if error occurs here

Service Post-Build Event:

xcopy /E /Y "$(ProjectDir)bin\Debug\*" "$(SolutionDir)UI\bin\Debug\ServiceFolder"
"C:\Windows\Microsoft.NET\Framework\v4.0.30319\InstallUtil.exe" "$(SolutionDir)UI\bin\Debug\ServiceFolder\Service.exe"
net start "Service Name"
  • copy everything needed for the service to a different folder
  • installs service
  • starts service

About permissions?

  • visual studio will ask for elevated permission automatically
星光不落少年眉 2024-10-28 14:24:56

Prebuild:

net stop "Capstone Service"
Exit /b 0

Postbuild:

net start "Capstone Service"
Exit /b 0
  • Exit:该命令用于终止当前运行的批处理脚本或子例程。
  • /b:指定从批处理文件或子例程中退出。
  • 0:指定退出代码。在这种情况下,0 表示成功完成或没有错误。

特别是对于预构建,这会忽略 Visual Studio 中尝试停止服务(如果未启动)的错误。

Prebuild:

net stop "Capstone Service"
Exit /b 0

Postbuild:

net start "Capstone Service"
Exit /b 0
  • Exit: This command is used to terminate the currently running batch script or subroutine.
  • /b: Specifies that the exit is from within a batch file or subroutine.
  • 0: Specifies the exit code. In this case, 0 indicates successful completion or no error.

Specifically for the prebuild, this ignores the error in visual studio of trying to stop the service if it's not started.

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