2010-07-03 145 views
4

如何讓我的Windows服務,以通過以下方式工作...雙擊啓動Windows服務

1)自動啓動安裝

2)自動即使啓動後,我們簡單地雙擊可執行文件

換句話說,我不想使用「NET START」,「SC」命令並且不想通過服務控制檯啓動它。我只想讓我的服務自動安裝並自動啓動自身...當可執行文件被雙擊時自動啓動。

謝謝。

回答

2

看看在Topshelf項目(http://topshelf-project.com),並消除所有的複雜性用.NET編寫Windows服務。它處理所有的自注冊並消除應用程序對服務代碼的所有依賴。

它也是開源的,託管在GitHub上,可以很容易地適應任何應用程序。

(全面披露,我是該項目的作者之一)

+0

試過你的項目......但是SERVICE參數不起作用。它就像一個普通的windows服務。它說「 --------------------------- Windows服務啓動失敗 ----------------- ---------- 無法從命令行或調試程序啓動服務必須首先安裝Windows服務(使用installutil.exe),然後以ServerExplorer,Windows服務管理工具或NET START啓動命令 --------------------------- OK ----------------- ---------- 「。 – Josh 2010-07-06 06:50:37

+0

你需要創建自己的EXE,你可以按照這裏的播放:http://topshelf-project.com/documentation/getting-started/ – 2010-07-07 02:53:32

+0

謝謝...和下載鏈接不工作在您的網站上。 – Josh 2010-07-15 12:19:10

4

看看ServiceController class。

可以在commited事件中使用這樣的:

[RunInstaller(true)] 
public class ServiceInstaller : Installer 
{ 
    string serviceName = "MyServiceName"; 

    public ServiceInstaller() 
    { 
     var processInstaller = new ServiceProcessInstaller(); 
     var serviceInstaller = new ServiceInstaller(); 

     processInstaller.Account = ...; 
     processInstaller.Username = ...; 
     processInstaller.Password = ...; 

     serviceInstaller.DisplayName = serviceName; 
     serviceInstaller.StartType = ServiceStartMode.Automatic; 

     serviceInstaller.ServiceName = serviceName; 

     this.Installers.Add(processInstaller); 
     this.Installers.Add(serviceInstaller); 

     this.Committed += new InstallEventHandler(ServiceInstaller_Committed); 
    } 

    void ServiceInstaller_Committed(object sender, InstallEventArgs e) 
    { 
     // Auto Start the Service Once Installation is Finished. 
     var controller = new ServiceController(serviceName); 
     controller.Start(); 
    } 
} 
+0

我還是要使用「NET START」或「SC」 .... 我該如何開始通過雙擊該服務它? – Josh 2010-07-06 07:36:09

+0

@Josh您可以將可執行文件的時鐘加倍並運行該服務。 你可以做的只是創建另一個可執行文件,它將使用ServiceController啓動未啓動的服務。你可以檢查MS SQL Server如何做到這一點。如果您不想使用MMC,則會有單獨的程序停止並啓動服務。 – Incognito 2010-07-06 09:14:12

0

我交here展示瞭如何讓你的Windows服務使用一個-install選項命令行安裝本身。您可以將此邏輯擴展爲具有-start選項,然後在包含該選項的桌面上創建快捷方式。

+0

-start選項是我真正需要的。 – Josh 2010-07-06 08:16:17

1

您可以添加調用安裝程序(使用ManagedInstallerClass.InstallHelper()),和代碼來啓動服務的命令行參數......

public class DataImportService : ServiceBase 
{ 
    // ----------- Other code ----------- 

    static void Main(string[] args) 
    { 
     if (args.Length == 0) 
     { 
      InstallService(false, argValue); break; 
      StartService(); 
     } 
     else 
     { 
      string arg0 = args[0], 
      switchVal = arg0.ToUpper(), 
      argValue = arg0.Contains(":") ? 
      arg0.Substring(arg0.IndexOf(":")) : null; 

      switch (switchVal.Substring(0, 1)) 
      { 
       //Install Service and run 
       case ("I"): case ("-I"): case ("/I"): 
        InstallService(true, argValue); break; 

       // Start Service 
       case ("S"): case ("-S"): case ("/S"): 
        StartService(); 
       default: break; 

       // Install & Start Service 
       case ("IS"): case ("-IS"): case ("/IS"): 
        InstallService(false, argValue); break; 
        StartService(); 

       // Uninstall Service 
       case ("U"): case ("-U"): case ("/U"): 
        InstallService(false, argValue); break; 

       default: break;     
      } 
     } 

    private static void InstallService(bool install, string argFileSpec) 
    { 
     string fileSpec = Assembly.GetExecutingAssembly().Location; 
     if (!String.IsNullOrEmpty(argFileSpec)) fileSpec = argFileSpec; 
     // ------------------------------------------------------------ 
     string[] installerParams = 
      install? new string[] { fileSpec } : 
        new string[] { "/u", fileSpec }; 
     ManagedInstallerClass.InstallHelper(installerParams); 
    } 

    private void StartService() 
    { 
     var ctlr = new ServiceController(); 
     ctlr.ServiceName = "MyService"; // hard code the service name 
     // Start the service 
     ctlr.Start();   
    } 
} 
+0

開始服務怎麼樣?我需要通過雙擊它來啓動服務...... – Josh 2010-07-06 07:36:38

+0

@Josh,使用'ServiceController()'類來啓動和停止服務。你將需要決定如何編寫這個Main例程來做到這一點......這取決於你... – 2010-07-06 13:28:26