Nitin Agarwal
Nitin Agarwal

Reputation: 151

Windows, Start service System.InvalidOperationException: Cannot start service on computer '.' Access in Denied(Running as Admin)

I am trying to start service using the below code. This works fine for 99% machines but i get this issue on user machines. Any help to be able to reproduce this error or why this issue happens.

            ServiceController sc = new ServiceController(name);

            if (sc.Status == ServiceControllerStatus.Running ||
                    sc.Status == ServiceControllerStatus.StartPending)
            {
                sc.WaitForStatus(ServiceControllerStatus.Running);
                Logger.Info("Service already running");
                return true;
            }
            sc.Start();

Error I receive is

System.InvalidOperationException: Cannot start service on computer '.'. ---> System.ComponentModel.Win32Exception: Access is denied

I am running with Administrator privileges

When creating the service I also run sc sdset command to make service start stop by non admin processes.

Upvotes: 5

Views: 10239

Answers (4)

big boy
big boy

Reputation: 395

The issue by me was RunningAsLocalService in topshelf, hope this helps someone.

Upvotes: 0

Kirsten
Kirsten

Reputation: 18140

This is not an answer, it is my further exploration of the question.

Here is the log

Running a transacted installation.

Beginning the Install phase of the installation.
See the contents of the log file for the 

D:\devnet10\Flight\Flight.ServiceHost\bin\Debug\flight.servicehost.exe assembly's progress.
The file is located at D:\devnet10\Flight\Flight.ServiceHost\bin\Debug\flight.servicehost.InstallLog.

An exception occurred during the Install phase.
System.InvalidOperationException: An exception occurred in the OnAfterInstall event handler of Flight.ServiceHost.Installation.
   at System.Configuration.Install.Installer.Install(IDictionary stateSaver)
   at Flight.ServiceHost.Installation.Install(IDictionary stateSaver) in D:\devnet10\Flight\Flight.ServiceHost\Installation.cs:line 36
   at System.Configuration.Install.Installer.Install(IDictionary stateSaver)
   at System.Configuration.Install.AssemblyInstaller.Install(IDictionary savedState)
   at System.Configuration.Install.Installer.Install(IDictionary stateSaver)
   at System.Configuration.Install.TransactedInstaller.Install(IDictionary savedState)
The inner exception System.InvalidOperationException was thrown with the following error message: Cannot start service PreFlight on computer '.'..
   at System.ServiceProcess.ServiceController.Start(String[] args)
   at System.ServiceProcess.ServiceController.Start()
   at Flight.ServiceHost.Installation.OnAfterInstall(IDictionary savedState) in D:\devnet10\Flight\Flight.ServiceHost\Installation.cs:line 49
   at System.Configuration.Install.Installer.Install(IDictionary stateSaver)
The inner exception System.ComponentModel.Win32Exception was thrown with the following error message: Access is denied.


The Rollback phase of the installation is beginning.
See the contents of the log file for the D:\devnet10\Flight\Flight.ServiceHost\bin\Debug\flight.servicehost.exe assembly's progress.
The file is located at D:\devnet10\Flight\Flight.ServiceHost\bin\Debug\flight.servicehost.InstallLog.

The Rollback phase completed successfully.

The transacted install has completed.

[Update]

I should mention that I am running WIndows 10 I managed to create an installer using an installer project, which does work.

Upvotes: 0

Subbu
Subbu

Reputation: 2205

I am assuming you are trying to achieve a scenario similar to this, i.e., install a service and start it automatically.

Assuming you have made sure that you are really running as Administrator, i.e., the Admin command prompt or Run As Administrator.

Also, assuming you have restarted the machine to make sure that old version of your service is really removed, as you have tried multiple times to install / uninstall your service.

The error Access is denied essentially means that the user running it does not have access. Since you have said that you are running as Administrator, it is possibly that even Administrator does not have access to start the service. May be you are in a locked down environment (likely via Group Policy), where only the Domain Administrator is the "most powerful" !

The next step would be to investigate the permissions your service has. A useful tool for this is: SubInAcl

https://ss64.com/nt/subinacl.html

Display or modify Access Control Entries (ACEs) for file and folder Permissions, Ownership and Domain.

SubInAcl /service "your service name"

The above command is not easy to use ! You need to download it from the Microsoft web site.

Another useful tool is the SC command. This would normally be available by default.

Service Control - Create, Start, Stop, Query or Delete any Windows SERVICE.

SC sdshow "your service name"

would give details of the permission.

This would give you data which can help in further investigation.

You could also start / stop the service with this command. You can try with this to check whether you are getting the same exception when using this tool as well.

The following serverfault question gives some details about setting permission to a service

https://serverfault.com/questions/187302/how-do-i-grant-start-stop-restart-permissions-on-a-service-to-an-arbitrary-user

If you want to see the Stack trace from installutil you can use the /ShowcallStack option

https://learn.microsoft.com/en-us/dotnet/framework/tools/installutil-exe-installer-tool

/ShowCallStack
Outputs the call stack to the log file if an exception occurs at any point during installation.

Upvotes: 3

Barr J
Barr J

Reputation: 10927

There is a whole thread about it in social msdn. The issue persisted for numerous users and it seems you do not have enough privileges to start the service, in which case you will have to change the service into Administrative account:

make sure that the service is set to Local Account by:

  • Rightclick on the property(in Services.msc panel).
  • select the Log on option

And then check again to see if it's working.

Upvotes: 2

Related Questions