.NET定时任务执行管理器开源组件–FluentScheduler,可以在web应用程序里面使用

github作者的例子

https://github.com/fluentscheduler/FluentScheduler


public class MvcApplication : System.Web.HttpApplication
    {
        /// 
        /// 数据上传服务地址,比如 http://localhost:89/DataToJt_WS.asmx
        /// 
        public static readonly string severApiUploadAddress = System.Configuration.ConfigurationManager.AppSettings["SeverApiUploadAddress"];

        //启动服务
        protected void Application_Start()
        {
            RegisterGlobalFilters(GlobalFilters.Filters);//注册过滤器
            RegisterRoutes(RouteTable.Routes);

            //创建任务
            try
            {                
                string timeSet = System.Configuration.ConfigurationManager.AppSettings["RunTime"];//获取配置文件任务执行设置时间
                DateTime setRunTime = Convert.ToDateTime(timeSet);
                Registry task = new Registry();
                task.Schedule(() => Run_ServerUpload()).ToRunEvery(1).Days().At(setRunTime.Hour, setRunTime.Minute);//任务每天定时执行
                FluentScheduler.JobManager.Initialize(task);
                //启动服务日志
                LogManager.AddLog("700", "启动服务成功", "UplodLog");
                LogManager.AddLine("UplodLog", 1);
            }
            catch (Exception)
            {
                LogManager.AddLog("700", "启动服务失败", "UplodLog");
                LogManager.AddLine("UplodLog", 1);
            }
        }
        /// 
        /// 自动执行上传方法
        /// 
        private void Run_ServerUpload()
        {
            string timeSet = System.Configuration.ConfigurationManager.AppSettings["RunTime"];
            LogManager.AddLog("300", "设置的执行时间" + timeSet, "UplodLog");
            LogManager.AddLog("300", "======开始自动执行上传方法", "UplodLog");
            WebApplicationBAOJI.Controllers.HomeController hc = new Controllers.HomeController();
            string dateSeach = DB.GetLiJiDate().ToString("yyyy-MM-dd");

            hc.Send_Laboratory_Data(dateSeach);
            hc.Send_Instorage_Check(dateSeach);
            hc.Send_DumpStorage();
            hc.Send_Measure_Train(dateSeach);
            hc.Send_OrgDumpInfo(dateSeach);
            hc.Send_Furnace_Rqteamdata(dateSeach);
            hc.Send_DumpOperationOut(dateSeach);
            hc.Send_DumpOperationIn(dateSeach);
            hc.Send_Coalyard_DevicesLog(dateSeach);
            hc.Send_DumpInfo();
            hc.Send_Clrzc(dateSeach);

            LogManager.AddLine("UplodLog", 1);
            LogManager.AddLog("300", "自动执行上传完成", "UplodLog");
        }

       protected void Application_End()
        {
            LogHelpter.AddLog("进程即将被IIS回收");
            LogHelpter.AddLog("重新访问一个页面,以唤醒服务");
            //地址最好是全路徑,帶有http://開頭的,參考:http://localhost:8087/
            string strUrl = System.Configuration.ConfigurationManager.AppSettings["SelfAddress"];//本程序部署地址  
            try
            {
                System.Net.WebClient wc = new System.Net.WebClient();
                System.IO.Stream stream = wc.OpenRead(strUrl);
                System.IO.StreamReader reader= new System.IO.StreamReader(stream);
                string html = reader.ReadToEnd();
                //LogHelpter.AddLog("訪問的頁面" + html);
                if (!string.IsNullOrWhiteSpace(html))
                {
                    LogHelpter.AddLog("喚醒程序成功");
                }
                reader.Close();
                reader.Dispose();
                stream.Close();
                stream.Dispose();
                wc.Dispose();
            }
            catch (Exception ex)
            {
                LogHelpter.AddLog("喚醒異常" + ex.Message);
            }           
        }



你可能感兴趣的:(ASP.NET,MVC,vs,C#,前沿技术,IIS,IIS7)