Windows 服務開發 以及 重啟IIS應用池


前言:最近公司部署的IIS項目應用池間斷性停止,導致程序死掉,如下圖

如果不能及時重啟,會導致很嚴重的后果。所以我耗時5分鍾開發了這個服務,用於監聽應用程序池的應用狀態並重啟。

一、windows 服務

  1、打開vs,新建windows服務程序(本實例使用vs 2019)。

 2、點擊MyServices.cs[設計]界面的切換到代碼視圖

兩個默認的方法OnStart和OnStop,顧名思義就是服務啟動和停止的事件(自行按需寫業務,其他方法請查閱官方文檔)

public partial class IISRestart : ServiceBase
    {
        public IISRestart()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            // TODO:  在此處添加代碼以啟動服務。
            var timer = new Timer(1000 * 60) { AutoReset = true, Enabled = true }; //間隔1分鍾
            timer.Elapsed += timer_Elapsed;
            timer.Start();
        }

        protected override void OnStop()
        {
        } 
        // 遍歷應用程序池的應用,如果停止就重啟
        private void timer_Elapsed(object sender, ElapsedEventArgs e)
        {
            var manager = new Microsoft.Web.Administration.ServerManager();
            System.Threading.ThreadPool.QueueUserWorkItem((state) =>
            {
                while (true)
                {
                    var pools = manager.ApplicationPools;
                    foreach (var pool in pools)
                    {
                        if (pool.State == Microsoft.Web.Administration.ObjectState.Stopped)
                            pool.Start();
                    } 
                }
            });
        }
    }

 3、在MyServices.cs[設計]界面右擊,選擇“添加安裝程序”

 

 

 4、單擊ProjiectInstall.cs文件,然后單擊serviceProcessInstaller1,屬性欄的"Account"按需修改,我選擇的"LocalSystem",

單擊serviceInstall1(如上圖),屬性欄的SartType按需設置,我選擇的"Automatic"

然后"Description"和"DisplayName"就是服務的名字和顯示內容了

5、生成解決方案,右擊項目,選擇屬性,在"應用程序"選項卡中把"啟動對象"改為"WindowsService.Program"

 6、①安裝服務,找到項目的"bin\debug"路徑

 

 把"InstallUtil.exe"復制到debug下(框選的文件)。InstallUtil默認在"C:\Windows\Microsoft.NET\Framework\版本號"下

 以管理員身份打開cmd。路徑為debug文件夾,輸入"InstallUtil 應用程序名稱.exe"即可安裝服務

 卸載應用程序為"InstallUtil /u 應用程序名稱.exe"

②程序安裝

1、新建winfoirm程序,拖動4個按鈕,如下圖:

 

配置服務路徑以及名稱

string serviceFilePath = $"{Application.StartupPath}\\服務項目.exe";
string serviceName = "服務名稱";

 2、按鈕事件

 1        //事件:安裝服務
 2         private void btn_install_Click(object sender, EventArgs e)
 3         {
 4             if (this.IsServiceExisted(serviceName)) this.UninstallService(serviceFilePath);
 5             this.InstallService(serviceFilePath);
 6         }
 7 
 8         //事件:卸載服務
 9         private void btn_uninstall_Click(object sender, EventArgs e)
10         {
11             if (this.IsServiceExisted(serviceName))
12             {
13                 this.ServiceStop(serviceName);
14                 this.UninstallService(serviceFilePath);
15             }
16         }
17         //事件:啟動服務 
18         private void btn_start_Click(object sender, EventArgs e)
19         {
20             if (this.IsServiceExisted(serviceName)) this.ServiceStart(serviceName);
21         }
22         //事件:停止服務
23         private void btn_stop_Click(object sender, EventArgs e)
24         {
25             if (this.IsServiceExisted(serviceName)) this.ServiceStop(serviceName);
26         }     

按鈕業務

        //安裝服務  
        private void InstallService(string serviceFilePath)
        {
            using (AssemblyInstaller installer = new AssemblyInstaller())
            {
                installer.UseNewContext = true;
                installer.Path = serviceFilePath;
                IDictionary savedState = new Hashtable();
                installer.Install(savedState);
                installer.Commit(savedState);
                MessageBox.Show("安裝成功");
            }
        }

        //卸載服務  
        private void UninstallService(string serviceFilePath)
        {
            using (AssemblyInstaller installer = new AssemblyInstaller())
            {
                installer.UseNewContext = true;
                installer.Path = serviceFilePath;
                installer.Uninstall(null);
                MessageBox.Show("卸載成功");
            }
        }
        //啟動服務  
        private void ServiceStart(string serviceName)
        {
            using (ServiceController control = new ServiceController(serviceName))
            {
                if (control.Status == ServiceControllerStatus.Stopped)
                {
                    control.Start();
                    MessageBox.Show("啟動成功");
                }
            }
        }

        //停止服務  
        private void ServiceStop(string serviceName)
        {
            using (ServiceController control = new ServiceController(serviceName))
            {
                if (control.Status == ServiceControllerStatus.Running)
                {
                    control.Stop();
                    MessageBox.Show("停止成功");
                }
            }
        } 
View Code

完結!

 


免責聲明!

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



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