调度Web Api方法以设置的间隔运行 [英] Scheduling Web Api method to run on set intervals

查看:65
本文介绍了调度Web Api方法以设置的间隔运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我当前的项目中,需要安排一种方法,使其以设定的时间间隔运行,例如每周一次,目前通过Windows服务创建HttpClient并点击所需的控制器方法来完成.

In my current project there is a need to schedule a method to run at set intervals e.g. once a week, and currently this is done via a windows service creating an HttpClient and hitting the desired controller method.

我想知道是否可以在Web Api项目本身中实现自动化,而不是使用外部服务.到目前为止,我还没有找到任何有关此操作的文档.

I was wondering whether this is possible to automate in the Web Api project itself, rather than using an external service. So far I have not found any documentation on doing this.

很抱歉,没有代码示例可供使用,因为我还没有找到一个基础.

Apologies for not having a code sample to work from as I have not found a base to start from yet.

推荐答案

如果您需要安排每周执行一次后台任务,则可以使用 FluentScheduler (

If you need to schedule a background task to run every week, you can use FluentScheduler (NuGet link) to run it for you. You can do something like this:

public class WeeklyRegistry : Registry
{
    public WeeklyRegistry()
    {            
        Schedule<WeeklyTask>().ToRunEvery(1).Weeks(); // run WeeklyTask on a weekly basis           
    }
}

public class WeeklyTask : IJob
{
    public void Execute()
    {            
        // call the method to run weekly here
    }
}

更新 FluentScheduler 的新版本对API进行了少许更改.现在,该任务应源自IJob,而不是ITask.更新了我的示例以反映这一点.

Update The new version of FluentScheduler changed the API slightly. The task should now be derived from IJob, not ITask. Updated my example to reflect this.

这篇关于调度Web Api方法以设置的间隔运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆