项目中应用了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的更多相关文章

  1. autofac 在webapi中拿到当前request的scope

    https://stackoverflow.com/questions/31321386/autofac-web-api-get-current-scope   Unless you are usin ...

  2. Ioc:autofac lifetime scope.

    During application execution, you’ll need to make use of the components you registered. You do this ...

  3. Autofac.Integration.Mvc.Owin分析

    using System; using System.ComponentModel; using System.Diagnostics.CodeAnalysis; using System.Secur ...

  4. C# Ioc 接口注册实例以及注入MVC Controller

    当弄一个小程序时,就忽略了使用Ioc这种手段,作为一个帅气程序员,代码规范,你懂的~,废话不多说,快速搭建一个Ioc接口实例以及直接注入到 MVC Controller 构造函数中如下: MVC in ...

  5. IoC之AutoFac(三)——生命周期

    阅读目录 一.Autofac中的生命周期相关概念 二.创建一个新的生命周期范围 三.实例周期范围 3.1   每个依赖一个实例(InstancePerDependency) 3.2  单个实例(Sin ...

  6. ASP.NET MVC IOC依赖注入之Autofac系列(二)- WebForm当中应用

    上一章主要介绍了Autofac在MVC当中的具体应用,本章将继续简单的介绍下Autofac在普通的WebForm当中的使用. PS:目前本人还不知道WebForm页面的构造函数要如何注入,以下在Web ...

  7. An Autofac Lifetime Primer

    Or, “Avoiding Memory Leaks in Managed Composition” Understanding lifetime can be pretty tough when y ...

  8. 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 ...

  9. 对request.getSession(false)的理解(附程序员常疏忽的一个漏洞)--转

    出处:http://blog.csdn.net/xxd851116/archive/2009/06/25/4296866.aspx [前面的话] 在网上经常看到有人对request.getSessio ...

随机推荐

  1. C语言中续行符“\”说明

    把一个预处理指示写成多行要用“\”续行,因为根据定义,一条预处理指示只能由一个逻辑代码行组成. 而把C代码写成多行则不必使用续行符,因为换行在C代码中只不过是一种空白字符,在做语法解析时所有空白字符都 ...

  2. django web 笔记

    安装 django  pip install django 创建虚拟环境  python -m venv  testenvironment 进入虚拟环境: testenvironment\Script ...

  3. JavaScript与DOM(上)

    本来像自己写一篇的...结果看到了Tom uncle的这篇..总结的确实很赞,其他文章也非常好推荐 转载自:http://www.cnblogs.com/TomXu/archive/2011/12/1 ...

  4. 3dmax坐标系与导出fbx的坐标系

    3dmax和opengl都是右手坐标系,但是3dmax是z轴向上,而opengl中是Y轴向上.如图: 所以在3dmax的fbx导出对话框中有“轴转化”一项,可以设置“Y向上”或者“Z向上”. 默认是“ ...

  5. Python之反射练习

    # 什么是反射?可以用字符串的方式去访问对象的属性 class Test(): _name = "sss" def fun(self): return "Hellowor ...

  6. 【Android】7.1 布局控件常用的公共属性

    分类:C#.Android.VS2015: 创建日期:2016-02-10 一.简介 Android应用程序中的布局控件都是容器控件,用于控制子元素的排列和放置方式.Android提供的布局控件有: ...

  7. Disk performance

    http://blogs.msdn.com/b/ntdebugging/archive/2014/12/09/disk-performance-internals.aspx http://blogs. ...

  8. js用new Object创建json数据

    var str = '';var json = new Object;var arr =new Array(); for(var i =0; i<4;i++){        var jsons ...

  9. Python IDLE快捷键【转载合集】

    转载自:http://www.douban.com/note/212321426/ 编辑状态时:Ctrl + [ .Ctrl + ] 缩进代码Alt+3 Alt+4 注释.取消注释代码行Alt+5 A ...

  10. 问题-XE10.2开发Datasnap时提示"provider not exported datasetprovider1"

    问题现象: 在用最新版本的XE10.2开发一个代有图片的数据操作时,出现“provider not exported datasetprovider1”. 问题原因: 提示这个信息,代表未找到data ...