Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't I install my windows service - Specified service already exists

I wish I could put a grenade to my computer at this point. I'm so frustrated because I don't understand why my application won't install using installUtil.

I've just looked through this link now: Windows Service Install Ends in Rollback and unfortunately the kind suggestions on there don't help in my situation, the following error was generated after taking into consideration all of the answers posted by the good people of SO on that link and others.

I have looked for best practices on the web for task parallel processing patterns but there's nothing helpful so far. The latest error I get when attempting to install is as follows:

.exe assembly's progress. The file is located at E:\xxx\MyService\Service_V2.InstallLog. Installing assembly 'E:\xxx\MyService\Service_V2.exe'. Affected parameters are:
logtoconsole = logfile = E:\xxx\MyService\Service_V2.InstallLog
assemblypath = E:\xxx\MyService\Service_V2.exe Installing service Service V2... Service Service V2 has been successfully installed. Creating EventLog source Service V2 in log Application... Installing service Service V2... Creating EventLog source Service V2 in log Application...

An exception occurred during the Install phase. System.ComponentModel.Win32Exception: The specified service already exists

The Rollback phase of the installation is beginning. See the contents of the log file for the E:\xxx\MyService\Service_V2 .exe assembly's progress. The file is located at E:\xxx\MyService\Service_V2.InstallLog. Rolling back assembly 'E:\xxx\MyService\Service_V2.exe'. Affected parameters are:
logtoconsole = logfile = E:\xxx\MyService\Service_V2.InstallLog
assemblypath = E:\xxx\MyService\Service_V2.exe Restoring event log to previous state for source Service V2. Restoring event log to previous state for source Service V2. Service Service V2 is being removed from the system... Service Service V2 was successfully removed from the system.

The Rollback phase completed successfully.

The transacted install has completed. The installation failed, and the rollback has been performed.

There was nothing written to the event log either.

Here is my OnStart() method:

protected override void OnStart(string[] args)
        {
            var tokenSource = new CancellationTokenSource();
            var token = tokenSource.Token;

            ErrorLogFileName = "Service_V2Errors" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt";
            Service_V2LogFile = "Service_V2Log" + DateTime.Now.ToString("yyyy-MM-dd") + ".log";

            ErrorLogPath = ConfigurationManager.AppSettings["Errorpath"].ToString();
            CheckBatchRecord = Convert.ToInt32(ConfigurationManager.AppSettings["CheckBatchTime"].ToString());

            if (!Directory.Exists(ErrorLogPath))
            {
                Directory.CreateDirectory(ErrorLogPath);
            }

            LogMessage("Starting Service " + DateTime.Now.ToString());

            _ErrorLog = new StreamWriter(ErrorLogPath + "//" + ErrorLogFileName, true);
            _ErrorLog.WriteLine("Error, Location, AdditionalInformation", true);
            _ErrorLog.Close();

            var t = Task.Run(() => Service_V2Start(), token);

            try
            {
                t.Wait();
            }
            catch (AggregateException e)
            {
                LogMessage("Exception messages:");
                foreach (var ie in e.InnerExceptions)
                    LogMessage(ie.GetType().Name + " : " + ie.Message);

                LogMessage("\nTask status: " + t.Status);       
            }
            finally
            {
                tokenSource.Dispose();
            }          
        }

I have also set the compile mode to release for the final install files compiled.

I have done an "sc delete Servie V2" and I also checked the services console and there is no such service listed there.

I have also tried the InstallUtil.exe -u command to uninstall, but I still get this nitwit error. What should I do now?

like image 285
Sizons Avatar asked Mar 12 '23 19:03

Sizons


1 Answers

  1. Make sure your Program.cs file looks something like this:

    static class Program
    {
        static void Main()
        {
            var service = new YourServiceName();
            ServiceBase.Run(service);
        }
    }
    
  2. Inside the InitializeComponent() method make sure that the ServiceName property value is the same as the ServiceName in the ProjectInstaller.cs

     this.ServiceName = "MyServiceName";//in the YourServiceName partial class
    
     this.myServiceInstaller.ServiceName = "MyServiceName";//in the installer
    
  3. Make sure you have only one installer.

  4. In the batch files that you created to install and uninstall your service make sure that you are pointing to the correct InstallUtil.exe.

    For 64 bit architectures you can use - C:\Windows\Microsoft.NET\Framework64\v4.0.30319\installutil.exe

    For 32 bit architectures you can use - C:\Windows\Microsoft.NET\Framework\v4.0.30319\InstallUtil.exe

  5. Sample InstallService.bat file:

    "C:\Windows\Microsoft.NET\Framework64\v4.0.30319\installutil.exe" "PathToTheExecutables\MyServiceName.exe" pause

  6. Sample UninstallService.bat file:

    "C:\Windows\Microsoft.NET\Framework64\v4.0.30319\installutil.exe" /u "PathToTheExecutables\MyServiceName.exe" pause

like image 129
Denys Wessels Avatar answered Mar 23 '23 01:03

Denys Wessels