分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 代码编程

The request lifetime scope cannot be created because the HttpContext is not available

发布时间:2023-09-06 01:17责任编辑:董明明关键词:暂无标签

项目中应用了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

原文地址:http://www.cnblogs.com/icebutterfly/p/7660270.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved