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 ...
随机推荐
- paip.提升性能--多核编程中的java .net php c++最佳实践 v2.0 cah
paip.提升性能--多核编程中的java .net php c++最佳实践 v2.0 cah 作者Attilax 艾龙, EMAIL:1466519819@qq.com 来源:attilax ...
- js正则匹配中文
alert(/[\u4e00-\u9fa5]{4}/.test("司徒正美"))//true alert(/[\u4e00-\u9fa5]{4}/.test("司正美&q ...
- JVM ,Java paper
http://files.cnblogs.com/files/WCFGROUP/IntroductiontoCompilerConstructioninaJavaWorld.pdf A Fast Wr ...
- gulp的使用以及Gulp新手入门教程
Gulp新手入门教程 原文 http://w3ctrain.com/2015/12/22/gulp-for-beginners/ Gulp 是一个自动化工具,前端开发者可以使用它来处理常见任务: 搭 ...
- 自己写bootloader(一)
bootloader功能目标:启动内核1.从Flash上把内核读入内存 1>读Flash: a.NORFLASH:读内存 b.NANDFLASH: 2>初始化内存(SDRA ...
- C陷阱与缺陷的学习笔记
1用单引号括起的字符实际代表一个整数,整数值对应于该字符在编译器中采用的字符集对应的序列值:双引号括起的字符串,代表的是一个指向无名数组起始字符的指针,该数组被双引号之间的字符和‘\0’初始化.对于' ...
- 在centos中安装mangodb
1.下载完安装包,并解压 tgz(以下演示的是 64 位 Linux上的安装) curl -O https://fastdl.mongodb.org/linux/mongodb-linux-x86_6 ...
- 利用jdbc连接池(利用jdni)
简介 前段时间用jdbc连接池,在这里记录下 1.利用jdni配置数据源 在Web项目的META-INF文件夹中新建context.xml文件,内容为: <?xml version=" ...
- sql经典语句收集
1随机取出10条数据 select top 10 * from MA_LOT order by newid() 2.随机选择记录select newid() 3.列出数据库里所有的表名select n ...
- 各种hash函数
public class HashAlgorithms { /** * 加法hash * @param key 字符串 * @param prime 一个质数 * @return hash结果 */ ...