I have a C# service that requires to write a certain text to a SQL Server database before shutting down after OS restart. So the service MSSQL$SQLEXPRESS should not stop before my service and in order to achieve this, I have added a dependency of MSSQL$SQLEXPRESS using the command
sc.exe config MyService depend= MSSQL$SQLEXPRESS
However, it does not seem to work as I am still getting error
SHUTDOWN is in progress.
Login failed for user 'user'.
Cannot continue the execution because the session is in the kill state
when trying to write to the database in OnShutdown(). Sometimes the error is an "invalid handle" error..
What am I missing? Also, sc.exe qc MyService shows the dependency, but dependencies under services.msc shows nothing. Why?
From the docs:
Service Control Handler Function