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的更多相关文章
- autofac 在webapi中拿到当前request的scope
https://stackoverflow.com/questions/31321386/autofac-web-api-get-current-scope Unless you are usin ...
- Ioc:autofac lifetime scope.
During application execution, you’ll need to make use of the components you registered. You do this ...
- Autofac.Integration.Mvc.Owin分析
using System; using System.ComponentModel; using System.Diagnostics.CodeAnalysis; using System.Secur ...
- C# Ioc 接口注册实例以及注入MVC Controller
当弄一个小程序时,就忽略了使用Ioc这种手段,作为一个帅气程序员,代码规范,你懂的~,废话不多说,快速搭建一个Ioc接口实例以及直接注入到 MVC Controller 构造函数中如下: MVC in ...
- IoC之AutoFac(三)——生命周期
阅读目录 一.Autofac中的生命周期相关概念 二.创建一个新的生命周期范围 三.实例周期范围 3.1 每个依赖一个实例(InstancePerDependency) 3.2 单个实例(Sin ...
- ASP.NET MVC IOC依赖注入之Autofac系列(二)- WebForm当中应用
上一章主要介绍了Autofac在MVC当中的具体应用,本章将继续简单的介绍下Autofac在普通的WebForm当中的使用. PS:目前本人还不知道WebForm页面的构造函数要如何注入,以下在Web ...
- An Autofac Lifetime Primer
Or, “Avoiding Memory Leaks in Managed Composition” Understanding lifetime can be pretty tough when y ...
- Instances cannot be resolved and nested lifetimes cannot be created from this LifetimeScope as it has already been disposed.
2019-07-24 11:09:15.231+08:00 LISA.Common.Utilities.LogUtil - System.ObjectDisposedException: Instan ...
- 对request.getSession(false)的理解(附程序员常疏忽的一个漏洞)--转
出处:http://blog.csdn.net/xxd851116/archive/2009/06/25/4296866.aspx [前面的话] 在网上经常看到有人对request.getSessio ...
随机推荐
- XML基础(一)
一.简单介绍 1.概念 可扩展标记语言(EXtensible Markup Language),标准通用标记语言的子集,是一种用于标记电子文件使其具有结构性的标记语言.非常类似 HTML. ...
- php DES加密或者解密
function pkcs5_pad ($text, $blocksize) { //加密时的字节填充,保持和java 一致 $pad = $blocksize - (strlen($text) % ...
- jquery.dataTables的用法
写页面前端时,使用表格的插件可以快速漂亮的排版.本例子中使用jquery.dataTables来处理table.直接来点干货 html代码如下 <table cellpadding=" ...
- Java中的static关键字的用法
1.静态方法 static:通常在一个类中定义一个方法为static,那就是说,无需本类的对象即可调用此方法 声明为static的方法有以下几条限制: (1)它们仅能调用其他的static方法. (2 ...
- Linux下hosts、host.conf、resolv.conf的区别
/etc/resolv.conf 该文件是DNS域名解析的配置文件,它的格式很简单,每行以一个关键字开头,后接配置参数.resolv.conf的关键字主要有四个,分别是:nameserver #定 ...
- 如何学习Linux
为啥我们要学习Linux 技术的价值不在于这个技术有多么高超,而在于技术本身给我们带来什么价值,所以很多时候我们学习一个技术,不能盲目学,是为了使用这个技术,知道这个技术的使用场景,知道这个技术带来的 ...
- ASP.NET MVC 入门9、Action Filter 与 内置的Filter实现(介绍)
原帖地址:http://www.cnblogs.com/QLeelulu/archive/2008/10/09/1307660.html 有时候你想在调用action方法之前或者action方法之后处 ...
- android framework-安装samba
用于在windows下使用souceinsight访问linux中的android源代码. □ apt-get install samba samba-common #安装samba ...
- 428. Pow(x, n)【medium】
Implement pow(x, n). Notice You don't need to care about the precision of your answer, it's acceptab ...
- 每日英语:Some Chinese Students Stay Home to Get Ahead
Li Shan's oldest son was the perfect candidate to join the throngs of Chinese students studying abro ...