The request lifetime scope cannot be created because the HttpContext is not available
Posted 只为成功找方向,不为失败找借口
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了The request lifetime scope cannot be created because the HttpContext is not available相关的知识,希望对你有一定的参考价值。
项目中应用了Autofac,在Global轮询处理Job的时候,需要获取现有得Service,而这些Service已经通过Autofac进行了配置,所以理所应当的用下面的代码去获取了。
DependencyResolver.Current.GetService<ClinicalCaseService>();
结果出现问题了,直接抛出错误“The request lifetime scope cannot be created because the HttpContext is not available”。
结果疯狂的stackoverflow(地址:https://stackoverflow.com/questions/21804857/autofac-the-request-lifetime-scope-cannot-be-created-because-the-httpcontext-i),查找解决方案。
因为在AutofacDependencyResolver.Current内部GetService的时候需要HttpContext,所以会直接抛出错误。
解决方案:
1,autofac
var resolver = new AutofacWebApiDependencyResolver(container);
GlobalConfiguration.Configuration.DependencyResolver = resolver;
2,获取Service
ClinicalCaseService = GlobalConfiguration.Configuration.DependencyResolver.GetService(typeof (ClinicalCaseService)) as ClinicalCaseService;
以上是关于The request lifetime scope cannot be created because the HttpContext is not available的主要内容,如果未能解决你的问题,请参考以下文章
ReactiveSwift源码解析 Lifetime代码实现
The process of container handles the servlet request
Laravel5为啥设置session的lifetime时间就是没有用的
Kafka - which is larger than the maximum request size you have configured with the max.request