【发布时间】:2015-08-24 13:05:56
【问题描述】:
在我的 ASP MVC 5 应用程序中,我有这个与数据库相关的操作,我可能需要每月执行一次,大约需要 30 到 60 分钟。 我这样开始这个动作:
Repository dbForCategories = new Repository();
dbForCategories.Database.CommandTimeout = 60000;
var t = Task.Factory.StartNew(async delegate
{
var crs = new ProductCategoryRelationsSetter(dbForCategories, categoryService);
crs.AddProductsCategoriesRelations(oneLineTree);
}, CancellationToken.None, TaskCreationOptions.LongRunning, TaskScheduler.Default);
在后台工作大约 5 分钟后,我退出了应用程序。我认为应用程序重置是因为我使用的一些静态变量被重置。 在 elmah 我没有任何错误。我把我的代码放在 Try Catch 块中。
只希望你们:)
【问题讨论】:
-
"operation that I've to perform once in month maybe and it takes about 30 - 60 minutes"- 这不是 Web 应用程序的任务。这是计划的控制台应用程序或可能是 Windows 服务的任务。 -
或使用 ASP.NET MVC 的任务调度程序库,如 github.com/jgeurts/FluentScheduler
标签: c# asp.net asp.net-mvc entity-framework