2017-03-07 186 views
-1

試圖deubg一個窗口服務(除去需要安裝,代碼,安裝,代碼等),還以爲我找到了解決辦法調試Windows服務 - 防止無法啓動服務

class Program 
{ 
    static void Main(string[]args) 
    { 
     ClientService service = new ClientService(); 

     if (args.Length > 0) 
     { 
      Task.Factory.StartNew(service.Main, TaskCreationOptions.LongRunning); 

      Console.WriteLine("running..."); 
      Console.ReadLine(); 

     } 
     else 
     { 
#if (!DEBUG) 
      ServiceBase[]ServicesToRun; 
      ServicesToRun = new ServiceBase[] 
      { 
       new MyService() 
      }; 
      ServiceBase.Run(ServicesToRun); 
# else 
      ServiceBase.Run(service); 
     } 
# endif 
    } 
} 

不過我「M仍然得到錯誤:

Cannot start service from the command line or debugger. A windows Service must first be installed(using installutil.exe) and then started with the ServerExplorer, Windows Services Administrative tool or the NET START command.

enter image description here

什麼,我需要改變,以避免出現提什麼想法?

+2

在項目的調試屬性選項卡中傳遞命令行參數。任何參數都可以。不要只是複製粘貼代碼而不理解它。 – CodeCaster

+0

當您搜索錯誤消息文本時,谷歌搜索結果是什麼? –

+0

根據錯誤消息,Windows服務必須使用'installutil.exe'安裝,而不是從命令行或調試器運行。你嘗試過嗎? – David

回答

2

您已經從Running Windows Service Application without installing it複製的代碼,但它被設計打破,你改變它不正確。

其背後的思想,是你必須包含執行實際的服務邏輯的公開方法的ServiceBase -inheriting類:

public class MyService : ServiceBase 
{ 
    public void DoTheServiceLogic() 
    { 
     // Does its thing 
    } 

    public override void OnStart(...) 
    { 
     DoTheServiceLogic(); 
    } 
} 
在你的應用

然後,你可以這樣開始的:

class Program 
{ 
    static void Main(string[] args) 
    { 
     ServiceBase[] ServicesToRun = new ServiceBase[] 
     { 
      new MyService() 
     }; 

     ServiceBase.Run(ServicesToRun); 
    } 
} 

這提供了可安裝爲Windows服務的可執行應用程序。

但是你不想安裝你的服務,然後附加Visual Studio來調試它;這是一個非常低效的工作流程。

因此,您發現的代碼嘗試執行的操作,就像使用某個命令行標誌(如/debug)啓動應用程序一樣,然後調用服務邏輯上的公共方法 - 將其作爲Windows服務運行

這可以這樣實現:

class Program 
{ 
    static void Main(string[] args) 
    { 

     if (args.Length == 1 && args[0] == "/debug") 
     { 
      // Run as a Console Application 
      new MyService().DoTheServiceLogic(); 
     } 
     else 
     { 
      // Run as a Windows Service 
      ServiceBase[] ServicesToRun = new ServiceBase[] 
      { 
       new MyService() 
      }; 

      ServiceBase.Run(ServicesToRun); 
     } 
    } 
} 

現在,您可以指示Visual Studio中調試應用程序時傳遞/debug標誌,如MSDN: How to: Set Start Options for Application Debugging解釋。

這稍微好一些,但仍然是一個壞方法。您應該完全提取服務邏輯,並編寫單元測試以便能夠測試您的邏輯,而無需在應用程序中運行它,更不用說Windows服務了。

0

這裏是你可以做什麼:

具有相同的名稱作爲旅遊服務創建一個部分類:

public partial class AlertEngineService 
    { 
     public void Run(string[] args) 
     { 

      OnStart(args); 
      Console.WriteLine("Press any key to stop program alert engine service"); 
      Console.Read(); 
      OnStop(); 
     } 
    } 
} 

和調試調用此命令:

var servicesToRun = new ClientService(); 
servicesToRun.Run(args); 

可以還要將服務配置更改爲作爲控制檯運行(在屬性部分中)。

希望它可以幫助

0

開始改變你的代碼位:

static void Main(string[] args) 
{ 
#if DEBUG // If you are currently in debug mode 
    ClientService service = new ClientService(); // create your service's instance 
    service.Start(args); // start this service 
    Console.ReadLine(); // wait for user input (enter key) 
#else // else you're in release mode 
    ServiceBase[] ServicesToRun; // start service normally 
    ServicesToRun = new ServiceBase[] 
    { 
     new MyService() 
    }; 
    ServiceBase.Run(ServicesToRun); 
#endif 
} 

在服務添加這個方法:

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

現在你需要改變的是在性能改變應用程序類型從Windows ApplicationConsole Application

0

在Program.cs的

class Program 
{ 
    static void Main(string[] args) 
    { 
     #if DEBUG 
     new ClientService().Start(); 
     Thread.Sleep(Timeout.Infinite); //Ensure your service keeps running    
     #else 
     ServiceBase[] ServicesToRun; 
     ServicesToRun = new ServiceBase[] 
     { 
      new MyService() 
     }; 
     ServiceBase.Run(ServicesToRun); 
     #endif 
    } 
} 

在ClientService.cs

public void Start() 
{ 
    OnStart(null); 
}