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 execution types to control the execution logic of tasks
Executes at a specified interval of time
Daily execution time per day
Specify time to execute once
Specify start and end times per day and execute at specified intervals
The following is a task that periodically executes a stored procedure.
Inheriting base classes and writing business logic code
Using dataaccesshelper.sqlhelper;using services.common;using system;namespace services.tasks{Public class Callproctask:servicebase { protected override void Exec () { 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
[ {//Loop execution 60 seconds per execution interval "ServiceName": "Callproctask-proc_test Task",//Service name non-empty "Assembly": "Services.exe",/ /assembly non-null "Methods": "Services.Tasks.CallProcTask",//class name that executes the class name is not empty "s_interval": 60,//interval unit seconds Exectype ": 0,//Execution Type (0: Execution at a specified interval of 1: Daily specified time is performed once per day 2: Specify time to execute once 3. Specify start and end times per day and execute at specified intervals) nullable default 0 " Param ":" Proc_test "/ /custom parameter In this case is a SQL parameter nullable }]
Once written, compile, run: Install.bat to see the corresponding service in Service Manager.
Framework code path: framework Code