DeleteService doesn't actually delete the service until the program is closed.

  • Thread starter Thread starter UJ
  • Start date Start date
U

UJ

I have a program that will upgrade a service automatically. If the service
already exists, it will delete the service and then reinstall it through
code. To delete the service I'm using the DeleteService command which marks
the service as disabled but the service doesn't disappear until the program
shuts down. I've read on the web that it won't disappear until all
connections to the service manager have been closed. I close all my
connections yet it still is there until I close the program.

1. So I have two questions - does anybody know how to delete the service and
have it disappear immediately?
2. If I have a service and am upgrading it, do I need to remove it and
re-install it or can I just replace the executable?

TIA - Jeff.
 
1) This is probably an object lifetime issue. If you still have a
ServiceInstaller or ServiceController that references this service, you
will need to make sure that in has been cleaned up. You can acomplish
this by calling the Dispose() method which will explicitly invoke the
destructor and release associated resources.

2) You must remove and re-install. Services executables and
associated data are stored in a secure location in the registry.
Replacing the file on disk does not affect the service in the registry.
 
Thanks for your help. I finally found a spot where I wasn't closing the
service handle. Once I closed that, it works just as advertised.

J.
 
With most services you can just replace the exe, of course it has to be
stopped to do this (whihc you can automate) - but you dont typically have to
remove the service entry.

--
Regards

John Timney
ASP.NET MVP
Microsoft Regional Director
 
This is true if you know where the .NET ServiceInstaller tucked away
your executable. The trick is that if you replace the exe that you
used to install with InstallUtil, it will have no effect until you use
InstallUtil to uninstall and then reinstall your service. Of course if
you find where on the system it's putting your exe, you could just
replace that exe.
 
Back
Top