1.第一步 添加新建项目

Windows服务+Quartz+EF_日志文件

2.在NuGet下载Quartz包(版本:2.6.2)

Windows服务+Quartz+EF_ide_02

3.在NuGet下载EF包(版本:6.4)

Windows服务+Quartz+EF_业务逻辑_03

4.点击Service1=>切换到视图代码,然后把下面代码copy进去

Windows服务+Quartz+EF_业务逻辑_04

先到步骤9添加日志类


private IScheduler scheduler;         public Service1()         {             // 新建一个调度器工工厂             ISchedulerFactory schedulerFactory = new StdSchedulerFactory();             // 使用工厂生成一个调度器             scheduler = schedulerFactory.GetScheduler();             InitializeComponent();         }          protected override void OnStart(string[] args)         {             Log.WriteLog("", "", "服务已开启");             scheduler.Start();         }          protected override void OnPause()         {             scheduler.PauseAll();             base.OnPause();             Log.WriteLog("", "", "服务已暂停");         }         protected override void OnStop()         {             if (!scheduler.IsShutdown)             {                 scheduler.Shutdown();             }             Log.WriteLog("", "", "服务已停止");          }         protected override void OnContinue()         {             scheduler.ResumeAll();             base.OnContinue();             Log.WriteLog("", "", "服务已路过");         }


5.生成安装程序

Windows服务+Quartz+EF_xml_05

6.点击安装程序,右键属性,修改参数

Windows服务+Quartz+EF_xml_06



Windows服务+Quartz+EF_windows服务_07

7.在app.config里添加quartz配置、数据库连接配置(configuration节点里)


<quartz>         <add key="quartz.scheduler.instanceName" value="ExampleDefaultQuartzScheduler"/>         <add key="quartz.threadPool.type" value="Quartz.Simpl.SimpleThreadPool, Quartz"/>         <add key="quartz.threadPool.threadCount" value="10"/>         <add key="quartz.threadPool.threadPriority" value="2"/>         <add key="quartz.jobStore.misfireThreshold" value="60000"/>         <add key="quartz.jobStore.type" value="Quartz.Simpl.RAMJobStore, Quartz"/>         <!--******************************Plugin配置*********************************************-->         <add key="quartz.plugin.xml.type" value="Quartz.Plugin.Xml.XMLSchedulingDataProcessorPlugin, Quartz"/>         <add key="quartz.plugin.xml.fileNames" value="~/quartz_jobs.xml"/>     </quartz>


8.添加业务逻辑类


namespace WindowsServiceTest {     public class Test1 : IJob     {          public void Execute(IJobExecutionContext context)         {             try             {                 //业务逻辑             }             catch (Exception e)             {                 Log.WriteLog("", "", e.ToString());             }         }     } }


9.日志类


public class Log     {         public static void WriteLog(string type, string className, string content)         {             string path = AppDomain.CurrentDomain.BaseDirectory + "\\log\\";             if (!Directory.Exists(path))//如果日志目录不存在就创建             {                 Directory.CreateDirectory(path);             }             string time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff");//获取当前系统时间             string filename = path + "/" + DateTime.Now.ToString("yyyy-MM-dd") + ".log";//用日期对日志文件命名              //创建或打开日志文件,向日志文件末尾追加记录             StreamWriter mySw = System.IO.File.AppendText(filename);              //向日志文件写入内容             string write_content = time + " " + type + " " + className + ": " + content;             mySw.WriteLine(write_content);              //关闭日志文件             mySw.Close();         }     }


10.添加quartz_jobs.xml文件(配置信息在步骤7里面)

Tip:WindowsServiceTest是windows服务的项目名,Test1是业务逻辑执行类名


<?xml version="1.0" encoding="UTF-8"?>  <!-- This file contains job definitions in schema version 2.0 format -->  <job-scheduling-data xmlns="http://quartznet.sourceforge.net/JobSchedulingData" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.0">      <processing-directives>         <overwrite-existing-data>true</overwrite-existing-data>     </processing-directives>      <schedule>         <!--定义示例任务1 Job-->         <job>             <name>Test1</name>             <group>Test1Group</group>             <description>Quartz.Net示例任务1</description>             <job-type>WindowsServiceTest.Test1,WindowsServiceTest</job-type>             <durable>true</durable>             <recover>false</recover>         </job>          <!--定义示例任务1 触发器 每月最后一天16点40分执行一次Test1任务-->         <trigger>             <cron>                 <name>Test1Trigger</name>                 <group>Test1TriggerGroup</group>                 <job-name>Test1</job-name>                 <job-group>Test1Group</job-group>                 <!--下午15点53分执行-->                 <cron-expression>0 53 15 * * ?</cron-expression>             </cron>         </trigger>     </schedule> </job-scheduling-data>


11.安装windows服务(生成=>进入bin=>进入debug,看一下有没有生成的windows服务exe)

tip:将以下几个bat文件copy到debug文件下

①.安装.bat


sc create MyVipWinService binPath= "%~dp0WindowsServiceTest.exe" start= auto net start MyVipWinService pause


②.启动.bat


net start MyVipWinService pause


③.停止.bat


net stop MyVipWinService pause


④.卸载.bat


net stop MyVipWinService sc delete MyVipWinService binPath= "%~dp0JDWindowsServiceTest.exe" start= auto pause


⑤.重启.bat


net stop MyVipWinService net start MyVipWinService pause


copy完之后,双击安装.bat,会弹出cmd提示有没有安装成功

最后去服务瞅一眼有没有安装成功!

Windows服务+Quartz+EF_xml_08