【发布时间】:2015-12-02 20:02:55
【问题描述】:
我的项目结构是一样的:
https://github.com/MarlabsInc/webapi-angularjs-spa
我已按照以下说明进行操作:
http://docs.hangfire.io/en/latest/background-methods/using-ioc-containers.html
所以我创建了一个容器作业激活器。
在我的 Bootstrapper.cs 中
containerBuilder.RegisterType<DatabaseFactory>().As<IDatabaseFactory>().AsImplementedInterfaces().InstancePerApiRequest();
containerBuilder.RegisterType<UnitOfWork>().As<IUnitOfWork>().AsImplementedInterfaces().InstancePerApiRequest();
containerBuilder.RegisterApiControllers(System.Reflection.Assembly.GetExecutingAssembly());
IContainer container = containerBuilder.Build();
Hangfire.GlobalConfiguration.Configuration
.UseAutofacActivator(container);
JobActivator.Current = new AutofacJobActivator(container);
System.Web.Http.GlobalConfiguration.Configuration.DependencyResolver = new AutofacWebApiDependencyResolver(container);
我的启动类有一个方法:
GlobalConfiguration.Configuration
.UseSqlServerStorage("entitiesDB",
new SqlServerStorageOptions
{
PrepareSchemaIfNecessary = false,
InvisibilityTimeout = TimeSpan.FromMinutes(30)
});
app.UseHangfireDashboard();
app.UseHangfireServer();
在控制器中:我正在尝试将 2000 张发票的状态更新为“已批准” 所以方法很简单如下:
foreach(int id in invoiceIds)
{
BackgroundJob.Enqueue<IInvoiceService>(a => a.UpdateInvoice(id));
}
现在当我在 SQL 中查询时:
select * from HangFire.[State]
我在数据列中得到以下异常:
{"FailedAt":"2015-07-07T10:00:40.9454943Z","ExceptionType":"Autofac.Core.DependencyResolutionException","ExceptionMessage":"否 带有与“AutofacWebRequest”匹配的标记的范围从 请求实例的范围。这通常表明 正在请求注册为 per-HTTP 请求的组件 一个 SingleInstance() 组件(或类似的场景)。在 web 下 集成总是从 DependencyResolver.Current 或 ILifetimeScopeProvider.RequestLifetime, 从不从容器中 本身。","ExceptionDetails":"Autofac.Core.DependencyResolutionException: 没有标记匹配“AutofacWebRequest”的范围从 请求实例的范围。这通常表明 正在请求注册为 per-HTTP 请求的组件 一个 SingleInstance() 组件(或类似的场景)。在 web 下 集成总是从 DependencyResolver.Current 或 ILifetimeScopeProvider.RequestLifetime, 从不来自容器本身。\r\n at Autofac.Core.Lifetime.MatchingScopeLifetime.FindScope(ISharingLifetimeScope mostNestedVisibleScope)\r\n 在 Autofac.Core.Resolving.InstanceLookup..ctor(IComponentRegistration 注册,IResolveOperation 上下文,ISharingLifetimeScope mostNestedVisibleScope,IEnumerable
1 parameters)\r\n at Autofac.Core.Resolving.ResolveOperation.GetOrCreateInstance(ISharingLifetimeScope currentOperationScope, IComponentRegistration registration, IEnumerable1 参数)\r\n at Autofac.Core.Resolving.ResolveOperation.ResolveComponent(IComponentRegistration 注册,IEnumerable1 parameters)\r\n at Autofac.Core.Resolving.ResolveOperation.Execute(IComponentRegistration registration, IEnumerable1 参数)\r\n at Autofac.Core.Lifetime.LifetimeScope.ResolveComponent(IComponentRegistration 注册,IEnumerable1 parameters)\r\n at Autofac.Core.Container.ResolveComponent(IComponentRegistration registration, IEnumerable1 参数)\r\n at Autofac.ResolutionExtensions.TryResolveService(IComponentContext 上下文、服务服务、IEnumerable1 parameters, Object& instance)\r\n at Autofac.ResolutionExtensions.ResolveService(IComponentContext context, Service service, IEnumerable1 参数)\r\n at Autofac.ResolutionExtensions.Resolve(IComponentContext 上下文,类型 serviceType, IEnumerable`1 参数)\r\n at Autofac.ResolutionExtensions.Resolve(IComponentContext 上下文,类型 serviceType)\r\n 在 Hangfire.AutofacJobActivator.ActivateJob(Type jobType)\r\n 在 Hangfire.Common.Job.Activate(JobActivator 激活器)"}
有人可以帮我理解我做错了什么吗?
【问题讨论】:
-
谢谢杰瑞,很抱歉我无法编辑它,因为我是新手。
标签: asp.net-web-api2 autofac hangfire autofac-module hangfire.ninject