I want to create a dependency on service through the registry, in On start method I am calling a method which I can create a dependency in a registry.The below is the code.
But I am facing some exceptions while using it, someone please help me to resolve this issue.
protected override void On Start(string[] args)
{
//System.Diagnostics.Debugger.Launch();
serverDependency();
int.TryParse(System.Configuration.ConfigurationSettings.AppSettings["IntervalTime"].ToString(), out intervalTime);
// Dynamically create new timer
System.Timers.Timer timScheduledTask = new System.Timers.Timer();
// Timer interval is set in miliseconds,
// In this case, we'll run a task every minute
timScheduledTask.Interval = (intervalTime) * 1000;
timScheduledTask.Enabled = true;
// Add handler for Elapsed event
timScheduledTask.Elapsed +=
new System.Timers.ElapsedEventHandler(timScheduledTask_Elapsed);
}
public bool serverDependency()
{
string ServiceName = "MySQL";
// BinaryFormatter bm = new BinaryFormatter();
MemoryStream mem = new MemoryStream();
Microsoft.Win32.RegistryKey reg = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("POSAgent");
if (reg != null)
{
Microsoft.Win32.Registry.LocalMachine.DeleteSubKey("POSAgent");
}
reg = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("services");
if (reg != null)
{
reg = reg.OpenSubKey("POSAgent");
reg.SetValue("DependOnService", ServiceName, RegistryValueKind.MultiString);
}
else
{
reg = Microsoft.Win32.Registry.LocalMachine.CreateSubKey("POSAgent");
reg.SetValue("DependOnService", ServiceName, RegistryValueKind.MultiString);
}
return true;
}
The below is the exception
Service cannot be started. System. IO. IO Exception : The parameter is incorrect.
at Microsoft . Win 32 . Registry Key. Win 32 Error(Int 32 error Code, String str) at Micro soft. Win 32. Registry Key .Create Sub Key (String sub key, Registry Key Permission Check permission Check, Registry Security registry Security) at Microsoft .Win 32. Registry Key. Create Sub Key(String sub key) at POS Agent Service. POS Agent. server Dependency () at POS Agent Service. POS Agent. On Start(String[] args) at System .Service Process. Service Base. Service Queued Main Call back(Object state)