Windows 服务开发 以及 重启IIS应用池

摘要:
所以我花了5分钟开发这个服务来监视应用程序池的应用程序状态并重新启动它。

前言:最近公司部署的IIS项目应用池间断性停止,导致程序死掉,如下图

Windows 服务开发 以及 重启IIS应用池第1张

如果不能及时重启,会导致很严重的后果。所以我耗时5分钟开发了这个服务,用于监听应用程序池的应用状态并重启。

一、windows 服务

  1、打开vs,新建windows服务程序(本实例使用vs 2019)。

Windows 服务开发 以及 重启IIS应用池第2张

 2、点击MyServices.cs[设计]界面的切换到代码视图

Windows 服务开发 以及 重启IIS应用池第3张

两个默认的方法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[设计]界面右击,选择“添加安装程序”

 Windows 服务开发 以及 重启IIS应用池第4张

 Windows 服务开发 以及 重启IIS应用池第5张

 4、单击ProjiectInstall.cs文件,然后单击serviceProcessInstaller1,属性栏的"Account"按需修改,我选择的"LocalSystem",

Windows 服务开发 以及 重启IIS应用池第6张Windows 服务开发 以及 重启IIS应用池第7张

单击serviceInstall1(如上图),属性栏的SartType按需设置,我选择的"Automatic"

Windows 服务开发 以及 重启IIS应用池第8张

然后"Description"和"DisplayName"就是服务的名字和显示内容了

Windows 服务开发 以及 重启IIS应用池第9张

5、生成解决方案,右击项目,选择属性,在"应用程序"选项卡中把"启动对象"改为"WindowsService.Program"

Windows 服务开发 以及 重启IIS应用池第10张

 6、①安装服务,找到项目的"bindebug"路径

 Windows 服务开发 以及 重启IIS应用池第11张

 把"InstallUtil.exe"复制到debug下(框选的文件)。InstallUtil默认在"C:WindowsMicrosoft.NETFramework版本号"下

Windows 服务开发 以及 重启IIS应用池第12张

 以管理员身份打开cmd。路径为debug文件夹,输入"InstallUtil 应用程序名称.exe"即可安装服务

 卸载应用程序为"InstallUtil /u 应用程序名称.exe"

②程序安装

1、新建winfoirm程序,拖动4个按钮,如下图:

Windows 服务开发 以及 重启IIS应用池第13张

配置服务路径以及名称

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         }     

按钮业务

Windows 服务开发 以及 重启IIS应用池第14张Windows 服务开发 以及 重启IIS应用池第15张
        //安装服务  
        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

完结!

免责声明:文章转载自《Windows 服务开发 以及 重启IIS应用池》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇LaTeX技巧23:BIBTeX制作参考文献关于SVN提交强制加入注释下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

随便看看

Datax3.0使用说明

任务是DataX作业的最小单位。每个任务负责一些数据的同步。DataX的调度决策思想是:-DataXJob根据数据库和表划分为100个任务。...

python调用接口,python接收post请求接口(附完整代码)

与Scala语言相比,Python有其独特的优势和广泛的应用。Python调用接口,因此Spark还引入了PySpark,它在框架上提供了一个使用Python语言的接口。Python接收后请求接口,这为数据科学家使用框架提供了一种方便的方式。Python和JVM进程同时出现在驱动程序和执行器上。当通过spark-submit提交PySparkPython脚本...

Delete from join 用法

delete(别名)fromtblA(别名)leftjointblb(别名)on。。。...

无法将您的Kindle连接到Wi-Fi网络怎么办-kindle无法连接wifi-kindle无法连接手机热点

问题描述:当连接到Wi-Fi或移动热点时,Kindle会弹出提示:如果我无法将您的Kindle连接到Wi-Fi网络,该怎么办。步骤1:通过USB数据线将Kindle连接到计算机。2.连接后,我电脑的磁盘将像一个USB闪存驱动器,Kindle磁盘将出现在其中。3.进入Kindle磁盘。在Kindle磁盘下,右键单击创建一个名为WIFI_NO_NET_PROBE...

高通LCD开发常见问题&分析

LCD的fps一般都上限为60,正常使用时候一般设置为58,因为由于LCDpanelrange存在刷新率温度漂移情况,所以在dtsi中关键panel-framerate都不建议修改。平台端的CABL功能算法是用于LCD背光灯相关计算,在更新灰阶前,处理出现不连续不均匀等的状况。当然该算法也可以回导致LCD出现灰阶部分会有波纹以及短暂的不连续的条纹情况。这是由...

线阵相机参数计算,选择合适的相机与镜头

1.精度=对象宽度/像素,此公式用于选择相机。如果要求的精度为0.3mm/像素,对象宽度为1200mm,则像素为4K,因此选择4K相机。2.物体的最大移动速度=精度×最大线频率,此公式用于计算相机是否满足移动速度。0.3mm/pixel×26000Hz=7617mm/s,即物体移动速度低于457m/min,因此4K相机是可以的。3.物体距离=焦距×精度/像素...