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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM