JobActivator, ASP.NET 5 RC1 IoC and DbContext?

I have been able to get Hangfire running with this version of Kestrel

“Microsoft.AspNet.Server.Kestrel”: “1.0.0-rc1-final”

(with tips on using ApplicationBuilderExtensions from here: How to config Hangfire with Asp.Net 5? )

However, I can not figure out for the life of me how to get access to the database context via my jobs that are scheduled. I tried using this technique related to creating a custom JobActivator, unfortunately the iContainer here does not have the “Resolve(type)” method. (http://docs.hangfire.io/en/latest/background-methods/using-ioc-containers.html)

If I try to instantiate the job using the existing database context, I get the “No parameterless constructor defined for this object” error in Hangfire.

I tried to setup database access using the repository pattern in ConfigureServices, and then add:

services.AddScoped<IApplicationRepository, ApplicationRepository>();

But, how do i inject the ApplicationRepository into a JobActivator to access the context in my jobs?

I’m quite lost…

ok, i was finally able to get things working without having to create a custom JobActivator…

In the “job” I have to use slimmed down version of my ApplicationContext which only has my DbSet defined, and an override for OnConfiguring (since I’m unable to figure out how to inject with Microsoft.Extensions.DependencyInjection )

so, my job gets queued as such from the web app controller:

BackgroundJob.Schedule(() => new JobWorker().ProcessJob(job.Id), TimeSpan.FromSeconds(30));

where JobWorker is:

    public class JobWorker
    {
        public void ProcessJob(int jobId)
        {
            using (var _context = new JobDbContext())
            {
                var job = _context.Jobs.Where(t => t.Id == jobId).FirstOrDefault();
                if (job != null)
                {
                    job.IsProcessed = true;
                    _context.Update(job);
                    _context.SaveChanges();

                    Console.WriteLine("[JobWorker], ProcessJob: " + job.Id + "; message = " + job.Message);
                }
            }
        }
    }

of which, JobDbContext is so that:

    public class JobDbContext : DbContext
    {
        public DbSet<JobCache> Jobs { get; set; }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            var connectionString = "Server=(localdb)\\mssqllocaldb;Database=HangfireSample;Trusted_Connection=True;MultipleActiveResultSets=true";
            optionsBuilder.UseSqlServer(connectionString);
            base.OnConfiguring(optionsBuilder);
        }
    }

which my job queue model is like:

    [Table("JobCache")]
    public class JobCache
    {
        public int Id { get; set; }
        public bool IsProcessed { get; set; }
        public string Message { get; set; }
    }

I’m not sure I like having to do this extra using of a different DbContext, as I don’t know the long term affect… but, in my simple test case… it does what is suppose to (—[edited]-- messages are outputed to dnx console (and logged)) it changes the “isProcessed” flag to true… which is all i really wanted to get at (the database).

I really do wish I could figure out how to get IoC working though… I know it’s there, but without knowing how to use the IServiceProvider in Configure with Hangfire… I just couldn’t figure that out :frowning:

almost forgot this: