小编Yog*_*ogi的帖子

尝试停止C#Windows服务时拒绝访问

我使用visual studio创建了一个C#Web服务来停止名为SMTPSVC的Windows服务"简单邮件传输协议(SMTP)".

以下是执行此操作的Web方法:

[WebMethod]
public string StopService()
{
    String status = "";
    try
    {
        ServiceController scAlerter = new ServiceController("SMTPSVC");

        Console.WriteLine(scAlerter.DisplayName);
        Console.WriteLine(scAlerter.CanStop);

        scAlerter.Stop();
        Console.WriteLine("Service stopped");
        status = "STOPPED";
    }
    catch (Exception e)
    {
        Console.WriteLine("Exception caught here" + e.ToString());
        status = "Exception" + e.ToString();
    }
    return status;
}
Run Code Online (Sandbox Code Playgroud)

我在IIS 5.1服务器中发布了此Web服务.当我调用该服务时,它会抛出以下"拒绝访问"异常

<?xml version="1.0" encoding="utf-8" ?> 
<string xmlns="http://y-square.org/">
    ExceptionSystem.InvalidOperationException: Cannot open SMTPSVC service on 
    computer '.'. ---> System.ComponentModel.Win32Exception: Access is denied 
    --- End of inner exception stack trace --- at 
    System.ServiceProcess.ServiceController.GetServiceHandle(Int32 desiredAccess)
    at System.ServiceProcess.ServiceController.Stop() …
Run Code Online (Sandbox Code Playgroud)

.net c# web-services visual-studio-2005 iis-5

5
推荐指数
1
解决办法
8950
查看次数

标签 统计

.net ×1

c# ×1

iis-5 ×1

visual-studio-2005 ×1

web-services ×1