2017-03-17 220 views
0

當我調試我的代碼時,它彈出一個對話框,該服務啓動失敗 - 無法從命令行和調試器啓動服務。首先必須安裝Windows服務(使用installutil.exe),然後使用服務器資源管理器,Windows服務管理工具或網絡啓動命令啓動。windows服務啓動失敗

但我已經安裝了我的服務。以下是帶有管理員權限的cmd的腳本。

C:\Users\Sapuser\Documents\Visual Studio 2013\Projects\WindowsService1\WindowsSe 
rvice1\bin\Debug>installutil WindowsService1.exe 
Microsoft (R) .NET Framework Installation utility Version 4.0.30319.33440 
Copyright (C) Microsoft Corporation. All rights reserved. 


Running a transacted installation. 

Beginning the Install phase of the installation. 
See the contents of the log file for the C:\Users\Sapuser\Documents\Visual Studi 
o 2013\Projects\WindowsService1\WindowsService1\bin\Debug\WindowsService1.exe as 
sembly's progress. 
The file is located at C:\Users\Sapuser\Documents\Visual Studio 2013\Projects\Wi 
ndowsService1\WindowsService1\bin\Debug\WindowsService1.InstallLog. 
Installing assembly 'C:\Users\Sapuser\Documents\Visual Studio 2013\Projects\Wind 
owsService1\WindowsService1\bin\Debug\WindowsService1.exe'. 
Affected parameters are: 
    logtoconsole = 
    logfile = C:\Users\Sapuser\Documents\Visual Studio 2013\Projects\WindowsServi 
ce1\WindowsService1\bin\Debug\WindowsService1.InstallLog 
    assemblypath = C:\Users\Sapuser\Documents\Visual Studio 2013\Projects\Windows 
Service1\WindowsService1\bin\Debug\WindowsService1.exe 
Installing service MyTestWinService... 
Service MyTestWinService has been successfully installed. 
Creating EventLog source MyTestWinService in log Application... 

The Install phase completed successfully, and the Commit phase is beginning. 
See the contents of the log file for the C:\Users\Sapuser\Documents\Visual Studi 
o 2013\Projects\WindowsService1\WindowsService1\bin\Debug\WindowsService1.exe as 
sembly's progress. 
The file is located at C:\Users\Sapuser\Documents\Visual Studio 2013\Projects\Wi 
ndowsService1\WindowsService1\bin\Debug\WindowsService1.InstallLog. 
Committing assembly 'C:\Users\Sapuser\Documents\Visual Studio 2013\Projects\Wind 
owsService1\WindowsService1\bin\Debug\WindowsService1.exe'. 
Affected parameters are: 
    logtoconsole = 
    logfile = C:\Users\Sapuser\Documents\Visual Studio 2013\Projects\WindowsServi 
ce1\WindowsService1\bin\Debug\WindowsService1.InstallLog 
    assemblypath = C:\Users\Sapuser\Documents\Visual Studio 2013\Projects\Windows 
Service1\WindowsService1\bin\Debug\WindowsService1.exe 

The Commit phase completed successfully. 

The transacted install has completed. 

C:\Users\Sapuser\Documents\Visual Studio 2013\Projects\WindowsService1\WindowsSe 
rvice1\bin\Debug>net start MyTestWinService 
The MyTestWinService service is starting. 
The MyTestWinService service was started successfully. 

我米附着我的代碼爲參考:

public partial class Service1 : ServiceBase 
    { 
     private Timer timer1 = null; 

     public Service1() 
     { 
      InitializeComponent(); 
     } 

     protected override void OnStart(string[] args) 
     { 
      timer1 = new Timer(); 
      this.timer1.Interval = 60000; //60 sec 
      this.timer1.Elapsed +=new System.Timers.ElapsedEventHandler(this.timer1_Tick); 
      timer1.Enabled=true; 
      Library.WriteErrorLog("test windows service started"); 

     } 

     protected override void OnStop() 
     { 
      timer1.Enabled = false; 
      Library.WriteErrorLog("Test Service ended"); 
     } 

     public void timer1_Tick(object sender, ElapsedEventArgs e) 
     { 
      //job 
      var result = RunProcess(@"c:\", "webupknvp.Bat", "", false); 
      if (result == 0) 
      { 
       // success 
       Console.WriteLine("Sucess"); 
      } 
      else 
      { 
       // failed ErrorLevel/app ExitCode 
       Console.WriteLine("failed try again"); 

      } 


     } 

     public int RunProcess(string workDir, string appName, string args, bool hide = false) 
      { 

        Process proc = null; 
        proc = new Process();   
        string batrun = string.Format("cmd.exe", "/c" + @"C:\Abhay\batfile"); // or @"C:\Abhay\batfile" in the end ("cmd.exe", "/c" + @"C:\Abhay\batfile") 
        proc.StartInfo.UseShellExecute = false; //addition  
        proc.StartInfo.WorkingDirectory = workDir;//batrun 
        proc.StartInfo.FileName = appName; 
        proc.StartInfo.Arguments = args; 
        proc.StartInfo.CreateNoWindow = hide; 

        proc.Start(); 
        proc.WaitForExit(); 

        return proc.ExitCode; 
       } 
    } 
} 

庫類

public static void WriteErrorLog(Exception ex) 
     { 
      StreamWriter sw = null; 
      try 
      { 
       sw = new StreamWriter(AppDomain.CurrentDomain.BaseDirectory + "\\ Logfile.txt", true); 
       sw.WriteLine(DateTime.Now.ToString() + ":" + ex.Source.ToString().Trim() + ";" + ex.Message.ToString().Trim()); 
       sw.Flush(); 
       sw.Close(); 

      } 
      catch 
      { 

      } 
     } 

     public static void WriteErrorLog(string Message) 
     { 
      StreamWriter sw = null; 
      try 
      { 

       sw = new StreamWriter(AppDomain.CurrentDomain.BaseDirectory + "\\ Logfile.txt", true); 
       sw.WriteLine(DateTime.Now.ToString() + ":" + Message); 
       sw.Flush(); 
       sw.Close(); 
      } 
      catch 
      { 

      } 
     } 
    } 
} 

的Program.cs

static void Main() 
     { 
      ServiceBase[] ServicesToRun; 
      ServicesToRun = new ServiceBase[] 
      { 
       new Service1() 
      }; 
      ServiceBase.Run(ServicesToRun); 

     } 

我應該改變什麼?

+0

通過'調試我的代碼'你的意思是從視覺工作室?您無法從Visual Studios運行服務來進行調試。根據你發佈的內容,你安裝了服務並且正在運行(你應該在服務窗口中看到它)。但是,這是已安裝的版本; VS仍然無法通過IDE運行服務。 – Lithium

+0

@Lithium是我能夠在服務窗口中看到服務,並且它也是從admin cmd啓動net start。另一方面,即使服務從網絡啓動開始,但作業(即運行批處理文件)不會由該服務完成。 –

回答

2

您的主要功能不允許進行調試。您可以創建除了發行版本調試的版本(注意,您可以只調試調試版本,您將無法安裝調試版本的服務!)如下:

private static void Main(string[] _arguments) 
    { 
     // ... some general code here 
     StartDebug(_arguments); 
     StartRelease(); 
    } 

    [Conditional("DEBUG")] 
    private static void StartDebug(string[] _arguments) 
    { 
     MessageBox.Show("Starting in debug mode"); 

     try 
     { 
      Service1 service = new Service1(); 
      service.Start(_arguments); 
      while (true) 
      { 
       Thread.Sleep(100); 
      } 
     } 
     catch (Exception ex) 
     { 
      Logger.LogException(ex); 
     }    
    } 

    [Conditional("RELEASE")] 
    private static void StartRelease() 
    { 
     ServiceBase[] servicesToRun = { new Service1() }; 
     ServiceBase.Run(servicesToRun); 
    } 

請注意,您的Service1應具有internal Start方法。

編輯:

目前,您覆蓋OnStart。更改如下:

protected override void OnStart(string[] args) 
    { 
     Start(args); 
    } 

    internal void Start(string[] args) 
    { 
     timer1 = new Timer(); 
     this.timer1.Interval = 60000; //60 sec 
     this.timer1.Elapsed +=new System.Timers.ElapsedEventHandler(this.timer1_Tick); 
     timer1.Enabled=true; 
     Library.WriteErrorLog("test windows service started"); 
    } 
+0

我的服務沒有內部啓動方法。這是我第一次創建服務,所以我不知道什麼是內部啓動方法。 –

+0

@AbhaySinghania編輯答案,看看那裏的例子 –

+0

謝謝@BernhardHiller –