Monday 1 February 2016

Start and Stop Windows Services on Visual Studio Build.


Introduction

If you have a windows service it is handle to get Visual Studio build to stop the service before build then restart it after it completes the build.


Setup your service

run this from an admin dos command prompt

sc.exe create myService binpath= "C:\services\MyService.exe" DisplayName= MyService



Pre-Build Event
Its in the project properties in Visual Studio, Build Events tab.

This will stop the service.

set svcname=MyService
net stop %svcname%
EXIT 0



Post-Build
Its in the project properties in Visual Studio, Build Events tab.

This copy the files built and start the service. Will also create the c:\services directory if it doesn't exist.

IF NOT EXIST "C:\services" MD "C:\services\";
attrib -r "C:\services\*.*" /s
:while1
Copy "$(TargetDir)*.*" "C:\services";
IF ERRORLEVEL 1 goto :while1



To Debug The service

Attach to the process number (PID) in the Services Tab in Windows Task Manager I've found to be the easiest way.





No comments:

Post a Comment

Comments are welcome, but are moderated and may take a wee while before shown.