49
votes

SC.exe and InstallUtil both install/uninstall windows services. But they don't seem to work the same way.

What is the difference?


For instance InstallUtil fails (some file or dependency not found error) while Sc create happily installs the service. Too add to the strangeness; the service doesn't show up if I run net start in the console. But it does show up in the services GUI. Variants of this happen when I try to uninstall.

I have written the service myself and earlier versions work. Dotnet3.5.

5
Here is a caveat: uninstallation (can't remember if it is sc.ex or installUtil) isn't possible if you have the services open in the control panel.LosManos
Maybe the OP meet the same error with me: when using InstallUtil, I tried to configure the service name in App.config. It turns out that we can't install the service if we read from the config file (must be a dependency thing). In the end we decide to hardcode the service name. Then switch to SCHoàng Long

5 Answers

28
votes

Yes, installing a service isn't particularly complicated. It just takes writing a handful of registry keys. You can have a look-see with Regedit.exe, navigate to HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services.

Sc.exe can write these keys too, using the supplied command line arguments. Nevertheless, this is not the right way to do it. The point of InstallUtil.exe is that it can activate custom installation code. Code that the service author wrote. Which is not that uncommon, services tend to stuff config info in their registration keys for their own use. You'll see plenty of evidence for that when you have a look with Regedit.

15
votes

I prefer sc.exe over installutil.exe.

InstallUtil forces you to add the dreadful ProjectInstaller class (I believe) and hardcode there the service name and service description.

InstallUtil makes it very hard to put two versions of the same service running in the same machine at the same time.

That's why I just don't use InstallUtil.exe at all. Also because of previous responses: you need it to be in your deploy package. sc.exe is already in any Windows Xp and above (I believe).

5
votes

Main difference is that InstallUtil is not utility meant for service installation but as general installer tool. From MSDN pages you can see that:

"The Installer tool is a command-line utility that allows you to install and uninstall server resources by executing the installer components in specified assemblies. This tool works in conjunction with classes in the System.Configuration.Install namespace."

So it can install service but it has many many many other benefits. Creating executables based on Installer Class gives you programatic control of whole installation/uninstallation procedure. ServiceInstaller and ServiceProcessInstaller, for instance, are used for service installation.

'Sc' utility is used for service control and 'create' command will just create service based on chosen executable.

In your example
1. It is not meant to be installed with InstallUtil and error response should be quite clear about it.
2. InstallUtil fails because of a bug in installation code and using sc create will probably create a faulty service for you. Check into {exe_name}.InstallLog for details.

2
votes

From the uninstall usage experience: sc.exe under windows 7 removes the entry from the list immediately, while after uninstalling with installutil there is a need for restart

2
votes

While InstallUtil is the preferred way to go with .NET services, one of it's shortcomings is it won't pick up binding redirects from your app.config, which in certain circumstances, can cause the install to fail. That's where using SC might gain you some benefit, at the expense of not being able to run code at install time.

Unfortunately for the OP, TopShelf didn't exist at the time of his question. It works around the shortcomings of both SC and InstallUtil, and allows the service to start up with the debugger attached when starting it in Visual Studio. Plus, it's a lot easier to type myservice install than have to drill down to the specific folder for InstallUtil, or type in a ton of parameters for SC.