专业网站建设品牌,十四年专业建站经验,服务6000+客户--广州京杭网络
免费热线:400-963-0016      微信咨询  |  联系我们

C#重启IIS指定网站和指定应用程序池

当前位置:网站建设 > 技术支持
资料来源:网络整理       时间:2023/2/14 0:37:38       共计:3684 浏览
using Jinher.AMP.BTP.Deploy;
using Microsoft.Web.Administration;
using Redis.Helper;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Redis
{

    public class Program
    {
        static readonly string AppPoolName = ConfigurationManager.AppSettings["ApplicationPoolName"].ToString();
        static readonly string WebSiteName = ConfigurationManager.AppSettings["WebSiteName"].ToString();
        static readonly int SleepTime = int.Parse(ConfigurationManager.AppSettings["SleepTime"].ToString());
        static ServerManager sm;

        static void Main(string[] args)
        {
            Console.WriteLine($"检测程序启动,【{WebSiteName}】当网站或其应用池停下后,会自动启动。");
            sm = new ServerManager();
            new Thread(RecoveryWebSite).Start();
        }

        static void RecoveryWebSite()
        {
            while (true)
            {
                try
                {
                    var pool = sm.ApplicationPools[AppPoolName];
                    if (pool != null && pool.State == ObjectState.Stopped)
                    {
                        Console.WriteLine("检测到应用池" + AppPoolName + "停止服务");
                        Console.WriteLine("正在启动应用池" + AppPoolName);
                        if (pool.Start() == ObjectState.Started)
                        {
                            Console.WriteLine("成功启动应用池" + AppPoolName);
                        }
                        else
                        {
                            Console.WriteLine("启动应用池" + AppPoolName + "失败. " + SleepTime / 60 + "秒后重试启动");
                        }
                    }

                    var site = sm.Sites[WebSiteName];
                    if (site != null && site.State == ObjectState.Stopped)
                    {
                        Console.WriteLine("检测到网站" + WebSiteName + "停止服务");
                        Console.WriteLine("正在启动网站" + WebSiteName);
                        if (site.Start() == ObjectState.Started)
                        {
                            Console.WriteLine("成功启动网站" + WebSiteName);
                        }
                        else
                        {
                            Console.WriteLine("启动网站" + WebSiteName + "失败. " + SleepTime / 60 + "秒后重试启动");
                        }
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message.ToString());
                }

                GC.Collect();
                Thread.Sleep(SleepTime);
            }
        }
    }
}
版权说明:
本网站凡注明“广州京杭 原创”的皆为本站原创文章,如需转载请注明出处!
本网转载皆注明出处,遵循行业规范,如发现作品内容版权或其它问题的,请与我们联系处理!
欢迎扫描右侧微信二维码与我们联系。
·上一条:sqlserver计算时间差DATEDIFF 函数 | ·下一条:AppPoolService-IIS应用程序池辅助类(C#控制应用程序池操作)

Copyright © 广州京杭网络科技有限公司 2005-2025 版权所有    粤ICP备16019765号 

广州京杭网络科技有限公司 版权所有