Tag: dependency resolver

Autofac – 无法创建请求生命周期范围,因为HttpContext不可用 – 由于异步代码?

简短问题: 与此未解决的问题相同 长问题: 我刚刚从MVC 4 + Web Api解决方案中移植了一些代码,该解决方案使用Autofac进入我的新解决方案,该解决方案也使用Autofac但仅使用Web Api 2(没有MVC 5.1项目,只有web api)。 在我以前的解决方案中,我有MVC4和Web Api所以我有2个Bootstrapper.cs文件,每个文件一个。 我复制了新项目的Web Api引导程序。 现在我在新解决方案中有2个其他需要依赖项目的项目。 让我们假设我必须使用DependencyResolver.Current.GetService()尽管它是一个反模式。 起初,在我将MVC依赖关系解析器设置为同一容器之前,这是行不通的: GlobalConfiguration.Configuration.DependencyResolver = new AutofacWebApiDependencyResolver(container); //I had to pull in Autofac.Mvc and Mvc 5.1 integration but this line fixed it DependencyResolver.SetResolver(new AutofacDependencyResolver(container)); 奇怪的是,这样做只会修复其中一个项目! 这是情况: Solution.Web project Bootstrapper.cs that registers both dependency resolvers for web api and mvc. […]