简体   繁体   中英

Autofac configuration for hangfire

I want to use hangfire in my Asp.Net MVC App and I'm using Autofac as DI.
I've installed Hangfire.Autofac nuget
I want to add a recurring job like this:


UserService.cs

 public class UserService : IUserService { public void MyRecurringJob() { // do something here } }


startup.cs

Unable to resolve the type 'MyApp.Core.Service.UserService' because the lifetime scope it belongs in can't be located. The following services are exposed by this registration: - MyApp.Core.Service.UserService Details

Autofac.Core.DependencyResolutionException: Unable to resolve the type 'MyApp.Core.Service.UserService' because the lifetime scope it belongs in can't be located. The following services are exposed by this registration:
- MyApp.Core.Service.UserService

Details ---> Autofac.Core.DependencyResolutionException: No scope with a tag matching 'AutofacWebRequest' is visible from the scope in which the instance was requested.

If you see this during execution of a web application, it generally indicates that a component registered as per-HTTP request is being requested by a SingleInstance() component (or a similar scenario). Under the web integration always request dependencies from the dependency resolver or the request lifetime scope, never from the container itself.

And I get this error:

 Unable to resolve the type 'MyApp.Core.Service.UserService' because the lifetime scope it belongs in can't be located. The following services are exposed by this registration: - MyApp.Core.Service.UserService Details Autofac.Core.DependencyResolutionException: Unable to resolve the type 'MyApp.Core.Service.UserService' because the lifetime scope it belongs in can't be located. The following services are exposed by this registration: - MyApp.Core.Service.UserService Details ---> Autofac.Core.DependencyResolutionException: No scope with a tag matching 'AutofacWebRequest' is visible from the scope in which the instance was requested. If you see this during execution of a web application, it generally indicates that a component registered as per-HTTP request is being requested by a SingleInstance() component (or a similar scenario). Under the web integration always request dependencies from the dependency resolver or the request lifetime scope, never from the container itself.

我现在无法对其进行测试,但是为了在 Hangfire 和 Autofac 中注册重复性工作,您应该编写

RecurringJob.AddOrUpdate<UserService>("SyncUsers", (user) => user.MyRecurringJob(), Cron.Hourly());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM