Scheduling Web Api method to run on set intervals

后端 未结 4 2403
夕颜
夕颜 2021-02-20 00:58

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

4条回答
  •  有刺的猬
    2021-02-20 01:35

    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().ToRunEvery(1).Weeks(); // run WeeklyTask on a weekly basis           
        }
    }
    
    public class WeeklyTask : IJob
    {
        public void Execute()
        {            
            // call the method to run weekly here
        }
    }
    

    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.

提交回复
热议问题