FluentScheduler, an open-source component of. NET scheduled task execution manager, can be used in web applications,
public class MvcApplication: System.Web.HttpApplication
{
/// <summary>
/// Data upload service address, such as https: // localhost: 89 / DataToJt_WS.asmx
/// </ summary>
public static readonly string severApiUploadAddress = System.Configuration.ConfigurationManager.AppSettings ["SeverApiUploadAddress"];
// Start the service
protected void Application_Start ()
{
RegisterGlobalFilters (GlobalFilters.Filters); // Register filters
RegisterRoutes (RouteTable.Routes);
// Create task
try
{
string timeSet = System.Configuration.ConfigurationManager.AppSettings ["RunTime"]; // Get configuration file task execution setting time
DateTime setRunTime = Convert.ToDateTime (timeSet);
Registry task = new Registry ();
task.Schedule (() => Run_ServerUpload ()). ToRunEvery (1) .Days (). At (setRunTime.Hour, setRunTime.Minute); // The task is executed regularly every day
FluentScheduler.JobManager.Initialize (task);
// Start service log
LogManager.AddLog ("700", "Start the service successfully", "UplodLog");
LogManager.AddLine ("UplodLog", 1);
}
catch (Exception)
{
LogManager.AddLog ("700", "Failed to start service", "UplodLog");
LogManager.AddLine ("UplodLog", 1);
}
}
/// <summary>
/// Automatically execute the upload method
/// </ summary>
private void Run_ServerUpload ()
{
string timeSet = System.Configuration.ConfigurationManager.AppSettings ["RunTime"];
LogManager.AddLog ("300", "Set execution time" + timeSet, "UplodLog");
LogManager.AddLog ("300", "====== Start to automatically execute upload method", "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", "Automatically perform upload completion", "UplodLog");
}
protected void Application_End (object sender, EventArgs e)
{
try
{
LogManager.AddLog ("4", "The process will be recycled by IIS", "Application_End");
LogManager.AddLog ("4", "Revisit a page to wake up the service", "Application_End");
string strUrl = System.Configuration.ConfigurationManager.AppSettings ["SelfAddress"]; // This program deployment address
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 (); // Get the byte stream written back
httpWebResponse.Close ();
}
catch (Exception ex)
{
LogManager.AddLog ("4", "Waking service exception:" + ex.Message, "Application_End");
}
}
This service will be recycled by IIS. Therefore, when you end the Application_End program, you need to access yourself to ensure that the application will not be recycled by iis. In this way, the service will not be interrupted for 24 hours, remember