2010-08-03 147 views
2

我目前正在嘗試使用ManagedInstallerClass.InstallHelper安裝多個服務。使用ManagedInstallerClass.InstallHelper安裝多個服務

當我安裝第一個服務時,代碼執行順利,然後出現錯誤,指出已存在同名服務。 但是,如果我退出程序,然後從第二個服務開始執行相同的過程,一切都會順利進行。

我使用的功能是這一個。

ManagedInstallerClass.InstallHelper(arguments.ToArray()); 

我100%確定參數是正確的。

的例外,我有是這樣的一種:

System.InvalidOperationException: "The installation failed, and the rollback has been performed." 
Inner Exception: "The specified service already exists" 

我的直覺是,ManagedInstallerClass執行第二個電話時,在同一過程中出現錯誤的東西保持在它的腹部,因此。

任何人都知道發生了什麼,爲什麼?

+0

你怎麼知道參數沒問題?這是一個不使用的方法,它沒有描述字符串數組的必需格式。此外,它不安裝服務,它需要程序集名稱,具有安裝程序的程序集。 – 2010-08-03 17:44:52

+0

參數確實沒問題。正如我所說的,第一個電話沒問題 - 第二個電話與我列出的例外分手。如果我重新啓動程序並執行第二個呼叫,呼叫也會正常運行 - 以此類推,第三,第四等。 – sh0uzama 2010-08-03 23:38:06

回答

5

經過大量測試後,我仍然無法解決ManagedInstallerClass的問題。

我得到的是解決問題的方法。

所以,與其呼籲:

ManagedInstallerClass.InstallHelper(arguments.ToArray()); 

現在我打電話

callInstallUtil(arguments.ToArray()); 

函數定義:

public static string InstallUtilPath = System.Runtime.InteropServices.RuntimeEnvironment.GetRuntimeDirectory(); 
private bool callInstallUtil(string[] installUtilArguments) 
{ 
    Process proc = new Process(); 
    proc.StartInfo.FileName = Path.Combine(InstallUtilPath, "installutil.exe"); 
    proc.StartInfo.Arguments = String.Join(" ", installUtilArguments); 
    proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; 
    proc.StartInfo.RedirectStandardOutput = true; 
    proc.StartInfo.UseShellExecute = false; 

    proc.Start(); 
    string outputResult = proc.StandardOutput.ReadToEnd(); 
    proc.WaitForExit(); 

    // ---check result--- 
    if (proc.ExitCode != 0) 
    { 
     Errors.Add(String.Format("InstallUtil error -- code {0}", proc.ExitCode)); 
     return false; 
    } 

    return true; 
} 

這種多個函數調用不同的服務產生任何錯誤,所以我想這適用於我:) - 它不像ManagedInstallerClass調用那樣優雅,bu它完成了工作。