<span>Windows 服务开发 以及 重启IIS应用池</span>

前言:最近公司部署的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

完结!

 

全部评论

相关推荐

昨天 18:22
门头沟学院 运营
是正式编吗?稳定吗?工资待遇怎么样?转正可以转到腾讯总部吗?
Java抽象带篮子:不是,不稳定,一般,不行
投递腾讯云智研发等公司7个岗位 >
点赞 评论 收藏
分享
05-11 11:48
河南大学 Java
程序员牛肉:我是26届的双非。目前有两段实习经历,大三上去的美团,现在来字节了,做的是国际电商的营销业务。希望我的经历对你有用。 1.好好做你的CSDN,最好是直接转微信公众号。因为这本质上是一个很好的展示自己技术热情的证据。我当时也是烂大街项目(网盘+鱼皮的一个项目)+零实习去面试美团,但是当时我的CSDN阅读量超百万,微信公众号阅读量40万。面试的时候面试官就告诉我说觉得我对技术挺有激情的。可以看看我主页的美团面试面经。 因此花点时间好好做这个知识分享,最好是单拉出来搞一个板块。各大公司都极其看中知识落地的能力。 可以看看我的简历对于博客的描述。这个帖子里面有:https://www.nowcoder.com/discuss/745348200596324352?sourceSSR=users 2.实习经历有一些东西删除了,目前看来你的产出其实很少。有些内容其实很扯淡,最好不要保留。有一些点你可能觉得很牛逼,但是面试官眼里是减分的。 你还能负责数据库表的设计?这个公司得垃圾成啥样子,才能让一个实习生介入数据库表的设计,不要写这种东西。 一个公司的财务审批系统应该是很稳定的吧?为什么你去了才有RBAC权限设计?那这个公司之前是怎么处理权限分离的?这些东西看着都有点扯淡了。 还有就是使用Redis实现轻量级的消息队列?那为什么这一块不使用专业的MQ呢?为什么要使用redis,这些一定要清楚, 就目前看来,其实你的这个实习技术还不错。不要太焦虑。就是有一些内容有点虚了。可以考虑从PR中再投一点产出
点赞 评论 收藏
分享
05-19 15:21
已编辑
华南农业大学 Java
白火同学:你才沟通了200,说实话,北上广深杭这里面你连一座城市的互联网公司都没投满呢,更别说还有各种准一线二线城市了。等你沟通突破了三位数,还没结果再考虑转行的事吧。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务