NopCommerce 中Task 原理是服务端开启线程定时跑。
1.在数据表ScheduleTask中添加一条数据,
2.自定义类,继承ITask 即可
using Data.Log4Net; using Data.Services.Tasks; using System; using System.Net; using System.Security.Policy; using System.Web; namespace Data.Services.Common { /// <summary> /// Represents a task for keeping the site alive /// </summary> public partial class KeepAliveTask : ITask { //private readonly IStoreContext _storeContext; //public KeepAliveTask(IStoreContext storeContext) //{ // this._storeContext = storeContext; //} /// <summary> /// Executes a task /// </summary> public void Execute() { //WriteLog.Info("KeepAliveTask,Execute"); string domainUrl = System.Configuration.ConfigurationManager.AppSettings["domainUrl"]; string url = domainUrl + "/keepalive/index";//"http://localhost:8060/keepalive/index"; WriteLog.Info("KeepAliveTask,url=" + url); using (var wc = new WebClient()) { var down = wc.DownloadString(url); WriteLog.Info("KeepAliveTask,down=" + down); } } } }
OK,服务端会每隔60秒 跑 Execute()方法。