日期:2014-05-17  浏览次数:20914 次

.net 程序发布到iis上 无法启动webservice服务
小弟自己写了一个webservice服务,然后想通过 网页去启动这个服务在vs上调试 可以启动成功 但是发布到 iis上却启动失败 报错是System.ComponentModel.Win32Exception: Access is denied

代码 如下

  ServiceController service = new ServiceController("DataCompareService");
            string state = service.Status.ToString();
            ServiceControllerPermission scp = new ServiceControllerPermission(ServiceControllerPermissionAccess.Control, service.MachineName, "DataCompareService");
            scp.Demand();
            string IsSucess = "ok";
           //先判断服务的状态
            if (service.Status.ToString() == "Stopped" || service.Status.ToString() == "StopPending")
            {
                
                try
                {
                    service.Start();
                    //如果服务30秒后还没有启动成功,报告失败
                    service.WaitForStatus(ServiceControllerStatus.Running, new TimeSpan(0, 0, 30));
                }
                catch
                {
                    IsSucess = "no";
                }
            }
            else
            {
                try
                {
                //这里先停止服务
                    service.Stop();
                //再次启动服务
                    service.Start();
                    //如果服务30秒后还没有启动成功,报告失败
               &n