1

I want to update data in my database each hour. So, I find good FluentScheduler library for this and create my IJob:

public class InfoLoader : IJob
{
    private readonly DataContext _db;

    public InfoLoader(DataContext db)
    {
        _db = db;
    }

    public void Execute()
    {
        foreach (User user in _db.Users.ToList())
        {
            foreach (Info i in user.Info.ToList())
            {
                UpdateInfo(i);
            }
        }
    }

    private void UpdateInfo(Info info)
    {
        // do some operations to update information in db
    }
}

And of course I create my Registry implementation to schedule all tasks which I need:

public class LoadersRegistry : Registry
{
    public LoadersRegistry()
    {
        Schedule<InfoLoader>().ToRunNow().AndEvery(1).Hours();
    }
}

Also I add following code in my Program.cs file to initalize scheduler and start it:

JobManager.JobException += (obj) => { logger.LogError(obj.Exception.Message); };
JobManager.Initialize(new LoadersRegistry());

But when I run my application I see following error:

Running app

I understand that LoadersRegistry can't create instance of InfoLoader (during JobManger initializes it in Program.cs), because InfoLoader receives DataContext. But I can't do not receive DataContext, because I need it to add data in my database.

Unfortunately I can't find a way to fix this issue.

Thanks for any help.

P.S. I read about using FluentScheduler in asp.net core, but developers of this library said that this feature will not be available in the future because of this, so I still don't know how I can solve the issue.

V. Panchenko
  • 774
  • 1
  • 10
  • 32
  • 1
    FYI - Background scheduling is [built-in to .NET Core 2.x](https://blogs.msdn.microsoft.com/cesardelatorre/2017/11/18/implementing-background-tasks-in-microservices-with-ihostedservice-and-the-backgroundservice-class-net-core-2-x/), there is no need to use a 3rd party library for it. – NightOwl888 Feb 17 '18 at 21:14
  • thanks, I start reading the article you specify! :) – V. Panchenko Feb 17 '18 at 21:18
  • 1
    @NightOwl888, it requires a lot of code to create own `IHostedService` to schedule task, so I will be happy to solve this small problem instead of re-write this part of project to use `IHostedService`. – V. Panchenko Feb 17 '18 at 23:09

2 Answers2

2

As per the API document you'll have to change the way you register. Following is just one way of doing it.

public LoadersRegistry()
    {
        var dataContext = new DataContext();
        Schedule(()=> new InfoLoader(dataContext)).ToRunNow().AndEvery(1).Hours();
    }

Here I'm doing new DataContext() but you could make dataContext available however you like as long as you are newing up InfoLoader with it.

sbp
  • 913
  • 8
  • 19
1

If someone will meet with similar issue, this is solution which helps me:

You just need to do initialization inside Startup.cs file:

public void ConfigureServices(IServiceCollection services)
    var provider = services.BuildServiceProvider();

    JobManager.Initialize(new LoadersRegistry(
        provider.GetRequiredService<DataContext>()
    ));

    services.AddMvc();
}

And of course, LoadersRegistry should receive DataContext instance, and InfoLoader should receive this instance in constructor:

public LoadersRegistry(DataContext db)
{
    Schedule(new InfoLoader(db)).ToRunNow().AndEvery(30).Seconds();
}

Good luck! :)

V. Panchenko
  • 774
  • 1
  • 10
  • 32