Using FluentScheduler - ASP.NET Core MVC

后端 未结 2 1310
野趣味
野趣味 2021-02-09 01:05

I currently have a simple website setup with ASP.NET Core MVC (.NET 4.6.1), and I would like to periodically do some processes like automatically send emails at the end of every

2条回答
  •  不知归路
    2021-02-09 01:52

    Instead of Program's Main function, I initialized the same in Startup.cs before app.UseMvc..

    public void Configure(...., IDependencyObject dependencyObject)
    {
               ....
               JobManager.Initialize(new MyRegistry(dependencyObject));
    
                app.UseMvc(routes =>
                {
                    routes.MapRoute(
                        name: "default",
                        template: "api/{controller}/{action}/{id?}");
                });
    }
    

    My registry class looks like this:

    public class MyRegistry: Registry
    {
        public MyRegistry(IDependencyObject dependencyObject)
        {            
            Schedule(() => new SyncUpJob(dependencyObject)).ToRunNow().AndEvery(10).Seconds();
        }
    }
    

    My Job class looks like this:

    public class SyncUpJob: IJob
    {
        public SyncUpJob(IDependencyObject dependencyObject)
        {
            DependencyObject= dependencyObject;
        }
    
        public IDependencyObject DependencyObject{ get; set; }
    
        public void Execute()
        {
            // call the method to run weekly here
        }
    }
    

提交回复
热议问题