简体   繁体   English

在asp.net应用程序中运行任务计划?

[英]Run a task schedule within asp.net application?

I have an asp.net application which needs to call an API very 5 minutes to get the latest data from a server. 我有一个asp.net应用程序,需要调用API非常5分钟才能从服务器获取最新数据。 Currently, we plan to 1) build a console application and then use windows server task scheduler to schedule it; 当前,我们计划1)构建一个控制台应用程序,然后使用Windows Server Task Scheduler对其进行调度; or, 2) build a windows service for this. 或2)为此构建Windows服务。

Is it possible to do this within asp.net application? 是否有可能在asp.net应用程序中执行此操作? If yes, how? 如果是,怎么办?

We just want to see whether there are better solution out there. 我们只想看看是否有更好的解决方案。

thanks 谢谢

As of .NET 4.5.2 there is a QueueBackgroundWorkItem API that allows you to create your own reoccuring task implementation ( see here for details and examples ) however I would recommend taking a look at some popular and more established open source libraries available. 从.NET 4.5.2开始,有一个QueueBackgroundWorkItem API,该API可让您创建自己的重新发生的任务实现( 有关详细信息和示例,请参见此处 ),但是我建议您看一下一些可用的,更流行的开放源代码库。 The most popular being hangfire.io 最受欢迎的是hangfire.io

After installing it via Nuget ( PM> Install-Package Hangfire ) and configuring it with your ASP.NET MVC application like so: 通过Nuget( PM> Install-Package HangfirePM> Install-Package Hangfire并使用ASP.NET MVC应用程序对其进行配置后,如下所示:

public void Configuration(IAppBuilder app)
{
    GlobalConfiguration.Configuration.UseSqlServerStorage("<connection string or its name>");

    app.UseHangfireDashboard(); //optional
    app.UseHangfireServer(); 
}

You can start to create fire and forget schedules, delayed or reoccurring schedules like so: 您可以开始制造火灾而忘记时间表,延迟或重复出现的时间表,如下所示:

BackgroundJob.Enqueue(() => Console.WriteLine("Fire-and-forget"));
BackgroundJob.Schedule(() => Console.WriteLine("Delayed"), TimeSpan.FromDays(1));
RecurringJob.AddOrUpdate(() => Console.WriteLine("Daily Job"), Cron.Daily);

What's also really nice about Hangfire is that it's got a really polished reporting feature that allows you to monitor your sheduled tasks: Hangfire的真正优点还在于它具有完善的报告功能,可让您监视自己的任务:

在此处输入图片说明

You can see more screenshots of the reporting dashboard here . 您可以在此处查看报告信息中心的更多屏幕截图。

ASP.Net is really for providing an HTML user interface to users. ASP.Net实际上是为用户提供HTML用户界面。 As the solution you are wanting does not need a user interface, creating it as an ASP.Net application makes the job of installing and maintaining the application that much harder. 由于您需要的解决方案不需要用户界面,因此将其创建为ASP.Net应用程序会使安装和维护应用程序的工作变得更加困难。

Here is a post talking about the dangers of recurring background tasks in ASP.NET, it includes examples of how to do it. 这是一篇有关在ASP.NET中重复执行后台任务的危险的文章 ,其中包括有关如何执行此任务的示例。 Probably best to use a library such as Hangfire to avoid doing it yourself. 最好使用诸如Hangfire之类的库来避免自己做。

As another option, you can even create a powershell script that is kicked off by the task scheduler. 作为另一个选择,您甚至可以创建由任务计划程序启动的Powershell脚本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM