Event Build pre & post to stop and restart Windows service
Asked Answered
W

5

18

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
)
Wehrle answered 14/3, 2011 at 16:5 Comment(0)
E
23

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.

Ethereal answered 4/7, 2011 at 15:35 Comment(2)
I prefer to have stop, copy, start, exit all in post-build since the service path probably shouldn't point directly to a /bin/Release folder. This also prevents the service from being stopped but never restarted if the build fails for other reasons.Harv
@Dan has the right answer. This also lets you easily switch to different build configs, branches, etc. without reinstalling/repointing the service. From a comment in SDM's link, you can also auto-install the service so that for new developers it just works.Nativity
A
1

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

Arabeila answered 28/3, 2011 at 10:0 Comment(0)
D
1

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

It should be like

if $(ConfigurationName) == Debug (
 net stop myService
 ...
)
Decompose answered 2/1, 2014 at 17:47 Comment(3)
I believe it's common practice to always use quotes in case the variable is empty, otherwise you can end up with if == Debug instead of if "" == "Debug". However, in this case $(ConfigurationName) should always have a value.Nativity
Not sure. When I added "", it didn't work for me on visual studioDecompose
You might be right. I've seen the practice many times in batch files and assumed that was the case. With a quick search online I didn't really find anything definitive.Nativity
R
0

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
Roa answered 1/9, 2017 at 14:42 Comment(0)
G
0

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.

Gangrene answered 28/6, 2024 at 17:43 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.