2011-01-31 192 views
1

即時通訊嘗試通過C#編程式安裝服務,但遇到了一個我無法解決的問題。以編程方式安裝Windows服務

在閱讀大量文檔後,我在那個我認爲微軟有bug的地方(但我們都知道情況並非如此)。我的申請是Main

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 }); 
       Console.Read(); 
       break; 
      case "/uninstall": 
       ManagedInstallerClass.InstallHelper(new string[] { "/u", Assembly.GetExecutingAssembly().Location }); 
       break; 
     } 
    } 
    else 
    { 
     ServiceBase.Run(new ProxyMonitor()); 
    } 
} 

像這樣ProxyMonitor /install下的管理權限內CMD當執行步入下降到行:

ManagedInstallerClass.InstallHelper(new string[] { Assembly.GetExecutingAssembly().Location }); 

不如預期,然後跳進我的安裝類,像這樣:

namespace Serco.Services.ProxyMonitor 
{ 
    [RunInstaller(true)] 
    public class ManagedInstallation : ServiceInstaller 
    { 
     public ManagedInstallation() 
     { 
      var ProcessInstaller = new ServiceProcessInstaller(); 
      var ServiceInstaller = new ServiceInstaller(); 

      //set the information and privileges 
      ProcessInstaller.Account  = ServiceConfiguration.AccountType; 
      ServiceInstaller.DisplayName = ServiceConfiguration.DisplayName; 
      ServiceInstaller.StartType  = ServiceConfiguration.StartType; 
      ServiceInstaller.Description = ServiceConfiguration.Description; 
      ServiceInstaller.ServiceName = ServiceConfiguration.ServiceName; 

      Installers.Add(ProcessInstaller); 
      Installers.Add(ServiceInstaller); 
     } 
    } 
} 

檢查調試文件後,我得到以下內容:

Installing assembly 'C:\Users\Robert\documents\visual studio 2010\Projects\ProxyMonitor\ProxyMonitor\bin\Debug\ProxyMonitor.exe'. 
Affected parameters are: 
    logtoconsole = 
    logfile = C:\Users\Robert\documents\visual studio 2010\Projects\ProxyMonitor\ProxyMonitor\bin\Debug\ProxyMonitor.InstallLog 
    assemblypath = C:\Users\Robert\documents\visual studio 2010\Projects\ProxyMonitor\ProxyMonitor\bin\Debug\ProxyMonitor.exe 
Installing service ... 
Creating EventLog source in log Application... 
Rolling back assembly 'C:\Users\Robert\documents\visual studio 2010\Projects\ProxyMonitor\ProxyMonitor\bin\Debug\ProxyMonitor.exe'. 
Affected parameters are: 
    logtoconsole = 
    logfile = C:\Users\Robert\documents\visual studio 2010\Projects\ProxyMonitor\ProxyMonitor\bin\Debug\ProxyMonitor.InstallLog 
    assemblypath = C:\Users\Robert\documents\visual studio 2010\Projects\ProxyMonitor\ProxyMonitor\bin\Debug\ProxyMonitor.exe 
Restoring event log to previous state for source . 

我也得到了以下調用內拋出的異常:

ManagedInstallerClass.InstallHelper(new string[] { Assembly.GetExecutingAssembly().Location }); 

指出:

安裝失敗,並回滾已執行。必須指定源的值。

任何想法?


更新:

配置類

namespace Serco.Services.ProxyMonitor 
{ 
    class ServiceConfiguration 
    { 
     public static string DisplayName 
     { 
      get { return "Serco Proxy Monitor"; } 
     } 

     public static string ServiceName 
     { 
      get { return "Serco Proxy Monitor"; } 
     } 

     public static string Description 
     { 
      get 
      { 
       return "Serco ProxyMonitor is a helper developed to manage the state of the proxy for the employess whilst of the internal network."; 
      } 
     } 

     public static ServiceStartMode StartType 
     { 
      get{return ServiceStartMode.Automatic;} 
     } 

     public static ServiceAccount AccountType 
     { 
      get{return ServiceAccount.LocalSystem;} 
     } 

     /*.. /Snip/ ..*/ 
    } 
} 
+0

您是否以管理員權限運行?此外,如果您創建Windows Service項目和安裝程序以查看生成的確切代碼,它可能會有所幫助,這可能是您嘗試手動編寫的內容,儘管可能以不同方式排列。 – 2011-01-31 21:39:23

+0

Im以管理員身份登錄,並聲明我在管理員模式下運行,並開始在VS 2010中作爲空白項目。 – RobertPitt 2011-01-31 21:41:31

回答

2

它看起來像數源爲null;你確定ServiceConfiguration.ServiceName被定義並有一個值嗎?

+0

是的,我確定,我已將配置類添加到OP – RobertPitt 2011-01-31 21:43:59

4

我想通了,並認爲我會張貼包裝其他人可能會有同樣的問題。

那是幾件事情的組合,但生病只是迅速告訴你他們:

public static string ServiceName 
{ 
    get { return "Serco Proxy Monitor"; } 
} 
  • 必須成爲return "SercoProxyMonitor";由於空間
  • 刪除了UnhandledException然後深入堆棧顯示更多跟蹤
  • 需要具有完全管理員權限。

我認爲主要的問題是,ServiceInstaller是使用ServiceName創建和EventLogSource,而作爲EventLogSource中有空格,被扔一個合適的。