【发布时间】:2012-08-24 03:16:10
【问题描述】:
我正在开发 MVC 3 Web 应用程序。我想在 Windows 中使用任务调度程序执行后台任务。是否可以在 Windows 中为 Web 应用程序创建计划? 提前致谢
【问题讨论】:
标签: c# javascript asp.net-mvc-3 jquery
我正在开发 MVC 3 Web 应用程序。我想在 Windows 中使用任务调度程序执行后台任务。是否可以在 Windows 中为 Web 应用程序创建计划? 提前致谢
【问题讨论】:
标签: c# javascript asp.net-mvc-3 jquery
运行此类任务的简单方法是在您的操作中实现它,然后通过 powershell 调用此页面。 为此,您需要执行以下步骤:
1) 允许 powershell 运行未签名的脚本。为此,请运行 powershell 并执行:
Set-ExecutionPolicy RemoteSigned
2) 使用下一个内容创建您的 powershell 脚本:
$client = New-Object System.Net.WebClient
$client.DownloadString("http://yourserver/controller/action") // provide here a correct URL to your action
例如保存在 c:\scripts\task.ps1 中
3) 使用下一条命令在调度程序中创建任务:
powershell.exe "c:\scripts\task.ps1"
【讨论】:
是的,您可以简单地使用任务调度程序调用您的网页
例子:
//Create a new scheduled task to open a browser eg: internet explorer then right
//click properties, in the run window type in with the quotes an example
"C:\Program Files\Internet Explorer\iexplore.exe" "http://www.mywebsite.com/something/myactionpage.chtml"
//If you want to open the page with another browser, just change the first path
“任务”将是控制器内显示 myactionpage.chtml 的代码。像这样:
public ActionResult MyActionPage()
{
// Do some actions
return this.View();
}
【讨论】:
是的,这是可能的。但是计划任务是否会在您的本地计算机上运行,如果您的互联网出现故障,可能会导致问题。
创建任务。创建一个基本的计划任务,设置名称和运行频率。然后在程序/脚本部分输入
"C:\Program Files\Internet Explorer\iexplore.exe" "http://www.yourwebsite.com/backgroundtask"
在我有你的网站/后台任务的地方,输入指向你想要的应用程序页面位置的链接。
还有帮助安排任务的代码库。我听说过的两个是石英:http://quartznet.sourceforge.net/tutorial/index.html 和 后台工作人员:http://backgroundworker.codeplex.com/
检查一下。
【讨论】:
使用 Quartz.net 在 web 应用程序中创建计划的后台任务,而不是使用 windows 任务调度程序。
【讨论】: