1

Got a windows service and need to recreate it with project build.

In pre event:

sc query "Service" | findstr /i running | if "%errorlevel%"=="0" (sc stop "Service")

sc query "Service" | findstr /i running | if "%errorlevel%"=="0" (sc delete "Service")

In post event:

C:\Windows\Microsoft.NET\Framework64\v4.0.30319\InstallUtil.exe $(TargetPath)

net start "Service"

but i have issues like

Error 1 The command "C:\Windows\Microsoft.NET\Framework64\v4.0.30319\InstallUtil.exe D:...\bin\Debug\Host.exe

net start "Service" " exited with code 2. Host

any help, please?

RBT
  • 24,161
  • 21
  • 159
  • 240
Roar
  • 2,117
  • 4
  • 24
  • 39

3 Answers3

4

I was facing the very same issue. In my case it was happening when you try to stop a windows service using sc stop "Service" command when the service is already in stopped state.

In case there is an error while executing the command so command shell returns non-zero value (mostly 1 or an integer error code) as return code based on which Visual Studio stops further build process.

Since the service is already in stopped state so what we want is that Visual Studio should simply ignore the return code. You can achieve it either way as mentioned below. Simply append it at the end of your pre-build event code:

SET ERRORLEVEL = 0

Or

EXIT 0

This forcefully returns 0 (success) code even in case of any error when the service is already in stopped state. So my complete Pre-build event code looks like this:

IF "Debug" == "$(ConfigurationName)" ( 
   sc query "W3SVC" | findstr "RUNNING" &  if %errorlevel% == 0 (net stop "World Wide Web Publishing Service")
   EXIT 0

My intention was to stop the IIS service only for debug build.

RBT
  • 24,161
  • 21
  • 159
  • 240
0

Don't forget that the scripts in Build events are really nothing more than shell scripts, so spaces in paths (or assemblies, etc.) will screw it up if you don't surround with [double] quotes.

What I do is use the macro ${TargetPath}, etc. in a shell, entering the commands exactly as they would be expanded, first to make sure it is correct. There is where you will see whether the space issue has got you.

I agree that the VS interface doesn't give a lot of specific feedback, but in fairness, it is really just forking off to a shell, and displaying what the shell returns after it is done with all lines.

So, an errorlevel ('exit code' to VS) on one line is tossed for a summary error level a the end.

Here are 2 batch files I put in my project root, "Copy If Newer" to output dir, and run on command line:

.\startDeploy.bat

C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\InstallUtil.exe SomeService.exe
net start "Some Server"

...and...

.\stopDeploy.bat

net stop "Some Server"
    C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\InstallUtil.exe /LogToConsole=true /u SomeService.exe

If you have a better way to do it (I like your use of sc), then just rinse, lather, repeat. When you are sure things are good, then convert into the Build events window(s). I don't do this until I am ready to deploy.

user229044
  • 232,980
  • 40
  • 330
  • 338
0

This works for me...

"C:\Windows\Microsoft.NET\Framework64\v4.0.30319\InstallUtil.exe" /u $(TargetFileName)
"C:\Windows\Microsoft.NET\Framework64\v4.0.30319\InstallUtil.exe" $(TargetFileName)
 net start "Replace With Service Name"

I place these commands in the Post-Build section of the Build Events.

The first line will uninstall a pre-existing instance of your service. This will not fail if the service does not exist so no need to query the service first. You also do not need to stop the service, the uninstall will do that automatically.

The second line will install/re-install your service.

The third line will start the service. Make sure to change this to the name of the service without any file extensions.

Hope this helps.

Fütemire
  • 1,705
  • 1
  • 26
  • 21
  • If the service is currently running then the post-build event won't get hit because the process is currently in use. Therefore, it will fail. You should add a pre-build event to stop the service first. – Dan Atkinson Oct 10 '18 at 08:18
  • Like I said it works for me and in my case it does hit and stops the service automatically. All situations are not the same so just because it may not work in your case doesnt mean its not a solution in someone else's. Thanks for the down vote. =\ – Fütemire Oct 11 '18 at 19:36