Prova la riga di comando degli eventi post-compilazione di Visual Studio .
Prova ad aggiungere questo in post-build:
@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 l'errore di compilazione con un messaggio come Error 1 The command "@echo off sc query "ServiceName" > nul
così via, Ctrl+ Cpoi Ctrl+ Vil messaggio di errore nel Blocco note e guarda l'ultima frase del messaggio.
Potrebbe dire exited with code x
. Cerca il codice in qualche errore comune qui e vedi come risolverlo.
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.
Maggiori informazioni sui codici di errore qui .
E se l'errore di compilazione con un messaggio come questo,
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
apri cmd, quindi prova a ucciderlo prima con taskkill /fi "services eq ServiceName" /f
Se tutto va bene, F5dovrebbe essere sufficiente per eseguire il debug.