System.ComponentModel.Win32Exception: 拒绝访问错误

16

我正在使用C#代码启动和停止窗口服务,但是我遇到了这个错误。

System.ComponentModel.Win32Exception: Access is denied

我的代码:

 public void StartService(string serviceName, int timeoutMilliseconds)
    {
        ServiceController service = new ServiceController(serviceName);
        try
        {
            TimeSpan timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds);

            service.Start();
            service.WaitForStatus(ServiceControllerStatus.Running, timeout);
            lblMessage.Text = "Service Started.";
        }
        catch (Exception ex)
        {
            //lblMessage.Text = "Error in Service Starting.";
            lblMessage.Text = ex.ToString();
        }
    }

1
你确定该用户帐户具有启动服务所需的权限吗?能否发布堆栈跟踪? - Dirk Vollmar
参见此处:http://stackoverflow.com/questions/6070295/starting-a-service-in-asp-net-c-with-the-right-permissions - Dirk Vollmar
堆栈跟踪是 System.InvalidOperationException: 无法在计算机“。”上打开 aspnet_state 服务。---> System.ComponentModel.Win32Exception: 拒绝访问 --- 内部异常堆栈跟踪结束 --- 在 System.ServiceProcess.ServiceController.GetServiceHandle(Int32 desiredAccess) at System.ServiceProcess.ServiceController.Start(String[] args) at System.ServiceProcess.ServiceController.Start() at _Default.StartService(String serviceName, Int32 timeoutMilliseconds)。 - Mahesh
2个回答

18

请确保您服务器中应用程序池标识帐户拥有启动该服务的权限。因为它在ASP.NET开发服务器上运行时是在您的用户帐户(管理员)下运行,所以可以正常工作。 在默认的IIS配置中,此帐户为Network Service或ApplicationPoolIdentity(取决于IIS版本),通常无法管理服务。

因此,请在IIS Manager(应用程序池/您的应用程序池名称/高级设置)中更改池帐户。 您可以使用内置帐户或使用您的域中的一个帐户。

apppool


5

以管理员身份运行您的VS,并加载您的项目。以管理员身份打开开发人员VS命令提示符。使用计算机域名和用户名提供正确的用户名,例如domainname\username。希望这可以解决问题。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接