Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Thomas 319 posts 606 karma points c-trib
    Dec 18, 2019 @ 13:40
    Thomas
    0

    hangfire - RecurringJob - call Method in Controller

    Hey,

    I'm stuck on a issue here.

    I'm trying to run a RecurringJob every night, and a need Hangfire to run a Method from a controller.

    I Have this that should call the methoder in "SyncClubs"

    public class ScheduleHangfireJobs
    {
        public void Sync()
        {
            RecurringJob.AddOrUpdate("Sync", () => SyncClubs(null), Cron.Daily(01, 00));
        }
    
        public void SyncClubs(PerformContext context)
        {
            //here should my method from the controller run.
            ImportClubsController.ClubSync();
        }
    }
    

    But when this run It fails on running the task from hangfire

    System.NullReferenceException: Object reference not set to an instance of an object.
    

    Is ther esomething i'm missing or are doing wrong ? If I remove ImportClubsController.ClubSync(); and just add a context.WriteLine("Normal text!"); Then it works fine...

  • Pasang Tamang 258 posts 458 karma points
    Dec 19, 2019 @ 02:31
    Pasang Tamang
    0

    Hi Thomas,

    It looks like something to do with initializing the controller from your scheduler method. Not sure how your controller looks, as controller is also just a class you might consider using like new ImportClubsController.ClubSync();.

    In general controllers are not to be use as business logic. Suggestion is to use it to maintain relation between view and business layer. You can move ClubSync method to different class file rather than in controller and see if it works.

    Thanks

Please Sign in or register to post replies

Write your reply to:

Draft