 |
|
 |
System.InvalidOperationException: An exception occurred in the OnAfterInstall event handler of TestService.WindowsServiceInstaller.
The inner exception System.UnauthorizedAccessException was thrown with the following error message: Cannot write to the registry key..
Winmdows 7 x64
What can i do for that ?
|
|
|
|
 |
|
|
 |
|
 |
million times better than manually changing the option
|
|
|
|
 |
|
 |
This did not work for me under WinXP, SP3, VS2010, .NET4.0, however, I found another, working solution at
C# Scheduler[^]
check the comment by corsairX. Worked like a charm!
|
|
|
|
 |
|
 |
Use using(RegistryKey cKey = ..) or explicit call cKey.close()
|
|
|
|
 |
|
 |
When i use this code it :
1) Sets key to 272 as desired.
2) in OnStart I try to show a form but it just says it stops the service.
Any clues?
Mark
|
|
|
|
 |
|
 |
Very good to know, thanks for this small article.
|
|
|
|
 |
|
 |
Excellent article. Looking allover for it.
|
|
|
|
 |
|
|
 |
|
 |
Private Sub ServiceInstaller1_AfterInstall(ByVal sender As System.Object, ByVal e As System.Configuration.Install.InstallEventArgs) Handles ServiceInstaller1.AfterInstall
Dim ServiceKey As Microsoft.Win32.RegistryKey = My.Computer.Registry.LocalMachine.OpenSubKey("SYSTEM\CurrentControlSet\Services\" & ServiceInstaller1.ServiceName, True)
If Not IsNothing(ServiceKey) Then
If Not IsNothing(ServiceKey.GetValue("Type")) Then ServiceKey.SetValue("Type", (CType(ServiceKey.GetValue("Type"), Integer) Or 256))
End If
End Sub
|
|
|
|
 |
|
 |
Hi , I do not want read meny posts below. What can I said
the code above was tested under Xp an 64bit 2003 Server and it works.
[RunInstaller(true)]
public partial class ProjectInstaller : Installer
{
protected override void OnAfterInstall(IDictionary savedState)
{
base.OnAfterInstall(savedState);
RegistryKey ckey = Registry.LocalMachine.OpenSubKey(
@"SYSTEM\CurrentControlSet\Services\" + this.serviceInstaller1.ServiceName, true);
if (ckey != null)
{
if (ckey.GetValue("Type") != null)
{
ckey.SetValue("Type", ((int)ckey.GetValue("Type") | 256));
}
}
}
.....
|
|
|
|
 |
|
 |
In order to make the sample code work I had to move the modification of the Registry key to the overriden OnCommitted method.
protected override void OnCommitted(IDictionary savedState)
{
base.OnCommitted(savedState);
using (RegistryKey ckey = Registry.LocalMachine.OpenSubKey(@"SYSTEM\CurrentControlSet\Services\My Service", true))
{
if (ckey != null)
{
if (ckey.GetValue("Type") != null)
{
ckey.SetValue("Type", (((int)ckey.GetValue("Type")) | 256));
}
}
}
}
modified on Thursday, February 18, 2010 12:26 PM
|
|
|
|
 |
|
 |
This is the key information for me.
|
|
|
|
 |
|
 |
Thanks for sharing this, I am about to hit a deadline and I won't have to stay up all night now!
|
|
|
|
 |
|
 |
You are here: tested->working->love it!!!
void izmoto(char* szKwazi);
|
|
|
|
 |
|
 |
I am getting error saying "From1 does not contain a definition for Installers" what should i do for it?
|
|
|
|
 |
|
 |
you solve my problem!
thanks a lot
--
http://jjwang0430.blogspot.com
|
|
|
|
 |
|
 |
hi,
thanks for giving such a wonderful code on internet.
the code works fine. Now I need to handle exceptions in windows service.Which is best way to inform user to inform about exception in windows service?
Thanks in advance
Regards
Pradeep Sattikar
|
|
|
|
 |
|
 |
hi,
thanks for giving such a wonderful code on internet.
the code works fine. Now I need to handle exceptions in windows service.Which is best way to inform user to inform about exception in windows service?
Thanks in advance
Regards
Pradeep Sattikar
|
|
|
|
 |
|
 |
Hi,
I have created one interactive service using a porperty "Allow service to intract with desktop"
And then while servie is running; in OnStart method one form is displayed.
My problem is that this form is loaded but not compeletly.
Service doesn't stops but i think it is in pause state at that moment.
I waited for some time but it dosent work out.
Plz help. ASAP
Amit
|
|
|
|
 |
|
 |
That's little better:
RegistryKey ckey = Registry.LocalMachine.OpenSubKey(
string.Format(@"SYSTEM\CurrentControlSet\Services\{0}", si.ServiceName), true);
|
|
|
|
 |
|
 |
I've used this code, the check box is successfully set to true but has no effect ? I still have to go manually change is state, apply and then change is state again, and finally restart the service to make it work.
Can you explain that behavior, anyone had this problem ? I have included most of the installer code, in case you can point out the error somewhere. I have no clue, is there a Commit action missing or anything ?
Thanks folks.
using System;
....
namespace ProbeService
{
[RunInstaller(true)]
partial class ProjectInstaller : Installer
{
#region variables
private System.ServiceProcess.ServiceProcessInstaller serviceProcessInstaller1;
private System.ServiceProcess.ServiceInstaller serviceInstaller1;
#endregion
private System.ComponentModel.IContainer components = null;
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Component Designer generated code
private void InitializeComponent()
{
this.serviceProcessInstaller1 = new System.ServiceProcess.ServiceProcessInstaller();
this.serviceInstaller1 = new System.ServiceProcess.ServiceInstaller();
this.serviceProcessInstaller1.Account = System.ServiceProcess.ServiceAccount.LocalSystem;
this.serviceProcessInstaller1.Password = null;
this.serviceProcessInstaller1.Username = null;
this.serviceProcessInstaller1.AfterInstall += new System.Configuration.Install.InstallEventHandler(this.serviceProcessInstaller1_AfterInstall);
this.serviceInstaller1.ServiceName = "DSD PROBE";
this.serviceInstaller1.StartType = System.ServiceProcess.ServiceStartMode.Automatic;
this.serviceInstaller1.AfterInstall += new System.Configuration.Install.InstallEventHandler(this.serviceInstaller1_AfterInstall);
this.Installers.AddRange(new System.Configuration.Install.Installer[] {
this.serviceProcessInstaller1,
this.serviceInstaller1});
}
#endregion
#region override methods
public override void Install(IDictionary mySavedState)
{
base.Install(mySavedState);
try
{
string SocketNumber = Context.Parameters["SocketNumber"];
string assemblypath = Context.Parameters["assemblypath"];
string appConfigPath = assemblypath + ".config";
XmlDocument doc = new XmlDocument();
doc.Load(appConfigPath);
XmlNode configuration = null;
foreach (XmlNode node in doc.ChildNodes)
if (node.Name == "configuration")
configuration = node;
if (configuration != null)
{
XmlNode settingNode = null;
foreach (XmlNode node in configuration.ChildNodes)
if (node.Name == "appSettings")
settingNode = node;
if (settingNode != null)
{
XmlNode NumNode = null;
foreach (XmlNode node in settingNode.ChildNodes)
{
if (node.Attributes["key"] != null)
if (node.Attributes["key"].Value == "SocketNumber")
NumNode = node;
}
if (NumNode != null)
{
XmlAttribute att = NumNode.Attributes["value"];
att.Value = SocketNumber; // Update the configuration file
doc.Save(appConfigPath);
}
}
}
}
catch (FormatException e)
{
string s = e.Message;
}
try
{
CreateEventLog();
}
catch (Exception)
{
}
}
protected override void OnAfterInstall(IDictionary mySavedState)
{
base.OnAfterInstall(mySavedState);
try
{
RegistryKey ckey = Registry.LocalMachine.OpenSubKey(@"SYSTEM\CurrentControlSet\Services\DSD PROBE", true); :wtf:
if (ckey != null)
{
if (ckey.GetValue("Type") != null)
{
ckey.SetValue("Type", ((int)ckey.GetValue("Type") | 256));
}
}
Process proc = new Process();
ProcessStartInfo pi = new ProcessStartInfo();
pi.FileName = "cmd";
pi.UseShellExecute = false;
pi.RedirectStandardInput = true;
pi.RedirectStandardOutput = true;
proc.StartInfo = pi;
proc.Start();
proc.StandardInput.WriteLine("net start \"DSD PROBE\"");
proc.StandardInput.Close();
}
catch (Exception)
{
}
}
protected override void OnBeforeUninstall(IDictionary mySavedState)
{
base.OnBeforeUninstall(mySavedState);
try :^)
{
Process proc = new Process();
ProcessStartInfo pi = new ProcessStartInfo();
pi.FileName = "cmd";
pi.UseShellExecute = false;
pi.RedirectStandardInput = true;
pi.RedirectStandardOutput = true;
proc.StartInfo = pi;
proc.Start();
proc.StandardInput.WriteLine("net stop \"DSD PROBE\"");
proc.StandardInput.Close();
}
catch (Exception)
{
}
}
public override void Rollback(IDictionary mySavedState)
{
.....
}
public override void Uninstall(IDictionary savedState)
{
..........
}
#endregion
}
}
"We can't solve problems by using the same kind of thinking we used when we created them." - Albert Einstein
-- modified at 10:59 Tuesday 31st July, 2007
|
|
|
|
 |
|
 |
The service controller is out of sync with the registry.
If you reboot, it will resync and work properly, but it sucks as a good solution.
As someone suggested below the 'right way is'
ConnectionOptions coOptions = new ConnectionOptions();
coOptions.Impersonation = ImpersonationLevel.Impersonate;
ManagementScope mgmtScope = new System.Management.ManagementScope(@"root\CIMV2", coOptions);
mgmtScope.Connect();
ManagementObject wmiService;
wmiService = new ManagementObject("Win32_Service.Name='" + ServiceMonitorInstaller.ServiceName + "'");
ManagementBaseObject InParam = wmiService.GetMethodParameters("Change");
InParam["DesktopInteract"] = true;
ManagementBaseObject OutParam = wmiService.InvokeMethod("Change", InParam, null);
|
|
|
|
 |
|
 |
You can start and stop your services using this code:
public static class ServiceHelper
{
public static void StartService(string serviceName, int timeoutMilliseconds)
{
ServiceController service = new ServiceController(serviceName);
var servicesDependedOn = service.ServicesDependedOn;
if (null != servicesDependedOn)
{
foreach (var svc in servicesDependedOn)
{
StartService(svc.ServiceName, timeoutMilliseconds);
}
}
try
{
TimeSpan timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds);
service.Start();
service.WaitForStatus(ServiceControllerStatus.Running, timeout);
service.Refresh();
}
catch
{
}
finally
{
service.Dispose();
}
}
public static void StopService(string serviceName, int timeoutMilliseconds)
{
ServiceController service = new ServiceController(serviceName);
var dependentServices = service.DependentServices;
if (null != dependentServices)
{
foreach (var svc in dependentServices)
{
StopService(svc.ServiceName, timeoutMilliseconds);
}
}
try
{
TimeSpan timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds);
service.Start();
service.WaitForStatus(ServiceControllerStatus.Stopped, timeout);
service.Refresh();
}
catch
{
}
finally
{
service.Dispose();
}
}
public static bool IsServiceAvailable(string serviceName)
{
var result = false;
ServiceController service = null;
try
{
service = new ServiceController(serviceName);
result = service.Status == ServiceControllerStatus.Running;
return result;
}
catch
{
return result;
}
finally
{
if (null != service)
{
service.Dispose();
}
}
}
}
|
|
|
|
 |
|
 |
The StopService method was wrong, i fixed that:
public static void StartService(string serviceName, int timeoutMilliseconds)
{
ServiceController service = new ServiceController(serviceName);
var servicesDependedOn = service.ServicesDependedOn;
if (null != servicesDependedOn)
{
foreach (var svc in servicesDependedOn)
{
StartService(svc.ServiceName, timeoutMilliseconds);
}
}
try
{
TimeSpan timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds);
if (service.Status != ServiceControllerStatus.Running)
{
service.Start();
service.WaitForStatus(ServiceControllerStatus.Running, timeout);
service.Refresh();
}
}
catch
{
}
finally
{
service.Dispose();
}
}
public static void StopService(string serviceName, int timeoutMilliseconds)
{
ServiceController service = new ServiceController(serviceName);
var dependentServices = service.DependentServices;
if (null != dependentServices)
{
foreach (var svc in dependentServices)
{
StopService(svc.ServiceName, timeoutMilliseconds);
}
}
try
{
TimeSpan timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds);
if (service.Status != ServiceControllerStatus.Stopped)
{
service.Stop();
service.WaitForStatus(ServiceControllerStatus.Stopped, timeout);
service.Refresh();
}
}
catch
{
}
finally
{
service.Dispose();
}
}
public static bool IsServiceAvailable(string serviceName)
{
var result = false;
ServiceController service = null;
try
{
service = new ServiceController(serviceName);
result = service.Status == ServiceControllerStatus.Running;
return result;
}
catch
{
return result;
}
finally
{
if (null != service)
{
service.Dispose();
}
}
}
}
|
|
|
|
 |