Home >Backend Development >C#.Net Tutorial >A brief introduction to the .NET background timing service framework

A brief introduction to the .NET background timing service framework

黄舟
黄舟Original
2017-07-27 16:17:363394browse

As long as developers with certain experience have come into contact with background services, they are inseparable from service creation, scheduling logic processing, business logic writing and other links. Often when we create a new background service project, we will copy the previous code, write some threads and other methods to complete it, and then deal with the installation of the service. Most of the time is wasted on these repetitive tasks.
What is provided here is a simple background service processing framework. The simple background service processing framework supports (execution at specified intervals; execution once a day at a specified time every day; execution once at a specified time; specifying the start and end time every day and in accordance with the specified interval execution), when developing a new scheduled service task, you only need to implement the abstract class method, add 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 the task
Execution at specified intervals
Every day Execute once a day at a specified time
Execute once a day at a specified time
Specify the start and end time every day and execute according to the specified interval

 

The following are A task that regularly executes a stored procedure.

Inherit the base class and write 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("开始执行存储过程 {0}", Config.Param));
                        SQLHelperFactory.Instance.ExecuteNonQuery(Config.Param, null);
                        LogFactory.GetLogger().Info(string.Format("执行存储过程 {0} 完成", Config.Param));
                    }
                }
            }            catch (Exception ex)
            {
                LogFactory.GetLogger().Error(string.Format("执行存储过程 {0} 异常:{1}", Config.Param, ex));
            }
        }
    }
}

Configure the service name

Configure each The execution interval is 60 seconds


 [
  { //循环执行任务 每次执行间隔60秒
    "ServiceName": "CallProcTask-proc_test任务",//服务名称 非空
    "Assembly": "Services.exe",//程序集 非空
    "Methods": "Services.Tasks.CallProcTask",//执行类名  对应业务的类名 非空
    "S_Interval": 60,//间隔时间 单位秒
    "ExecType": 0,//执行类型 ( 0:按指定间隔时间执行 1:每天指定时间执行 每天一次 2:指定时间执行一次 3.每天指定开始和结束时间并且按照指定间隔时间执行) 可空默认0
    "Param": "proc_test"//自定义参数 在本案例中为SQL参数 可空  }
]

After the writing is completed, compile and run: Install.bat to see the corresponding service in the service manager.

Frame code path: Framework code

The above is the detailed content of A brief introduction to the .NET background timing service framework. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn