生活随笔
收集整理的這篇文章主要介紹了
我的NopCommerce之旅(4): 定时任务之邮件
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一、功能簡介
用戶購買物品生成訂單后,系統將發送郵件提醒給用戶
二、操作步驟
后臺配置一個系統的默認發送郵箱
啟動定時任務,這里包括多個任務,只需要啟動郵件任務
查看郵件發送情況
三、數據庫分析
[dbo].[Log] 系統日志表,可查看郵件發送失敗的異常信息[dbo].[EmailAccount] 系統發送郵件配置表[dbo].[QueuedEmail] 訂單郵件序列表,[SentTries]為重試次數,默認嘗試3次失敗后不再發送。[dbo].[ScheduleTask] 定時任務信息表,存儲定時任務信息。四、源碼解析
根據MVC命名規則,可定位到Nop.Admin.Controllers命名空間,查看ScheduleTaskController的RunNow方法,可跟蹤查看到任務調用機制。通過反射類型,采用autofac實例化對象,然后執行。任務實現Nop.Services.Tasks.ITask接口的Execute()方法,如Nop.Services.Messages.QueuedMessagesSendTask。 1 private ITask CreateTask(ILifetimeScope scope)
2 {
3 ITask task =
null;
4 if (
this.Enabled)
5 {
6 var type2 = System.Type.GetType(
this.Type);
7 if (type2 !=
null)
8 {
9 object instance;
10 if (!EngineContext.Current.ContainerManager.TryResolve(type2, scope,
out instance))
11 {
12 //not resolved
13 instance =
EngineContext.Current.ContainerManager.ResolveUnregistered(type2, scope);
14 }
15 task = instance
as ITask;
16 }
17 }
18 return task;
19 }
1 /// <summary>
2 /// Executes the task
3 /// </summary>
4 /// <param name="throwException">A value indicating whether exception should be thrown if some error happens</param>
5 /// <param name="dispose">A value indicating whether all instances should be disposed after task run</param>
6 /// <param name="ensureRunOnOneWebFarmInstance">A value indicating whether we should ensure this task is run on one farm node at a time</param>
7 public void Execute(
bool throwException =
false,
bool dispose =
true,
bool ensureRunOnOneWebFarmInstance =
true)
8 {
9 ...
10 //initialize and execute 初始化成功后執行任務
11 var task =
this.CreateTask(scope);
12 if (task !=
null)
13 {
14 this.LastStartUtc =
DateTime.UtcNow;
15 if (scheduleTask !=
null)
16 {
17 //update appropriate datetime properties
18 scheduleTask.LastStartUtc =
this.LastStartUtc;
19 scheduleTaskService.UpdateTask(scheduleTask);
20 }
21 task.Execute();
22 this.LastEndUtc =
this.LastSuccessUtc =
DateTime.UtcNow;
23 }
24 }
25 catch (Exception exc)
26 {
27 this.Enabled = !
this.StopOnError;
28 this.LastEndUtc =
DateTime.UtcNow;
29
30 //log error
31 var logger = EngineContext.Current.ContainerManager.Resolve<ILogger>(
"", scope);
32 logger.Error(
string.Format(
"Error while running the '{0}' schedule task. {1}",
this.Name, exc.Message), exc);
33 if (throwException)
34 throw;
35 }
36
37 if (scheduleTask !=
null)
38 {
39 //update appropriate datetime properties
40 scheduleTask.LastEndUtc =
this.LastEndUtc;
41 scheduleTask.LastSuccessUtc =
this.LastSuccessUtc;
42 scheduleTaskService.UpdateTask(scheduleTask);
43 }
44
45 //dispose all resources
46 if (dispose)
47 {
48 scope.Dispose();
49 }
50 }
?
五、技術解析
Autofac的依賴注入反射
轉載于:https://www.cnblogs.com/devilsky/p/5337285.html
新人創作打卡挑戰賽發博客就能抽獎!定制產品紅包拿不停!
總結
以上是生活随笔為你收集整理的我的NopCommerce之旅(4): 定时任务之邮件的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。