-1

I installed my c# application as windows service, by using the command installutil. It get successfully installed. While starting the service I am getting the following error.

"Error 1053: The service did not respond to the start or control request in a timely fashion"

Why it happens?

Below is the source code

static void Main(string[] args)
        {
            // Get the version of the current application.
            Assembly assem = Assembly.GetExecutingAssembly();
            AssemblyName assemName = assem.GetName();
            Version ver = assemName.Version;
            // Console.WriteLine("{0}, Version {1}", assemName.Name, ver.ToString());

            Console.WriteLine("{0} version {1}", assemName.Name, ver.ToString());

            TouchService touchService = new TouchService();


            if (Environment.UserInteractive)
            {
                bool show_help = false;
                bool install_service = false;
                bool uninstall_service = false;
                string servicename = "";


                OptionSet p = new OptionSet()                  
                  .Add("h|?|help", delegate(string v) { show_help = v != null; })
                  .Add("s|servicename=", "name of installed service", delegate(string v) { servicename = v; })
                  .Add("i|install", "install program as a Windows Service. A valid servicename is needed.", delegate(string v) { install_service = v != null; })
                  .Add("u|uninstall", "uninstall program from Windows Services. A valid servicename is needed.", delegate(string v) { uninstall_service = v != null; });

                List<string> extra;
                try
                {
                    extra = p.Parse(args);
                }
                catch (OptionException e)
                {
                    Console.Write("TouchServer: ");
                    Console.WriteLine(e.Message);
                    Console.WriteLine("Try `TouchServer --help' for more information.");
                    return;
                }

                if (show_help)
                {
                    ShowHelp(p);
                    return;
                }

                else if (install_service)
                {
                    IntegratedServiceInstaller Inst = new IntegratedServiceInstaller();
                    Inst.Install(servicename, null, "Provides XML data over HTTP for Touch clients",                                                              
                                 System.ServiceProcess.ServiceAccount.NetworkService,
                                 System.ServiceProcess.ServiceStartMode.Manual);

                    return;
                }

                else if (uninstall_service)
                {
                    IntegratedServiceInstaller Inst = new IntegratedServiceInstaller();
                    Inst.Uninstall(servicename);
                    return;
                }

                // start and run the server,
                // and receive commands from the console
                else
                {

                    touchService.OnStart(args);
                    while (true)
                    {
                        Console.Write("TouchServer>");
                        string commandLine = Console.ReadLine().ToLower();

                        if (commandLine == "exit" || commandLine == "x")
                        {
                            break;
                        }
                        if (commandLine == "quit" || commandLine == "q")
                        {
                            break;
                        }

                        else if (commandLine == "version" || commandLine == "v")
                        {
                            Console.WriteLine("{0} version {1}", assem.GetName().Name, assem.GetName().Version.ToString());
                        }

                        else if (commandLine == "list" || commandLine == "l")
                        {
                            TouchServer.showURLs = (TouchServer.showURLs == false) ? true : false;
                            Console.WriteLine("List URLs: {0}", (TouchServer.showURLs ? "active" : "inactive"));
                        }

                        else if (commandLine == "status" || commandLine == "s")
                        {
                            Console.WriteLine("{0,-20} {1,8}", "Name", "Sessions");
                            Console.WriteLine("----------------------------");
                            foreach (Site site in TouchServer.siteCollection.All)
                            {
                                Console.WriteLine("{0,-20} {1,8}", site.Name, site.AllSessions.Length);
                            }
                            Console.WriteLine();
                        }
                    }

                    touchService.OnStop();
                }
            }
            else
            {
                ServiceBase[] ServicesToRun;
                ServicesToRun = new ServiceBase[] 
                { 
                    new TouchService() 
                };
                ServiceBase.Run(ServicesToRun);
            }

        }

        static void ShowHelp(OptionSet p)
        {
            Console.WriteLine("Usage: TouchServer [OPTIONS]+ ");
            Console.WriteLine();
            Console.WriteLine("Options:");
            p.WriteOptionDescriptions(Console.Out);
            Console.WriteLine();
            Console.WriteLine("Providing no options results in the server running in console mode (for debugging purposes).");
        }


        public TouchService()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            taskList.Clear();
            taskList.Add(new TouchServerTask("TouchServer"));
            taskList.Add(new HouseKeeperTask());

            //TouchServer.Execute();
            setupSynchronizerTasks();
            taskList.StartAllTasks();

        }
Cœur
  • 37,241
  • 25
  • 195
  • 267
Sangeetha
  • 601
  • 3
  • 12
  • 26
  • 1
    I think the error code explained it well... you should debug your service to get more information why it don't start – sra Sep 28 '11 at 06:35
  • 1
    No crystall balls here. show the service start method by editing your question. – Davide Piras Sep 28 '11 at 06:35
  • take a look at your eventlogs - chances are that you will find some further errors – Random Dev Sep 28 '11 at 06:36
  • Start by debugging your service. How to do it: http://stackoverflow.com/questions/6995565/debugging-window-service/6995714#6995714 – Otiel Sep 28 '11 at 06:39

2 Answers2

4

Your question has been answered here.

What's assumed is that there's a problem within your code that resides in

void OnStart(string[] args)    

in your code. In that case, attach a service debugger to troubleshoot your problem.

Good luck.

Mr_Spock
  • 3,815
  • 6
  • 25
  • 33
  • 2
    +1 although attaching the debugger after the service starts but before it hits the problem can be pretty tricky stuff. An answer can be to include a `Thread.Sleep` in the first line of the `OnStart` to give yourself 20 seconds to attach. – Kirk Broadhurst Sep 28 '11 at 06:53
  • 2
    If you don't have the time to attach the debugger you don't have that much work in `OnStart` to create this problem. However in the general case of debugging `OnStart` `Thread.Sleep` is a good hint. – Albin Sunnanbo Sep 28 '11 at 07:35
2

Your service does to much in OnStart.
Start the service choose Tools->attach to process in Visual Studio, choose your service. Break the process and try to figure out what the process is doing so long time in OnStart.

Albin Sunnanbo
  • 46,430
  • 8
  • 69
  • 108