VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > 网站开发 > ASP.net 4.0教程 >
  • asp.net教程之.NET定时任务执行管理器开源组件–

public class MvcApplication : System.Web.HttpApplication
   {
       /// <summary>
       /// 数据上传服务地址,比如 http://localhost:89/DataToJt_WS.asmx
       /// </summary>
       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);
           }
       }
       /// <summary>
       /// 自动执行上传方法
       /// </summary>
       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(object sender, EventArgs e)
       {
           try
           {
               LogManager.AddLog("4", "进程即将被IIS回收", "Application_End");
               LogManager.AddLog("4", "重新访问一个页面,以唤醒服务", "Application_End");
               string strUrl = System.Configuration.ConfigurationManager.AppSettings["SelfAddress"];//本程序部署地址
               System.Net.HttpWebRequest httpWebRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(strUrl);
               System.Net.HttpWebResponse httpWebResponse = (System.Net.HttpWebResponse)httpWebRequest.GetResponse();
               System.IO.Stream stream = httpWebResponse.GetResponseStream();//得到回写的字节流
               httpWebResponse.Close();
           }
           catch (Exception ex)
           {
               LogManager.AddLog("4", "唤醒服务异常:" + ex.Message, "Application_End");
           }
       }



该服务会被IIS回收,所以要在Application_End结束程序时,要自己访问自己,保证不会被iis回收掉,这样才能保证24小时不间断服务,才不会中断,切记

相关教程