2

I know many of you guys will think this problem is a duplicate and already solve before, but I having trouble in this problem and I am really frustrated right now because I can't solve this.

This is the first time I work with Windows Service, I wanted to Install my Service programmatically and I came up with this solution:

ServiceProcessInstaller ProcesServiceInstaller = new ServiceProcessInstaller();
ProcesServiceInstaller.Account = ServiceAccount.LocalSystem;
/**
    I already try to used this
    ProcesServiceInstaller.Account = ServiceAccount.LocalService;
    ProcesServiceInstaller.Username = null;
    ProcesServiceInstaller.Password = null;
**/
ServiceInstaller ServiceInstallerObj = new ServiceInstaller();
InstallContext Context = new System.Configuration.Install.InstallContext();
String path = String.Format("/assemblypath={0}", @"" + System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location));
String[] cmdline = { path };

Context = new System.Configuration.Install.InstallContext("Service-Install.log", cmdline);
ServiceInstallerObj.Context = Context;
ServiceInstallerObj.DisplayName = serviceName;
ServiceInstallerObj.Description = "Sample Description";
ServiceInstallerObj.ServiceName = serviceName;
ServiceInstallerObj.StartType = ServiceStartMode.Automatic;
ServiceInstallerObj.Parent = ProcesServiceInstaller;

System.Collections.Specialized.ListDictionary state = new System.Collections.Specialized.ListDictionary();
ServiceInstallerObj.Install(state); 

the above code is working and I can see it in my Services. but everytime I right click on it and try to Start the Service I get this error:

Windows could not start the [service name] service on Local Computer.
Error 5: Access is Denied.

I search for solution but none of this are working:

First I configure my manifest with this:

<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />

Second this link: Link1 and Link2

by doing the below

*Right-click on top-level folder containing the service executable. Go to Properties
*Go to "Security" Tab
*Click "EDIT"
*Click "ADD"
*Enter the name "SYSTEM","NETWORK SERVICE", "SERVICE" click OK (I already add this 3 account)
*Highlight "SYSTEM","NETWORK SERVICE", "SERVICE" user, and click ALLOW check-box next to "Full control"
*Click OK twice

and nothing is working.

UPDATE

The below image is the Security Permission I added. enter image description here enter image description here

and I already allow all the checkboxes*(Except the Deny)* specially the full control.

and the below image is what I have in Windows Event Viewer. enter image description here

and the Error is Access Denied enter image description here

anyone please can help me? Thank you!

Community
  • 1
  • 1
Polar
  • 3,327
  • 4
  • 42
  • 77
  • Pretty sure service installation requires admin privileges. Run your installer as administrator. – NotMe Jul 22 '16 at 15:34
  • @NotMe - thanks for quick comment. Currently I am running my application via Debug in Visual Studio, and I already added this to my manifest: `` I though this will make the application admin privileges? – Polar Jul 22 '16 at 15:42
  • And the Service was successfully installed in Services base on my Console Output and I can see that it is already added in the list of Services. – Polar Jul 22 '16 at 15:42
  • Did you already check the event log? Please post the relevant error details if this does not yet solve your issue. Btw, *Access is denied* means that your service tries to access a resource where it does not have permissions to, so check what resources (files, Registry, etc) are accessed by your service, under what account your service is running and whether this account has the needed privileges – Dirk Vollmar Jul 22 '16 at 15:43
  • @DirkVollmar - I'm sorry, I am really just a new. Which Event Log do I have to check? I can only see the Output Log in visual studio 2015? – Polar Jul 22 '16 at 15:59
  • Dirk is talking about the windows event viewer. Look at those logs – NotMe Jul 22 '16 at 16:33
  • Do I have to check it on Windows Event Viewer? If so, then how can I find my service here?... – Polar Jul 23 '16 at 10:45
  • There's a lot of stuff in Windows Event Viewer I don't know where do I check it, do I have to look for my application name or my Service name? – Polar Jul 23 '16 at 10:47
  • ok, I found this link to check my service event: http://stackoverflow.com/questions/1067531/are-there-any-log-file-about-windows-services-status I will update my question soon I find out. – Polar Jul 23 '16 at 10:56
  • I already update my question please take a look. If there's missing please just comment. Thank you! – Polar Jul 23 '16 at 11:24
  • Follow this document, including adding a service installer to your windows service application and using installutil to install the service: https://msdn.microsoft.com/en-us/library/aa983583(v=vs.71).aspx – George Helyar Jul 23 '16 at 12:37
  • @George Helyar- Thank you, but this documentation did not help me. I already found this before and I wanted to install my Service programmatically and the code that I am using is currently working but I am not able to start my service. – Polar Jul 23 '16 at 12:53
  • The usual practice is to create a Windows Service application, and either use installutil or, if you want a nice install experience, use a separate installer for it, such as WiX. It's not normal to either write an installer from scratch or have a service install itself, and without detailed error logs it's very hard to see what's going wrong. It could be that your service has installed correctly but something in your service is trying to do something which it doesn't have permissions to do, like install an EventSource, or maybe it needs to be NetworkService rather than LocalService, etc. – George Helyar Jul 23 '16 at 13:15
  • I tried NetworkService but still access is denied even I added both the Network and Network Service permission in security. It's almost 3 days still I didn't solve this. I just want to have a background process that will run until the machine goes to shutdown so I can handle such as process for my application even the UI is not running. – Polar Jul 23 '16 at 13:24

0 Answers0