0

I have a windows service app written in c# and am trying to start it right after installation. The service installs fine and gives no errors or exceptions, but when I open task manager and check my services after installation the status is "stopped". I can right click and start the service and it works fine. I know the the commit occurs after all the installers. I've followed several examples on stack overflow and it still will not start.

Here is a small glimpse of my code, any ideas how to get it to start automatically after installing?

  public example_project()
    {
        var processInstaller = new ServiceProcessInstaller();
        var serviceInstaller = new ServiceInstaller();

        //set the privileges
        processInstaller.Account = ServiceAccount.LocalSystem;
        processInstaller.Username = null;
        processInstaller.Password = null;

        //service properties
        serviceInstaller.DisplayName = "example project";
        serviceInstaller.StartType = ServiceStartMode.Automatic;
        serviceInstaller.ServiceName = "example project";


        this.Installers.Add(processInstaller);
        this.Installers.Add(serviceInstaller);



        //update appconfig
        Console.WriteLine("Please enter the directory path of the of the target folder . . .");
        string target = Console.ReadLine();
        Configuration config = ConfigurationManager.OpenExeConfiguration(Application.ExecutablePath);
        config.AppSettings.Settings.Add("target_directory", target);
        config.Save(ConfigurationSaveMode.Minimal);

        this.AfterInstall += new InstallEventHandler(ServiceInstaller_AfterInstall);
    }


     void ServiceInstaller_AfterInstall(object sender, InstallEventArgs e)
    {
        using (ServiceController controller = new ServiceController("example project"))
        {
            controller.Start();
        }
     }
user3062114
  • 129
  • 1
  • 3
  • 12
  • possible duplicate of [Automatically start a Windows Service on install](http://stackoverflow.com/questions/1036713/automatically-start-a-windows-service-on-install) – Gary Walker Dec 09 '13 at 19:20
  • @GaryWalker I've been on that and tried those solutions, the same problems still exist – user3062114 Dec 09 '13 at 19:42
  • Based on the info you provide and saying you've tried the previously posted solution, the problem must be something else -- Are you spelling the service correctly, etc. Using the Committed() instead of AfterInstall() should not be a problem. Are you installing the event handler correctly (in the right sequence, etc.) There is nothing in your posting that suggests what you could be doing wrong that I could see. I like the "full code" version shown in the referenced answer. I've never needed to do this myself, but I don't thing there is anothing preventing debugging an installer easily. – Gary Walker Dec 09 '13 at 20:01
  • I've added my full code – user3062114 Dec 09 '13 at 20:21
  • Just to follow up with anyone who might run into this question. My code to start automatically after installation was fine. There was an exception occurring later in the program I was not catching and it was shutting down unexpectedly. Thanks for the help @GaryWalker – user3062114 Dec 09 '13 at 23:02

0 Answers0