2010-11-10 54 views
35

我已閱讀此問題Inno Setup for Windows service?。我有同樣的問題,但我不明白從lubos hasko的答案。我該如何做到這一點?你有人可以發佈我完整的演練嗎?在.NET中自行安裝windows服務c#

當我運行下面的代碼時,會安裝一些東西,但在服務列表中,我找不到它。 我有這個,但是這不工作:

using System; 
using System.Collections.Generic; 
using System.Configuration.Install; 
using System.Linq; 
using System.Reflection; 
using System.ServiceProcess; 
using System.Text; 
using System.IO; 

namespace ConsoleApplication1 
{ 

public class Service1 : ServiceBase 
{ 

    public Service1() 
    { 
     File.AppendAllText("sss.txt", "ccccc"); 
    } 

    protected override void OnStart(string[] args) 
    { 
     File.AppendAllText("sss.txt", "asdfasdf"); 
    } 

    protected override void OnStop() 
    { 
     File.AppendAllText("sss.txt", "bbbbb"); 
    } 


    static void Main(string[] args) 
    { 
     if (System.Environment.UserInteractive) 
     { 
      string parameter = string.Concat(args); 
      switch (parameter) 
      { 
       case "--install": 
        ManagedInstallerClass.InstallHelper(new string[] { Assembly.GetExecutingAssembly().Location }); 
        break; 
       case "--uninstall": 
        ManagedInstallerClass.InstallHelper(new string[] { "/u", Assembly.GetExecutingAssembly().Location }); 
        break; 
      } 
     } 
     else 
     { 
      ServiceBase.Run(new Service1()); 
     } 


     Console.ReadKey(); 
    } 
} 
} 

我不understad這兩種:

if (System.Environment.UserInteractive) ... 

回答

72

這是我完整的解決方案,它的工作原理。這與this問題基本相同。

using System; 
using System.Configuration.Install; 
using System.Reflection; 
using System.ServiceProcess; 
using System.IO; 

namespace ConsoleApplication1 
{ 
    class Program : ServiceBase 
    { 
     static void Main(string[] args) 
     { 

      AppDomain.CurrentDomain.UnhandledException += CurrentDomainUnhandledException; 


      if (System.Environment.UserInteractive) 
      { 
       string parameter = string.Concat(args); 
       switch (parameter) 
       { 
        case "--install": 
         ManagedInstallerClass.InstallHelper(new string[] { Assembly.GetExecutingAssembly().Location }); 
         break; 
        case "--uninstall": 
         ManagedInstallerClass.InstallHelper(new string[] { "/u", Assembly.GetExecutingAssembly().Location }); 
         break; 
       } 
      } 
      else 
      { 
       ServiceBase.Run(new Program()); 
      } 



     } 

     private static void CurrentDomainUnhandledException(object sender, UnhandledExceptionEventArgs e) 
     { 
      File.AppendAllText(@"C:\Temp\error.txt", ((Exception)e.ExceptionObject).Message + ((Exception)e.ExceptionObject).InnerException.Message); 
     } 

     public Program() 
     { 
      this.ServiceName = "My Service"; 
      File.AppendAllText(@"C:\Temp\sss.txt", "aaa"); 

     } 

     protected override void OnStart(string[] args) 
     { 
      base.OnStart(args); 

      File.AppendAllText(@"C:\Temp\sss.txt", "bbb"); 
     } 

     protected override void OnStop() 
     { 
      base.OnStop(); 

      File.AppendAllText(@"C:\Temp\sss.txt", "ccc"); 
     } 
    } 
} 

在同一個項目中創建這個類:

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Configuration.Install; 
using System.Linq; 
using System.ServiceProcess; 
using System.Text; 

namespace ConsoleApplication1 
{ 
    [RunInstaller(true)] 
    public class MyWindowsServiceInstaller : Installer 
    { 
     public MyWindowsServiceInstaller() 
     { 
      var processInstaller = new ServiceProcessInstaller(); 
      var serviceInstaller = new ServiceInstaller(); 

      //set the privileges 
      processInstaller.Account = ServiceAccount.LocalSystem; 

      serviceInstaller.DisplayName = "My Service"; 
      serviceInstaller.StartType = ServiceStartMode.Automatic; 

      //must be the same as what was set in Program's constructor 
      serviceInstaller.ServiceName = "My Service"; 
      this.Installers.Add(processInstaller); 
      this.Installers.Add(serviceInstaller); 
     } 
    } 
} 

運行這個程序與參數--install/- 在Windows 7中檢查卸載作爲管理員錯誤日誌中的溫度。檢查相同路徑上的工作日誌。

0

System.Environment.UserInteractive財產告訴你 是否Windows進程或如IIS服務,沒有一個用戶運行接口。

如果此屬性爲false,則不顯示模式對話框或消息框,因爲沒有用於與用戶交互的圖形用戶界面。 Source

檢查this文章爲好。

+0

謝謝,那篇文章真的幫了我。但在文章中,作者使用了installutil。我不想使用installutil。有什麼選擇嗎?答案是在這篇文章http://stackoverflow.com/questions/1449994/inno-setup-for-windows-service/1450051#1450051但我不知道如何使用它。 – Simon 2010-11-10 13:47:29

+0

爲什麼你不想用installutil安裝服務?如果是因爲權限,您將無法在沒有管理員權限的情況下安裝服務。這沒有工作。 – jlafay 2010-11-10 14:09:09

+0

其實,我仍然使用installutil,但是通過ManagedInstallerClass.InstallHelper。這纔是重點。當我部署我的程序時,我也不需要部署installutil.exe。在使用Inno Setup安裝應用程序期間安裝WS,這是在管理員權限下進行的,因此沒有問題... – Simon 2010-11-11 09:45:01

1

首先,在你的Service1的構造函數中設置ServiceName屬性。從MSDN

摘錄:

你需要在構造函數中從ServiceBase繼承一個類來實現最小的是設置服務名稱的組件。在構造函數中沒有其他處理是特別需要的。您應該在OnStart中處理大多數初始化,而不是在構造函數中。

其次,在從命令行運行時,需要將參數傳遞給您的服務。 --install進行安裝,--uninstall進行卸載 - 查看你的switch語句,它正在輸入參數。

+0

是的,傳遞參數並修正了構造函數。仍然沒有運氣。 – Simon 2010-11-10 13:48:19

+0

Doh!請參閱Simon的回答,安裝程序部分丟失。 – Mihailo 2010-11-10 14:58:54

+0

順便說一下,在向項目添加新項目時,您可以選擇Windows Service(右鍵單擊項目,在彈出菜單中選擇Add - > New Item ...並在其中找到Windows Service)。像這樣添加後,您可以很好地設置服務的屬性並添加安裝程序等。 – Mihailo 2010-11-10 15:03:59