c# - Passing parameters to Windows Service during installation -
i creating windows service suppose data in specific table , process record based status.
i want pass db credentials while install service using installutill parameters , save them inside registry. have tried doing using code bellow, keep getting error on event "onbeforeinstall".
i believe either passing parameters incorrectly or writing code in wrong event. need figure 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 registry key"); key.setvalue("dbtype", this.context.parameters["dbtype"].tostring()); // throws error, assuming above event entry 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 writing on command prompt: installutil rmsbgservice.exe /dbtype=sqlserver /datasource=hitin-lt /dbname=rms /userid=admin /password=passw0rd
error: "object reference not set instance of object."
p.s. dont know how debug win service, using event log record every thing.
managed on own, suppose it.
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 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 complete setup"); console.writeline("note: if enter wrong information, need reinstall 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 custom function used encrypt/decrypt text. later on start, reading these stored values registry, , decrypt them pass onto database connection code , needful.
hope someone.
note
you must install service using "installutil", if choose install using setup project, installation stuck cmd window open.
Comments
Post a Comment