• 一个监测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();
      }
     }
    }

  • 相关阅读:
    HDU 1003 Max Sum
    HDU 1728 逃离迷宫
    UVA 10057 A midsummer night's dream.
    HDU 1232 畅通工程
    poj3331
    poj3481
    poj1053
    poj3281
    poj3199
    !!! Gridview的多种使用方法总结
  • 原文地址:https://www.cnblogs.com/flyfish/p/381866.html
Copyright © 2020-2023  润新知