创建Windows服务
新建一个Windows服务项目,系统会自动生成一个Service1.cs
我们自己需要定义一个操作类,添加一个Operate.cs文件,在该文件中添加操作方法
在Service1该类中添加:
Timer是用来循环执行Operate.cs文件中操作方法的
在OnStart方法中输入:
手工建立一个方法,用来被Timer执行:
我们可以给项目中添加不同的操作类
安装
我们需要自定义一个安装。在解决方案中添加一个安装和部署中的安装项目
解决方案资源管理器中选择该项目,设置相关属性:
如Title、ProductName、Manufacturer(应用程序或组件制造商的名称,安装时为安装目录)、Author
然后在解决方案资源管理器中右键点击该项目,视图-自定义操作:
在安装、提交、回滚、卸载中分别添加“应用程序文件夹”中的主输出来自(上面建立的项目名称)活动项
再在解决方案资源管理器中右键点击安装项目,添加-项目输出:
添加主输出
在原项目中添加一个安装程序类,如ProjectInstaller.cs,双击这个安装程序类,设置者两个控件:
在serviceInstaller1控件属性窗口中设置属性内容,或者在代码中,查看InitializeComponent();方法
在private void InitializeComponent()中按如下修改:
设置描述Description 、显示名称DisplayName 、服务名称ServiceName
还有启动类型
新建一个Windows服务项目,系统会自动生成一个Service1.cs
我们自己需要定义一个操作类,添加一个Operate.cs文件,在该文件中添加操作方法
在Service1该类中添加:
Timer doTimer;
Operate op1 = new Operate();
Timer是用来循环执行Operate.cs文件中操作方法的
在OnStart方法中输入:
//设置循环时间,以及循环执行方法
int doTimerNum = Convert.ToInt32(RestartMinute) * 60 * 1000;
doTimer = new Timer(doAutoRestartNetReg, null, 0, doTimerNum);
手工建立一个方法,用来被Timer执行:
private void doAutoRestartNetReg(object values)
{
//循环调用方法
op1.RestartAppPool(AppPoolName, LogType);
}
我们可以给项目中添加不同的操作类
安装
我们需要自定义一个安装。在解决方案中添加一个安装和部署中的安装项目
解决方案资源管理器中选择该项目,设置相关属性:
如Title、ProductName、Manufacturer(应用程序或组件制造商的名称,安装时为安装目录)、Author
然后在解决方案资源管理器中右键点击该项目,视图-自定义操作:
在安装、提交、回滚、卸载中分别添加“应用程序文件夹”中的主输出来自(上面建立的项目名称)活动项
再在解决方案资源管理器中右键点击安装项目,添加-项目输出:
添加主输出
在原项目中添加一个安装程序类,如ProjectInstaller.cs,双击这个安装程序类,设置者两个控件:
在serviceInstaller1控件属性窗口中设置属性内容,或者在代码中,查看InitializeComponent();方法
在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.Description = "本服务用于IIS应用程序池自定义操作";
this.serviceInstaller1.DisplayName = "IISAppPool Service";
this.serviceInstaller1.ServiceName = "IISAppPoolService";
this.serviceInstaller1.StartType = System.ServiceProcess.ServiceStartMode.Automatic;
//
// ProjectInstaller
//
this.Installers.AddRange(new System.Configuration.Install.Installer[] {
this.serviceProcessInstaller1,
this.serviceInstaller1});
}
设置描述Description 、显示名称DisplayName 、服务名称ServiceName
还有启动类型