我在asp.net 中 用如下的代码 重启 iis 进程   ServiceController sc = new ServiceController("iisadmin");   
 sc.Start();HttpRuntime.UnloadAppDomain();
Process p = new Process();   
结果报错 服务的范例已在运行中  
是怎么回事 

解决方案 »

  1.   

    重启前先判断一下service的状态
      

  2.   

    那上面代码该怎么改啊 ,我就是要重启iis 进程 啊 ,就是要先关闭 再开始啊 
      

  3.   

    if (sc.Status != ServiceControllerStatus.Running)
    {
       sc.Start();
    }
    else
    {
       sc.Stop();
       while (sc.Status != ServiceControllerStatus.Stopped)
       {
          Thread.Sleep(1000);
       }
       sc.Start();
    }
      

  4.   

    ServiceController sc = new ServiceController("aspnet_state");
      if (sc.Status == ServiceControllerStatus.Running)
      {
       sc.Stop();
       sc.WaitForStatus(ServiceControllerStatus.Stopped); //等待服务停止
       if (sc.Status == ServiceControllerStatus.Stopped)
       {
        sc.Start();
        }
      }
      else
      {
      sc.Start();
      }
      

  5.   

      我把下面的这段代码 放在 page_load 事件中的最后    ServiceController sc = new ServiceController("iisadmin");
            if (sc.Status == ServiceControllerStatus.Running)
            {
                sc.Stop();
                sc.WaitForStatus(ServiceControllerStatus.Stopped); //等待服务停止
                if (sc.Status == ServiceControllerStatus.Stopped)
                {
                    sc.Start();
                }
            }
            else
            {
                sc.Start();
            }        HttpRuntime.UnloadAppDomain();
            Process p = new Process(); 
    或是放到 
     控制台程序中这样iis 不工作了,再次浏览网页就打不开了,得先停止iis ,再启动iis 才可以浏览,这是怎么回事