1

I am trying to install a windows service and start it afterwards.I do not understand why after a couple of cycles (install+uninstall) i cannot install or uninstall the service anymore since i get the error:

Another version of this product is already installed

but the service is no longer present in the Services window , nor is the installer present in the Programs section.

If i try to uninstall i get :

Error 1001: An exception occured while uninstalling.This exception will be ignored and the uninstall will continue.However the uninstall might not be fully uninstalled after the uininstall is complete.

I do not understand what i am doing wrong. I have added my project output to all custom actions:

-Install
-Uninstall
-Commit
-Rollback

How is one supposed to perform clean installs/uninstalls ?

Installer Code

    [RunInstaller(true)]
    public partial class ProjectInstaller : Installer {
        public ProjectInstaller() {
            InitializeComponent();
            this.AfterInstall += ProjectInstaller_AfterInstall;
            this.BeforeUninstall += ProjectInstaller_BeforeUninstall;
        }

        private void ProjectInstaller_AfterInstall(object sender, InstallEventArgs e) {
            StartService();
        }

        private void ProjectInstaller_BeforeUninstall(object sender, InstallEventArgs e) {
            StopService();
        }

        private void StartService() {
            Debugger.Launch();
            bool isAdmin = IsAdmin();
            if (isAdmin) {
                using (ServiceController controller = new ServiceController(serviceInstaller1.ServiceName)) {
                    controller.Start();
                }
            } else {
                ProcessStartInfo info = new ProcessStartInfo {
                    Verb = "runas",
                    FileName = "net",
                    Arguments = $"start {serviceInstaller1.ServiceName}"
                };
                Process.Start(info);
            }
        }

        private void StopService() {
            bool isAdmin = IsAdmin();
            if (isAdmin) {
                using (ServiceController controller = new ServiceController(serviceInstaller1.ServiceName)) {
                    controller.Stop();
                }
            } else {
                ProcessStartInfo info = new ProcessStartInfo {
                    Verb = "runas",
                    FileName = "net",
                    Arguments = $"stop {serviceInstaller1.ServiceName}"
                };
                Process.Start(info);
            }
        }

        private static bool IsAdmin() {
           var identity = WindowsIdentity.GetCurrent();
           var princ = new WindowsPrincipal(identity);
           return princ.IsInRole(WindowsBuiltInRole.Administrator);
        }
}
Bercovici Adrian
  • 8,794
  • 17
  • 73
  • 152
  • Sounds like a racing condition to me. Give the service some time after stop and uninstall. – Fildor Jul 12 '19 at 08:04
  • It has this behaviour even on the `admin` branch where you do not spawn a new process. – Bercovici Adrian Jul 12 '19 at 08:06
  • Do you have Service dependencies? (Services that depend on this Service or vice versa) – Fildor Jul 12 '19 at 08:11
  • No i do not have.I have 2 projects.A service and a `winform` process that is opened by the the service at regular intervals, but no other dependencies.I have added both of them to `commit`,`install`,`uninstall` and `rollback` custom actions though.After i get this problem i have to delete the `Setup` project and create a new one since it won't work any other way. – Bercovici Adrian Jul 12 '19 at 08:18
  • If that error happens and you leave it alone for ... let's say an hour and then try again, will it consistently happen again after having waited? – Fildor Jul 12 '19 at 08:21
  • Yes once it happens it remains so.Also when i try to uninstall the service i get error `1001` – Bercovici Adrian Jul 12 '19 at 08:22
  • Then I retract my suspicion of a racing condition. Are traces of your app left in the registry? – Fildor Jul 12 '19 at 08:23
  • No there are not . I have checked in the regedit. – Bercovici Adrian Jul 12 '19 at 08:32
  • I upvoted your question; I do not have another idea right now, I am afraid. – Fildor Jul 12 '19 at 08:36
  • 1
    Found this: https://answers.microsoft.com/en-us/windows/forum/windows_xp-performance/installer-error-1001/a9f833b1-9b56-e011-8dfc-68b599b31bf5 -> can you check if Installer Service is (still) running? You should also check Windows Event Log. Maybe there is some new information to be found. – Fildor Jul 12 '19 at 08:40
  • 1
    This may also help: https://www.wintips.org/fix-error-1001-while-uninstalling-or-installing-programs/ – Fildor Jul 12 '19 at 08:40
  • Is this service well behaved with start and stop when you try that manually? Are there timeout issues that are known? How long does start and stop take? What tool are you using? Visual Studio Installer Projects are known to lack service deployment features. – Stein Åsmul Jul 12 '19 at 12:45

1 Answers1

1

TEMP: Adding this answer, not sure it is relevant until we hear more comments from OP.


Custom Actions: MSI has built-in mechanisms to start / stop and install / uninstall services that are quite reliable. These involve populating a number of standard MSI tables. Using custom actions could trigger problems like you describe, that are hard to debug and solve.


Deployment Tools: What tool are you using?


WiX Toolset: WiX is a free, Open Source alternative, and there are several other major deployment tools you can check. Advanced Installer has some free features, but I don't think that includes service installation. Worth a test though - nice features. Installshield has no free version that I know of, but is full-featured. PACE suite is the new kid on the block. I would test them all and pick one - just 2 cents.

WiX: Service Installation Samples:

Stein Åsmul
  • 39,960
  • 25
  • 91
  • 164