活动筹备及筹备发布停止并重新启动 Windows 服务
我一直在尝试使用构建事件来启动和停止我的项目中正在构建的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
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.
尝试在预构建代码的第一行使用左括号
Try using the opening parenthese on the first line of your pre-build code
条件语句不需要双引号(“”)
它应该是这样的
The conditional statement doesn't require double-qoute ("")
It should be like
这就是我让它工作的方式:
(此解决方案是企业软件的一部分,其中一些 dll 文件被另一个应用程序重用)
模型是一个在服务项目中引用的项目,并且是构建的服务前。这就是为什么我们在模型的预构建事件中编写这些代码的原因:
模型预构建事件:
服务后-构建事件:
关于权限?
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:
Service Post-Build Event:
About permissions?
Prebuild:
Postbuild:
特别是对于预构建,这会忽略 Visual Studio 中尝试停止服务(如果未启动)的错误。
Prebuild:
Postbuild:
Specifically for the prebuild, this ignores the error in visual studio of trying to stop the service if it's not started.