.net重启iis线程池和iis站点程序代码分享

复制代码 代码如下:


 /// <summary>
        /// 根据名字重启站点.(没重启线程池)
        /// </summary>
        /// <param></param>
        static void RestartWEbSite(string sitename)
        {
            try
            {
                var server = new ServerManager();
                var site = server.Sites.FirstOrDefault(s => s.Name == sitename);
                if (site != null)
                {
                    site.Stop();
                    if (site.State == ObjectState.Stopped)
                    {
                    }
                    else
                    {
                        Console.WriteLine("Could not stop website!");
                        throw new InvalidOperationException("Could not stop website!");
                    }
                    site.Start();
                }
                else
                {
                    Console.WriteLine("Could not find website!");

throw new InvalidOperationException("Could not find website!");
                }

}
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }

/// <summary>
        /// 重启完之后.要再检测下.是否开启了
        /// </summary>
        /// <param></param>
        static void FixWebsite(string sitename)
        {
            try
            {
                var server = new ServerManager();
                var site = server.Sites.FirstOrDefault(s => s.Name == sitename);
                if (site != null)
                {
                    if (site.State != ObjectState.Started)
                    {
                        Thread.Sleep(500);

//防止状态为正在开启
                        if (site.State != ObjectState.Started)
                        {
                            site.Start();
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }

重启iis线程池:

复制代码 代码如下:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wjgyfd.html