2011-09-28 141 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很長一段時間裏做了什麼。