2015-07-05 40 views
0

我正在創建一個windows服務,它假定在特定表中查找數據,然後根據狀態處理記錄。在安裝過程中將參數傳遞給Windows服務

我想在使用installutill作爲參數安裝服務並將它們保存在註冊表內時傳遞數據庫憑據。我已經嘗試使用代碼波紋管,但是我一直在事件「OnBeforeInstall」上發生錯誤。

我相信要麼我錯誤地傳遞參數,要麼我在錯誤的事件中編寫代碼。需要你的幫助來確定我做錯了什麼。

protected override void OnBeforeInstall(IDictionary savedState) 
{ 
    base.OnBeforeInstall(savedState); 
    _eventLog.WriteEntry("OnBeforeInstall Started"); 
    try 
    { 
     RegistryKey key = Registry.CurrentUser.CreateSubKey(@"SOFTWARE\RyteRMS"); 
     if (key != null) 
     { 
      _eventLog.WriteEntry("Write data to registry key"); 
      key.SetValue("DBTYPE", this.Context.Parameters["dbtype"].ToString()); // This throws error, I am assuming as the above event entry is visible. 
      key.SetValue("DATASOURCE", this.Context.Parameters["datasource"].ToString()); 
      key.SetValue("USERID", this.Context.Parameters["userid"].ToString()); 
      key.SetValue("PASSWORD", this.Context.Parameters["password"].ToString()); 
      key.SetValue("DBNAME", this.Context.Parameters["dbname"].ToString()); 
      key.Close(); 
     } 
    } 
    catch (Exception ex) 
    { 
     _eventLog.WriteEntry(ex.Message); 
    } 
    _eventLog.WriteEntry("OnBeforeInstall Finished"); 
} 

我在命令提示寫這: installutil RMSBGService.exe/DBTYPE = SQLSERVER /數據源= hitin-LT/DBNAME = RMS /用戶ID =管理員/密碼= PASSW0RD

錯誤:「未將對象引用設置爲對象的實例。」

P.S.我不知道如何調試Win服務,所以我使用事件日誌來記錄每一件事情。

回答

0

管理我自己做,這是我們應該做的。

protected override void OnBeforeInstall(IDictionary savedState) 
{ 
    _eventLog.WriteEntry("OnBeforeInstall Started"); 
    try 
    { 
     RegistryKey key = Microsoft.Win32.Registry.LocalMachine.CreateSubKey(@"SOFTWARE\RMS"); 
     if (key != null) 
     { 
      _eventLog.WriteEntry("Write data to registry key"); 

      Process _prc = new Process(); 
      _prc.StartInfo.FileName = "cmd.exe"; 
      _prc.StartInfo.UseShellExecute = false; 
      _prc.StartInfo.RedirectStandardOutput = true; 
      _prc.StartInfo.RedirectStandardInput = true; 
      _prc.Start(); 

      ConsoleColor _color = Console.ForegroundColor; 
      Console.ForegroundColor = ConsoleColor.DarkGreen; 
      Console.WriteLine("\n\n"); 
      Console.WriteLine("PLEASE ENTER FOLLOWING DETAILS TO COMPLETE SETUP"); 
      Console.WriteLine("NOTE: if you enter wrong information, you will need to reinstall the application."); 
      Console.WriteLine("\n\n"); 
      Console.WriteLine("Enter DBTYPE (SQLSERVER or ORACLE):"); 
      key.SetValue("DBTYPE", StringCipher.Encrypt(Console.ReadLine(), "@xx")); 
      Console.WriteLine("Enter DATASOURCE (SERVER NAME):"); 
      key.SetValue("DATASOURCE", StringCipher.Encrypt(Console.ReadLine(), "@xx")); 
      Console.WriteLine("Enter DATABASE USER ID:"); 
      key.SetValue("USERID", StringCipher.Encrypt(Console.ReadLine(), "@xx")); 
      Console.WriteLine("Enter PASSWORD:"); 
      key.SetValue("PASSWORD", StringCipher.Encrypt(Console.ReadLine(), "@xx")); 
      Console.WriteLine("Enter DATABASE NAME:"); 
      key.SetValue("DBNAME", StringCipher.Encrypt(Console.ReadLine(), "@xx")); 
      key.Close(); 
      Console.ForegroundColor = _color; 
      _prc.Close(); 
     } 
    } 
    catch (Exception ex) 
    { 
     _eventLog.WriteEntry(ex.Message); 
    } 
    _eventLog.WriteEntry("OnBeforeInstall Finished"); 
    base.OnBeforeInstall(savedState); 
} 

StringCipher是我用來加密/解密我的文本的自定義函數。稍後開始,我正在從註冊表中讀取這些存儲的值,並將它們解密以將其傳遞到數據庫連接代碼並執行必要的操作。

希望這可以幫助別人。

注意

,您必須安裝使用「InstallUtil」,如果您選擇使用安裝項目進行安裝此服務,此安裝將被固定在一個開放cmd窗口。