繁体   English   中英

无法从我的ASP.NET应用程序重新启动Windows服务

[英]Unable to restart windows service from my ASP.NET application

我正在尝试使用以下代码从asp.net应用程序重新启动Windows Time Service,但它始终返回sa TimeoutException。 我尝试了各种方法来删除此错误并重新启动服务,但不幸的是它失败了。 我为此目的使用的代码如下所示:

private ServiceController service = 
     new ServiceController( "W32Time", Environment.MachineName );
private TimeSpan timeout = TimeSpan.FromMilliseconds( 35000 );//15000 was the old value

// Restart W32Time Service
private void RestartService( )
{
    try
    {
        // Stop service if it is running
        if( service.Status == ServiceControllerStatus.Running )
        {
            service.Stop( );
            service.WaitForStatus( ServiceControllerStatus.Stopped, timeout );
        }

        // Start service if it is stopped
        if( service.Status == ServiceControllerStatus.Stopped )
        {
            if (!(service.Status.Equals(ServiceControllerStatus.Stopped) || 
                  service.Status.Equals(ServiceControllerStatus.StopPending)))
            {
                service.Stop();
            }

            service.Start( );
            service.WaitForStatus( ServiceControllerStatus.Running, timeout );
        }
    }
    catch( Exception ex )
    {
        log.Error( "Error in restarting windows service.", ex );
    }
}

我正在使用Windows7。有人可以建议我解决此问题吗? 任何帮助将不胜感激。

对于VS内置的Web服务器,您的示例对我来说很好。

这导致我认为运行您的Web应用程序的用户无权启动/停止服务。 根据应用程序的配置方式,它可以是AppPool用户,也可以是您登录的用户。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM