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

     1 using Jinher.AMP.BTP.Deploy;
     2 using Microsoft.Web.Administration;
     3 using Redis.Helper;
     4 using System;
     5 using System.Collections.Generic;
     6 using System.Configuration;
     7 using System.IO;
     8 using System.Linq;
     9 using System.Text;
    10 using System.Threading;
    11 using System.Threading.Tasks;
    12 
    13 namespace Redis
    14 {
    15 
    16     public class Program
    17     {
    18         static readonly string AppPoolName = ConfigurationManager.AppSettings["ApplicationPoolName"].ToString();
    19         static readonly string WebSiteName = ConfigurationManager.AppSettings["WebSiteName"].ToString();
    20         static readonly int SleepTime = int.Parse(ConfigurationManager.AppSettings["SleepTime"].ToString());
    21         static ServerManager sm;
    22 
    23         static void Main(string[] args)
    24         {
    25             Console.WriteLine($"检测程序启动,【{WebSiteName}】当网站或其应用池停下后,会自动启动。");
    26             sm = new ServerManager();
    27             new Thread(RecoveryWebSite).Start();
    28         }
    29 
    30         static void RecoveryWebSite()
    31         {
    32             while (true)
    33             {
    34                 try
    35                 {
    36                     var pool = sm.ApplicationPools[AppPoolName];
    37                     if (pool != null && pool.State == ObjectState.Stopped)
    38                     {
    39                         Console.WriteLine("检测到应用池" + AppPoolName + "停止服务");
    40                         Console.WriteLine("正在启动应用池" + AppPoolName);
    41                         if (pool.Start() == ObjectState.Started)
    42                         {
    43                             Console.WriteLine("成功启动应用池" + AppPoolName);
    44                         }
    45                         else
    46                         {
    47                             Console.WriteLine("启动应用池" + AppPoolName + "失败. " + SleepTime / 60 + "秒后重试启动");
    48                         }
    49                     }
    50 
    51                     var site = sm.Sites[WebSiteName];
    52                     if (site != null && site.State == ObjectState.Stopped)
    53                     {
    54                         Console.WriteLine("检测到网站" + WebSiteName + "停止服务");
    55                         Console.WriteLine("正在启动网站" + WebSiteName);
    56                         if (site.Start() == ObjectState.Started)
    57                         {
    58                             Console.WriteLine("成功启动网站" + WebSiteName);
    59                         }
    60                         else
    61                         {
    62                             Console.WriteLine("启动网站" + WebSiteName + "失败. " + SleepTime / 60 + "秒后重试启动");
    63                         }
    64                     }
    65                 }
    66                 catch (Exception ex)
    67                 {
    68                     Console.WriteLine(ex.Message.ToString());
    69                 }
    70 
    71                 GC.Collect();
    72                 Thread.Sleep(SleepTime);
    73             }
    74         }
    75     }
    76 }
  • 相关阅读:
    删除链表的倒数第N个节点
    SVN快速入门(TSVN)
    C# HttpWebRequest提交数据方式浅析
    简单的3个SQL视图搞定所有SqlServer数据库字典
    简单统计SQLSERVER用户数据表大小(包括记录总数和空间占用情况)
    详细讲解Android对自己的应用代码进行混淆加密防止反编译
    PHP之网络编程
    PHP之ThinkPHP模板标签操作
    PHP之ThinkPHP数据操作CURD
    关于数组的取极值和排序
  • 原文地址:https://www.cnblogs.com/happygx/p/9680299.html
Copyright © 2011-2022 走看看