Background services as long as there is a certain experience of developers have been exposed, which can not be separated from service creation, scheduling logic processing, business logic and other aspects of writing. Often when we create a new background service project will copy the previous code, and then write some threads and other ways to complete, and then to deal with the installation of the service problem. Most of the time is wasted on these repetitive tasks.
Here is a simple background service processing framework, simple background service processing box support (at a specified interval of time execution; Specify time per day to execute once a day; Specify the start and end times per day and execute at a specified interval), when developing a new scheduled service task Just implement the methods of the abstract class, add the task configuration, and run the installation script to complete the development of a service.
Framework code path: framework Code
The framework supports configuring the execution type to control the execution logic of a task executing a specified time per day at a specified interval execution once a day specifies the start and end time for each daily and executes at a specified interval of time
The following is a task that periodically executes a stored procedure.
Inheriting base classes and writing business logic code
usingDataaccesshelper.sqlhelper;usingServices.common;usingSystem;namespaceservices.tasks{ Public classCallproctask:servicebase {protected Override voidExec () {Try { if(_isstart) {if(!string. Isnullorwhitespace (Config.param)) {Logfactory.getlogger (). Info (string. Format ("start execution of stored procedure {0}", Config.param)); SQLHelperFactory.Instance.ExecuteNonQuery (Config.param,NULL); Logfactory.getlogger (). Info (string. Format ("execute stored procedure {0} completed", Config.param)); } } } Catch(Exception ex) {Logfactory.getlogger (). Error (string. Format ("execute stored procedure {0} exception: {1}", Config.param, ex)); } } }}
Configure Service Name
Configure 60 seconds per execution interval
[ { //cycle execution interval 60 seconds per execution "ServiceName":"callproctask-proc_test Tasks",//service name is not empty "Assembly":"Services.exe",//assembly not empty "Methods":"Services.Tasks.CallProcTask",//the class name that executes the class name corresponding to the business is not empty "S_interval": -,//interval time unit seconds "Exectype":0,//execution Type (0: performed at a specified interval of time 1: Once a day is performed 2: Once a specified time is executed 3. Specify start and end times per day and execute at specified intervals) nullable default 0 "Param":"proc_test"//custom parameters In this case are nullable for SQL parameters }]
Once written, compile, run: Install.bat to see the corresponding service in Service Manager.
Framework code path: framework Code
Simple. NET Background Timer service Framework