一个监测IIS,并定时重新启动的程序。
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Net;
namespace WuyinIISControler
{
?public class MainSrv : System.ServiceProcess.ServiceBase
?{
??private System.Timers.Timer timer1;
??///
??/// 必需的設計器變量。
??///
??private System.ComponentModel.Container components = null;
??string url =? "http://www.5inet.net/checkIIS.asp";
??int timeout = 6000;
??int repeatTime = 300000;
??EventLog log =null;
??int Times = 1;
??
??public MainSrv()
??{
???// 該調用是 Windows.Forms 組件設計器所必需的。
???InitializeComponent();
???// TODO: 在 InitComponent 調用后添加任何初始化
??}
??// 進程的主入口點
??static void Main()
??{
???System.ServiceProcess.ServiceBase[] ServicesToRun;
?
???// 同一進程中可以運行多個用戶服務。若要將
???//另一個服務添加到此進程,請更改下行
???// 以創建另一個服務對象。例如,
???//
???//?? ServicesToRun = New System.ServiceProcess.ServiceBase[] {new Service1(), new MySecondUserService()};
???//
???ServicesToRun = new System.ServiceProcess.ServiceBase[] { new MainSrv() };
???System.ServiceProcess.ServiceBase.Run(ServicesToRun);
??}
??///
??/// 設計器支持所需的方法 - 不要使用代碼編輯器
??/// 修改此方法的內容。
??///
??private void InitializeComponent()
??{
???this.timer1 = new System.Timers.Timer();
???((System.ComponentModel.ISupportInitialize)(this.timer1)).BeginInit();
???//
???// timer1
???//
???this.timer1.Interval = 300000;
???this.timer1.Elapsed += new System.Timers.ElapsedEventHandler(this.timer1_Elapsed);
???//
???// MainSrv
???//
???this.ServiceName = "WuyinIISControler";
???((System.ComponentModel.ISupportInitialize)(this.timer1)).EndInit();
??}
??///
??/// 清理所有正在使用的資源。
??///
??protected override void Dispose( bool disposing )
??{
???if( disposing )
???{
????if (components != null)
????{
?????components.Dispose();
????}
???}
???base.Dispose( disposing );
??}
??///
??/// 設置具體的操作,以便服務可以執行它的工作。
??///
??protected override void OnStart(string[] args)
??{
???log = new EventLog();
???log.Log = "Application";
???log.Source = this.ServiceName;
???foreach(string arg in args)
???{
????if(arg.ToUpper().StartsWith("/URL:"))
?????this.url = arg.Split(Convert.ToChar(":"))[1]+":"+arg.Split(Convert.ToChar(":"))[2];
????if(arg.ToUpper().StartsWith("/TIMEOUT:"))
?????this.timeout = int.Parse(arg.Split(Convert.ToChar(":"))[1].ToString())*1000;
????if(arg.ToUpper().StartsWith("/REPEATTIME:"))
?????this.repeatTime = int.Parse(arg.Split(Convert.ToChar(":"))[1].ToString())*1000*60;
???}
???// TODO: 在此處添加代碼以啟動服務。
???this.timer1.Interval = this.repeatTime;
???this.timer1.Enabled=true;
???if(!CheckIIS(this.url))
????this.RestartIIS();
??}
??private bool CheckIIS(string url)
??{
???bool b = false;
???System.Net.WebResponse wsp = null;
???string msg = "正在執行第" + this.Times.ToString() + "次檢測:\n檢測地址:" + url + "\n超時設置:" + (this.timeout/1000).ToString() + "秒\n檢測間隔時間:" + (repeatTime/1000/60).ToString() + "分\n檢測結果:";
???try
???{
????System.Net.WebRequest wq = WebRequest.Create(url);
????wq.Timeout = this.timeout;
????wsp = wq.GetResponse();
????if(wsp.Headers.Count>0)
????{
?????b = true;
?????msg+="正常";
????}
????wsp.Close();
???}
???catch(Exception ex)
???{
????b = false;
????msg+="錯誤\n詳細內容:" + ex.Message;
???}
???finally
???{
?????log.WriteEntry(msg);
???}
???return b;
??}
??private void RestartIIS()
??{
???try
???{
????System.Diagnostics.Process.Start("iisreset.exe","/restart");
????log.WriteEntry("正在執行重新啟動命令:iisreset.exe /restart");
???}
???catch(Exception ex)
???{
????log.WriteEntry("發生錯誤:\n" + ex.ToString());
???}
??}
?
??///
??/// 停止此服務。
??///
??protected override void OnStop()
??{
???// TODO: 在此處添加代碼以執行停止服務所需的關閉操作。
???this.timer1.Enabled=false;
???log.Close();
???log.Dispose();
??}
??private void timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
??{
???this.Times++;
???if(!CheckIIS(this.url))
????this.RestartIIS();
??}
?}
}
===============================================================================
using System;
using System.Collections;
using System.ComponentModel;
using System.Configuration.Install;
namespace WuyinIISControler
{
?///
?/// ProjectInstaller 的摘要說明。
?///
?[RunInstaller(true)]
?public class ProjectInstaller : System.Configuration.Install.Installer
?{
??private System.ServiceProcess.ServiceProcessInstaller serviceProcessInstaller1;
??private System.ServiceProcess.ServiceInstaller serviceInstaller1;
??///
??/// 必需的設計器變量。
??///
??private System.ComponentModel.Container components = null;
??public ProjectInstaller()
??{
???// 該調用是設計器所必需的。
???InitializeComponent();
???// TODO: 在 InitializeComponent 調用后添加任何初始化
??}
??///
??/// 清理所有正在使用的資源。
??///
??protected override void Dispose( bool disposing )
??{
???if( disposing )
???{
????if(components != null)
????{
?????components.Dispose();
????}
???}
???base.Dispose( disposing );
??}
??#region 組件設計器生成的代碼
??///
??/// 設計器支持所需的方法 - 不要使用代碼編輯器修改
??/// 此方法的內容。
??///
??private void InitializeComponent()
??{
???this.serviceProcessInstaller1 = new System.ServiceProcess.ServiceProcessInstaller();
???this.serviceInstaller1 = new System.ServiceProcess.ServiceInstaller();
???//
???// serviceProcessInstaller1
???//
???this.serviceProcessInstaller1.Account = System.ServiceProcess.ServiceAccount.LocalSystem;
???this.serviceProcessInstaller1.Password = null;
???this.serviceProcessInstaller1.Username = null;
???//
???// serviceInstaller1
???//
???this.serviceInstaller1.DisplayName = "無垠IIS控制器";
???this.serviceInstaller1.ServiceName = "WuyinIISControler";
???this.serviceInstaller1.StartType = System.ServiceProcess.ServiceStartMode.Automatic;
???this.serviceInstaller1.AfterInstall+=new InstallEventHandler(serviceInstaller1_AfterInstall);
???//
???// ProjectInstaller
???//
???this.Installers.AddRange(new System.Configuration.Install.Installer[] {
?????????????????????? this.serviceProcessInstaller1,
?????????????????????? this.serviceInstaller1});
??}
??#endregion
??private void serviceInstaller1_AfterInstall(object sender, InstallEventArgs e)
??{
???System.ServiceProcess.ServiceController sc = new System.ServiceProcess.ServiceController("WuyinIISControler");
???sc.Start(new string[]{"/url:http://www.5inet.net/checkIIS.asp","/timeout:10","/repeatTime:5"});
???sc.Dispose();
??}
?}
}
轉載于:https://www.cnblogs.com/skyover/archive/2004/02/24/1550.html
總結
以上是生活随笔為你收集整理的一个监测IIS,并定时重新启动的程序。的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 使用ADO操作数据库时一个好用的VARI
- 下一篇: 调试断点的条件设置