1

I am creating a windows service which is suppose to look up data in a specific table and then process the record based the status.

I want to pass the DB credentials while I install the service using installutill as parameters and save them inside registry. I have tried doing so using the code bellow, but I keep getting error on the event "OnBeforeInstall".

I believe either I am passing the parameters incorrectly or I am writing code in the wrong event. Need your help to figure what am I doing wrong.

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");
}

I am writing this on the command prompt: installutil RMSBGService.exe /dbtype=sqlserver /datasource=hitin-lt /dbname=rms /userid=admin /password=passw0rd

Error: "Object reference not set to an instance of an object."

P.S. I dont know how to debug the Win Service, so I am using event log to record every thing.

4

1 回答 1

1

我自己设法做到了,这就是我们应该做的。

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 窗口。

于 2015-07-07T09:23:28.833 回答