项目中应用了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. python标准库介绍——16 shutil模块详解

    ``shutil`` 实用模块包含了一些用于复制文件和文件夹的函数. [Example 2-4 #eg-2-4] 中使用的 ``copy`` 函数使用和 Unix 下 ``cp`` 命令基本相同的方式 ...

  2. NodeJS写日志_Log4js使用详解

    今天和大家分享一下NodeJS中写日志的一个常用第三方包:Log4js. 跟随主流Blog特色,先简单介绍下Log4js的基本信息.介绍Log4js之前,需要先说一下Log4***,Log4***是由 ...

  3. Token:服务端身份验证的流行方案【转】

    01- 身份认证 服务端提供资源给客户端,但是某些资源是有条件的.所以服务端要能够识别请求者的身份,然后再判断所请求的资源是否可以给请求者. token是一种身份验证的机制,初始时用户提交账号数据给服 ...

  4. How to set JAVA environment variables in Linux or CentOS

    How to set JAVA environment variables JAVA_HOME and PATH in Linux After installing new java (jdk or ...

  5. android.content.res.TypedArray-深入理解android自定义属性(AttributeSet,TypedArray)

    属性 自定义属性,首先要定义出来属性,我们新建一个attrs.xml: <?xml version="1.0" encoding="utf-8"?> ...

  6. js 的登录验证

    $scope.startLogin = function () { console.log('loginTimeHelper:startLogin: ' + getCurrentTime()); ef ...

  7. 基于HTML5/CSS3图片网格动画特效

    现在HTML5技术可以让网页上的图片变得非常神奇,各种各样的HTML5图片动画特效让你眼花缭乱.今天要分享的这款HTML5图片网格动画特效就非常炫酷.图片缩略图按网格的布局一行行排列,你只需点击按钮即 ...

  8. 服务器响应慢的分析与解决(Linux服务器)

    一.分析思路 1.排除本机自身原因 2.服务器性能分析 3.项目本身分析(不详细说) 4.虚拟机分析 5.数据库分析 二.详细分析方法 1.排除本机自身原因 可以使用站长工具测试网站速度. 2.服务器 ...

  9. socket编程基础-字节序/IP/PORT转换/域名

    socket编程基础 网络IP操作函数 字符串的IP和32位的IP转换 #include <sys/socket.h> #inlcude <netinet/in.h> #inc ...

  10. CDH impala安装

    环境 CDH版本:5.12.1 添加impala parcel 1.菜单“主机”-->Parcel-->配置-->远程 Parcel 存储库 URL,点击添加按钮,添加一个URL,h ...