Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Event Build pre & post to stop and restart Windows service

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
)
like image 668
wonea Avatar asked Mar 14 '11 16:03

wonea


People also ask

What is build events in Visual Studio?

MSBuild is the build engine that Visual Studio uses to process your project file when it performs a build. A build event in the IDE results in an MSBuild target in the project file. You can use any MSBuild property that is available in the target in your project file (for example, $(OutDir) or $(Configuration) ) .

What is a build event?

A build event is a command that MSBuild performs at a particular stage in the build process. The pre-build event occurs before the build starts; the pre-link event occurs before the link step starts; and the post-build event occurs after the build successfully ends.

What is PreBuild and PostBuild?

PreBuild and PostBuild aren't only used for hiding or unhiding pages. The main difference really is that PreBuild fires before any peoplecode code event on all the rows and fields in the component such as FieldDefault and RowInit. PostBuild runs after that.

What are post build events?

Pre/Post build events are useful when we wish to perform some operation before/after a project is built. These operations are nothing but the Shell commands being used from the command line. A build event can be formed using a single, multiple, or conditional commands.


2 Answers

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.

like image 70
SDM Avatar answered Oct 01 '22 17:10

SDM


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

like image 20
Luuk Krijnen Avatar answered Oct 01 '22 17:10

Luuk Krijnen