1

I have developed a Windows Service. I'm trying to install it.

    static void Main(string[] args)
    {
        args = new[] { "-i" };

        if (args.Length == 0)
        {
            ServiceBase.Run(new ServiceBase[] { new Service() });
        }
        else if (args.Length == 1)
        {
            var windowsServiceInstaller = 
                new WindowsServiceInstaller("AutocompleteTemplateService", typeof (Service));
            try
            {
                switch (args[0])
                {
                    case "-i":

                        Console.WriteLine("Install service...");
                        windowsServiceInstaller.InstallService();
                        Console.WriteLine("Start service");
                        windowsServiceInstaller.StartService();
                        Console.WriteLine("Сервис запущен...");
                        break;
                    case "-u":
                        Console.WriteLine("Stop service...");
                        windowsServiceInstaller.StopService();
                        Console.WriteLine("Delete service...");
                        windowsServiceInstaller.UninstallService();
                        Console.WriteLine("Сервис удалён...");
                        break;
                    default:
                        Console.WriteLine("Не известный параметр");
                        break;
                }
            }
            catch (Exception exception)
            {
                Console.WriteLine(exception.Message);
            }
        }
    }

The problem itself in the installation:

windowsServiceInstaller.InstallService();

The service itself is installed, but when I run service the message: Error 1053: The service did not respond to the start or control request in a timely fashion

To install using the following code:

    public void InstallService()
    {
        if (IsInstalled()) return;

        using (var installer = GetInstaller())
        {
            IDictionary state = new Hashtable();
            try
            {
                installer.Install(null);
                installer.Commit(null);
            }
            catch
            {
                installer.Rollback(state);
            }
        }
    }

Or:

ManagedInstallerClass.InstallHelper(new[] { Assembly.GetExecutingAssembly().Location });

Or: http://www.verious.com/qa/how-to-install-a-windows-service-programmatically-in-c/

The result is the same.

But when I install a installUtill, it everything turns to run.

How can I solve this problem?

sribin
  • 1,736
  • 1
  • 13
  • 21
  • One thing that immediately jumps out as being wrong is that `Rollback` is the only method being passed a `state` object - where do you think the `state` gets populated to be useful to `Rollback`? – Damien_The_Unbeliever Jul 02 '13 at 06:54
  • If the Install and Commit methods pass the state, all the same. – sribin Jul 02 '13 at 06:58
  • But since the `Install` and [`Commit`](http://msdn.microsoft.com/en-us/library/system.configuration.install.installer.commit.aspx) methods are *documented* to throw an `ArgumentException` if the *savedState* parameter is null, how can you possibly expect it to work? Pass the state, and remove your `catch` all handler, and see what *actual* exceptions are happening when the install is attempted. – Damien_The_Unbeliever Jul 02 '13 at 07:01
  • Did as you described, all the same. The exceptions do not appear. – sribin Jul 02 '13 at 07:13
  • Create Powershell script in C# – Saroop Trivedi Jul 02 '13 at 12:01
  • Your approach looks extremely similar to the workable one I've documented here: http://stackoverflow.com/questions/1195478/how-to-make-a-net-windows-service-start-right-after-the-installation/1195621#1195621 – Matt Davis Jul 03 '13 at 15:19

0 Answers0