Using Repository and Services inside RecurringJobExtension job

dotnetcore
aspnetcore
recurring
Tags: #<Tag:0x00007f21e652bac0> #<Tag:0x00007f21e652b868> #<Tag:0x00007f21e652b5c0>

#1

Dear All,

I am new to Hangfire. I am using the RecurringJobService extension in which I have a job that uses a repository and another service to perform a task.

RecurringJobService:

namespace Services.ScheduledJobs
{  
    public class RecurringJobService
    {
        [RecurringJob("*/1 * * * *")]
        [Queue("default")]
        public void SyncAllCustomers(ICvrChecker cvrChecker, ICustomerRepository customerRepository)
        {
             // do the task
        }
    }
}

Startup.cs

namespace Api
{
    public class Startup
    {
        public Startup(IConfiguration configuration, IHostingEnvironment env)
        {
            var builder = new ConfigurationBuilder()
                .SetBasePath(env.ContentRootPath)
                .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
                .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
                .AddEnvironmentVariables();

            Configuration = builder.Build();
        }

        public static IConfiguration Configuration { get; private set; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc();

            services.AddDbContext<DatabaseContext>(options =>
                options.UseSqlServer(Configuration.GetConnectionString("ApiDatabase")));

            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new Info { Title = "Data Checker Tool API", Version = "v1" });
            });

            services.RegisterIntegrations();
            services.RegisterCheckers();
            services.RegisterRepositories();
           
            services.AddHangfire(x => { 
                x.UseSqlServerStorage(Configuration.GetConnectionString("ApiDatabase"));
                //using RecurringJobAttribute to build RecurringJob automatically.
                x.UseRecurringJob(typeof(RecurringJobService));
                x.UseDefaultActivator();
                //x.UseActivator<AspNetCoreJobActivator>(new AspNetCoreJobActivator(scopeFactory));
            }
            );            
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseSwagger();
           
            app.UseSwaggerUI(c =>
            {
                c.OAuthUseBasicAuthenticationWithAccessCodeGrant();
                c.SwaggerEndpoint("/swagger/v1/swagger.json", "Data Checker Tool API V1");
            });

            app.UseMvc();

            GlobalConfiguration.Configuration.UseSqlServerStorage(Configuration.GetConnectionString("ApiDatabase"));
            
            app.UseHangfireServer();
            app.UseHangfireDashboard();
        }
    }
}

ServiceExtension.cs:

namespace Api
{
    public static class ServiceExtensions
    {
        public static IServiceCollection RegisterIntegrations(
            this IServiceCollection services)
        {
            // Register integrations
            services.AddScoped<ICvrClient, CvrClient>();

            return services;
        }

        public static IServiceCollection RegisterCheckers(
            this IServiceCollection services)
        {
            // Register checkers
            services.AddScoped<ICvrChecker, CvrChecker>();

            return services;
        }

        public static IServiceCollection RegisterRepositories(
            this IServiceCollection services)
        {
            // Register all repositories
            services.AddScoped<ICustomerRepository, CustomerRepository>();

            return services;
        }
    }
}

Every time the job is called the two arguments (cvrChecker and customerRepository) are null. I guess it is a DI issue, but could not figure out a solution.

Do you have any idea how to resolve this issue?

Thanks!