2

我用this tutorial使用Visual Studio 2010和其股票的控制檯應用程序項目,以創建C#Windows服務,但之後,我改變了一切,並試圖安裝它作爲這樣的:如何使用Visual Studio 2010將Windows控制檯應用程序C#項目更改爲Windows Service?

「C:\ WINDOWS \ Microsoft.NET \ Framework \ v4.0.30319 \ installutil「/ i myservice.exe

我沒有通過控制面板在服務列表中看到我的服務。然後我檢查了installutil的輸出並發現此消息:

由於沒有安裝程序,因此刪除InstallState文件。

我不知道爲什麼它說,這是因爲我有一個安裝程序類,這樣定義:

namespace MySrvr 
{ 
    class MyServiceInstaller : System.Configuration.Install.Installer 
    { 
     public MyServiceInstaller() 
     { 
      ServiceProcessInstaller process = new ServiceProcessInstaller(); 

      process.Account = ServiceAccount.LocalSystem; 

      ServiceInstaller serviceAdmin = new ServiceInstaller(); 

      serviceAdmin.StartType = ServiceStartMode.Automatic; 

      serviceAdmin.ServiceName = "MyServiceName"; 
      serviceAdmin.DisplayName = "My Service Display Name"; 
      serviceAdmin.Description = "My Service Description"; 

      Installers.Add(process); 
      Installers.Add(serviceAdmin); 
     } 

    } 
} 

那我錯在這裏做什麼?

+0

您是否在管理命令提示符下運行'installutil'命令? – 2013-03-28 01:06:34

+0

@chuex:是的,我是。 – ahmd0 2013-03-28 01:08:16

+0

我以前從來沒有見過它用於/ i參數。你試過把它作爲'installutil myservice.exe'來運行嗎? installutil是否與myservice.exe位於同一目錄中?如果不是,你需要給路徑。 – Tim 2013-03-28 01:11:17

回答

5

好的,我明白了。兩個錯誤:

  1. 的安裝程序類必須聲明爲public

  2. 它必須在它前面[RunInstaller(true)]屬性。

這樣:

namespace MySrvr 
{ 
    [RunInstaller(true)] 
    public class MyServiceInstaller : System.Configuration.Install.Installer 
    { 
    } 
} 

installutil版本無關,用它做。

+0

+1以解決它**和*共享未來用戶的解決方案:) – Tim 2013-03-28 04:50:26

相關問題