Daniel
Daniel

Reputation: 137

Issues with hangfire job activation and Autofac

I'm triying to implement hangfire in my projects. I have a issue when I add a RecurringJob to hangfire, when it's fired i get this error:

Autofac.Core.Registration.ComponentNotRegisteredException

The requested service 'XXXX.Services.ScheduleTasks.RepairNotificationSevice' has not been registered. To avoid this exception, either register a component to provide the service, check for service registration using IsRegistered(), or use the ResolveOptional() method to resolve an optional dependency.

I have installed hangfire and hangfire.autofac. I added hangfire configuration in my Startup.cs but my ContainerBuilder data is in another file. My startup.cs file look like:

app.UseHangfire(config =>
        {
            config.UseSqlServerStorage("EmacIntranetEntities");
            config.UseServer();
            var builder = new ContainerBuilder();
            config.UseAutofacActivator(builder.Build());
        });

It not works. I have check to paste all Container builder data between var builder and config.UseAutofacActivator without result. My Autofac configuration is in a file named "Bootstrapper.cs" called from Global.asax. How can I resolve this issue? Thanks

Upvotes: 2

Views: 3595

Answers (1)

user3420988
user3420988

Reputation: 487

var builder = new ContainerBuilder();

You have two separate autofac containers. You need to register the components you want your jobs to have access with. Also note if you using any database connections you are no longer in web mode and will not have access to the web request.

builder.RegisterModule<ServicesModule>(); 

or

builder.RegisterType<XXXX.Services.ScheduleTasks.RepairNotificationSevice>().PropertiesAutowired().

Upvotes: 2

Related Questions