Access Denied when accessing Service controller from application

I have a web application written in c# mvc which is trying to check the status of windows service whether it is stopped or started. I have written the code but the issue is the code works fine on my local dev machine but when pushed to the server the code errors out stating access is denied.

I have the following code:

            try
            {
                using (ServiceController sc = new ServiceController("Service"))
                {
                    if (sc.Status == ServiceControllerStatus.Running)
                    {
                        //do something
                    }
                }
            }
               catch (Exception ex) { }; 
           } 

The above code keeps failing at Service controller itself.

I also tried to use the below code:

    StringBuilder sb = new StringBuilder();
    Process process = new System.Diagnostics.Process();
    ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();

    startInfo.FileName = @"sc";
    startInfo.Arguments = "query \"My Service\"";
    startInfo.UseShellExecute = false;
    process.StartInfo = startInfo;
    process.OutputDataReceived += (sender, args) => 
    sb.AppendLine(args.Data);
    process.StartInfo.RedirectStandardOutput = true;
    process.Start();
    process.BeginOutputReadLine();
    process.WaitForExit();

Both of the above code keep failing at the Service controller / SC itself.

The error is

Cannot open Service Control Manager on computer '.'. This operation might require other privileges.

Our application is running in IIS under service account and we have given admin rights and remote access rights to this account.

Not sure what else needs to be given here. Is there any specific rights to access windows service?

0
задан 18 April 2018 в 15:28
1 ответ

Попробуйте запустить свой код как «LocalSystem», там тоже хороший фон; https://stackoverflow.com/questions/39326284/cant-query-run-or-stop-a-windows-service

0
ответ дан 5 December 2019 в 06:09

Теги

Похожие вопросы