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
)
wonea
  • 4,783
  • 17
  • 86
  • 139

4 Answers4

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.

SDM
  • 564
  • 5
  • 11
  • 4
    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. – Dan Bechard Feb 13 '13 at 18:46
  • 2
    @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. – Nelson Rothermel Apr 07 '14 at 17:50
1

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

It should be like

if $(ConfigurationName) == Debug (
 net stop myService
 ...
)
Andrew Chaa
  • 6,120
  • 2
  • 45
  • 33
  • 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. – Nelson Rothermel Apr 07 '14 at 17:51
  • Not sure. When I added "", it didn't work for me on visual studio – Andrew Chaa Apr 08 '14 at 14:42
  • 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. – Nelson Rothermel Apr 11 '14 at 21:14
1

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

Luuk Krijnen
  • 1,180
  • 3
  • 14
  • 37
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
Bizhan
  • 16,157
  • 9
  • 63
  • 101