Experimente a linha de comando de pós-construção do próprio Visual Studio .
Tente adicionar isso na pós-construção:
@echo off
sc query "ServiceName" > nul
if errorlevel 1060 goto install
goto stop
:delete
echo delete
sc delete "ServiceName" > nul
echo %errorlevel%
goto install
:install
echo install
sc create "ServiceName" displayname= "Service Display Name" binpath= "$(TargetPath)" start= auto > nul
echo %errorlevel%
goto start
:start
echo start
sc start "ServiceName" > nul
echo %errorlevel%
goto end
:stop
echo stop
sc stop "ServiceName" > nul
echo %errorlevel%
goto delete
:end
Se o erro de compilação com uma mensagem como esta Error 1 The command "@echo off sc query "ServiceName" > nul
por diante, Ctrl+ Centão Ctrl+ Va mensagem de erro no Bloco de Notas e observe a última frase da mensagem.
Pode ser dizer exited with code x
. Procure o código de algum erro comum aqui e veja como resolvê-lo.
1072 -- Marked for deletion → Close all applications that maybe using the service including services.msc and Windows event log.
1058 -- Can't be started because disabled or has no enabled associated devices → just delete it.
1060 -- Doesn't exist → just delete it.
1062 -- Has not been started → just delete it.
1053 -- Didn't respond to start or control → see event log (if logged to event log). It may be the service itself throwing an exception.
1056 -- Service is already running → stop the service, and then delete.
Mais sobre códigos de erro aqui .
E se o erro de compilação com mensagem como esta,
Error 11 Could not copy "obj\x86\Debug\ServiceName.exe" to "bin\Debug\ServiceName.exe". Exceeded retry count of 10. Failed. ServiceName
Error 12 Unable to copy file "obj\x86\Debug\ServiceName.exe" to "bin\Debug\ServiceName.exe". The process cannot access the file 'bin\Debug\ServiceName.exe' because it is being used by another process. ServiceName
abra o cmd e tente matá-lo primeiro com taskkill /fi "services eq ServiceName" /f
Se tudo estiver bem, F5deve ser suficiente para depurá-lo.