Pruebe la propia línea de comandos de eventos posteriores a la compilación de Visual Studio .
Intente agregar esto en la post-construcción:
@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
Si el error de compilación con un mensaje como Error 1 The command "@echo off sc query "ServiceName" > nul
así sucesivamente, Ctrl+ CentoncesCtrl + Vel mensaje de error en el Bloc de notas y mira la última oración del mensaje.
Podría estar diciendo exited with code x
. Busque el código en algún error común aquí y vea cómo resolverlo.
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.
Más sobre códigos de error aquí .
Y si el error de compilación con un mensaje como este,
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
abre cmd y luego intenta matarlo primero con taskkill /fi "services eq ServiceName" /f
Si todo va bien, F5debería bastar con depurarlo.