zoukankan      html  css  js  c++  java
  • C#重启IIS指定网站和指定应用程序池

    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);
    }
    }
    }
    }

  • 相关阅读:
    程序员需要看的书
    linux常见命令实践.
    数据库使用简单总结
    剑指offer【书】之简历抒写
    面试中自己项目和你应该问的问题环节总结
    Matlab近期用到的函数(持续更新)
    排序-快速排序算法
    系统运维-hub, repeater, switch, router初览
    C++基础-位运算
    排序-冒泡排序
  • 原文地址:https://www.cnblogs.com/zoujinhua/p/11934227.html
Copyright © 2011-2022 走看看