C# WebApi中带有Autofac的Hangfire
我在startup.cs中有以下配置,但尽管我已安装并配置了C# WebApi中带有Autofac的Hangfire,c#,asp.net-web-api,autofac,hangfire,hangfire-autofac,C#,Asp.net Web Api,Autofac,Hangfire,Hangfire Autofac,我在startup.cs中有以下配置,但尽管我已安装并配置了Hangifre.Autofacnuget软件包,但还是出现了错误 从中看不到标记与“AutofacWebRequest”匹配的作用域 请求实例的范围。这通常表明 正在请求按照HTTP请求注册的组件 web下的SingleInstance()组件(或类似场景) 集成始终从服务器请求依赖项 DependencyResolver.Current或ILifetimeScopeProvider.RequestLifetime, 不要从容器中取出
Hangifre.Autofac
nuget软件包,但还是出现了错误
从中看不到标记与“AutofacWebRequest”匹配的作用域
请求实例的范围。这通常表明
正在请求按照HTTP请求注册的组件
web下的SingleInstance()组件(或类似场景)
集成始终从服务器请求依赖项
DependencyResolver.Current或ILifetimeScopeProvider.RequestLifetime,
不要从容器中取出
Startup.cs
public void Configuration(IAppBuilder app)
{
var builder = new ContainerBuilder();
//if (AppConfigHelper.PlatformEnvironment == PlatformEnvironment.LocalHost)
builder.RegisterType<NLogLogger>().As<ILogger>().InstancePerLifetimeScope();
//else
//builder.RegisterType<SentryLogger>().As<ILogger>().InstancePerLifetimeScope();
//builder.RegisterWebApiFilterProvider(configuration);
// REGISTER CONTROLLERS SO DEPENDENCIES ARE CONSTRUCTOR INJECTED
builder.RegisterApiControllers(Assembly.GetExecutingAssembly()).PropertiesAutowired();
builder.RegisterControllers(Assembly.GetExecutingAssembly()).PropertiesAutowired();
//These lines warm up dlls and load into memory for automatic regisration
var r = new ReplyRepository(null);
var s = new BankService();
builder.RegisterModule(new SelfRegisterModule());
builder.RegisterModule(new RepositoryModule());
builder.RegisterModule(new ServiceModule());
builder.RegisterModule(new EFModule());
builder
.RegisterType<ApplicationOAuthProvider>()
.As<IOAuthAuthorizationServerProvider>()
.PropertiesAutowired() // to automatically resolve IUserService
.SingleInstance(); // you only need one instance of this provider
builder.RegisterType<SellutionUserStore>().As<IUserStore<ApplicationUser, int>>().InstancePerBackgroundJob().InstancePerRequest();
builder.RegisterType<SellutionUserManager>().AsSelf().InstancePerBackgroundJob().InstancePerRequest();
builder.RegisterType<SellutionRoleManager>().AsSelf().InstancePerBackgroundJob().InstancePerRequest();
builder.RegisterType<SellutionSignInManager>().AsSelf().InstancePerBackgroundJob().InstancePerRequest();
builder.Register<IAuthenticationManager>(c => HttpContext.Current.GetOwinContext().Authentication).InstancePerBackgroundJob().InstancePerRequest();
builder.Register<IDataProtectionProvider>(c => app.GetDataProtectionProvider()).InstancePerBackgroundJob().InstancePerRequest();
builder.RegisterType<TicketDataFormat>().As<ISecureDataFormat<AuthenticationTicket>>();
builder.RegisterType<TicketSerializer>().As<IDataSerializer<AuthenticationTicket>>();
builder.Register(c => new DpapiDataProtectionProvider("Sellution360").Create("ASP.NET Identity")).As<IDataProtector>();
builder.RegisterType<CurrencyRatesJob>().AsSelf().InstancePerBackgroundJob();
// BUILD THE CONTAINER
var container = builder.Build();
Hangfire.GlobalConfiguration.Configuration.UseAutofacActivator(container);
JobActivator.Current = new AutofacJobActivator(container);
// REPLACE THE MVC DEPENDENCY RESOLVER WITH AUTOFAC
DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
// Set the dependency resolver for Web API.
var webApiResolver = new AutofacWebApiDependencyResolver(container);
GlobalConfiguration.Configuration.DependencyResolver = webApiResolver;
// Set the dependency resolver for MVC.
var mvcResolver = new AutofacDependencyResolver(container);
DependencyResolver.SetResolver(mvcResolver);
// Register the Autofac middleware FIRST, then the Autofac MVC middleware.
app.UseAutofacMiddleware(container);
app.UseAutofacMvc().UseCors(CorsOptions.AllowAll);
app.UseAutofacWebApi(GlobalConfiguration.Configuration).UseCors(CorsOptions.AllowAll); ;
IocManager.Instance.IocContainer = container;
ConfigureAuth(app);
// Any connection or hub wire up and configuration should go here
app.MapSignalR();
Hangfire.GlobalConfiguration.Configuration.UseSqlServerStorage("DefaultConnection");
app.UseHangfireDashboard();
app.UseHangfireServer();
RecurringJob.AddOrUpdate<CurrencyRatesJob>(j => j.Execute(), Cron.Minutely);
}
public class CurrencyRatesJob
{
private readonly ILogger _logger;
private readonly IBusinessTypeService _businessTypeService;
public CurrencyRatesJob(ILogger logger, IBusinessTypeService businessTypeService)
{
_logger = logger;
_businessTypeService = businessTypeService;
}
public void Execute()
{
var types = _businessTypeService.GetBusinessTypes();
_logger.Log("waqar");
}
}
InstancePerBackgroundJob
使用BackgroundJobScope
标记创建每件苹果电脑的使用寿命
范围。但是每个请求
实例在另一个生命周期内通过请求
标记解析。因此,当您尝试在BackgroundJobScope
生命周期中解析每个请求的对象时,会出现错误。它说,您只能在请求
生存期内解析我,而不能在根目录或其他目录下解析我。因此,您应该在每个生命周期范围内使用,而不是在每个请求中使用
因此,这些每个生命周期范围的注册对象将获得父生命周期范围。如果是单例的话,它们将是根。如果他们的父生命周期范围是请求,他们将使用此请求范围。这与InstancePerBackgroundJob
相同,他们将生活在BackgroundJobScope
生活时间范围内
如果背景对象使用请求生存期范围,那么它有另一个生存期范围,这是很好的。当请求完成时,您的对象可以被处置。而且,如果它们在根范围内,它们将永远不会处理。这个问题没有得到回答,所以您认为使用这种方法有什么不好的做法InstancePerBackgroundJob
我没有检查所有代码,但正如我看到的那样,InstancePerBackgroundJob
使用BackgroundJobScope
创建InstancePerMachingfetimeScope
,并管理该范围内的对象。这很好,因为请求生存时间范围可以在后台工作仍在进行时完成。我将编辑我的答案。