2011-09-28 124 views
-1

我使用installutil命令将我的c#应用程序安装为windows服务。 它得到成功安装。在启动服务时,我收到以下错误。启动服务

“错误1053:服务没有及时到开始或控制请求”

任何一个可以请帮我为什么会发生。

下面是源代码

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(); 

     } 

谢谢。

redgards

桑吉塔

+1

我认为错误代码解释得很好......您应该调试您的服务以获取更多信息,为什么它不会启动 – sra

+1

Google说了什么? –

+1

这里没有水晶球。通过编辑您的问题来显示服务启动方法。 –

回答

3

您的问题已回答here

什么假设是,有一个驻留在

void OnStart(string[] args)  

在你的代码你的代码中的问题。在这种情况下,attach a service debugger解决您的问题。

祝你好运。

+1

+1虽然在服务启动后,但在它遇到问题之前附加调试器可能是相当棘手的东西。答案可以是在'OnStart'的第一行中包含一个'Thread.Sleep',让自己连接20秒。 –

+0

你是对的。 –

+1

如果您没有时间附加调试器,则在OnStart中没有太多工作来解决这个问题。然而,在调试'OnStart'' Thread.Sleep'的一般情况下,这是一个很好的提示。 –

2

您的服务确实给很多在OnStart
启动服务选择Tools-> attach to在Visual Studio中进行处理,选择你的服务。打破这个过程,并试图找出这个过程在OnStart很长一段时间里做了什么。