本文介绍了欧文与Autofac的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到 Autofac 的问题.该文档明确指出,在使用Web API 2和OWIN时,您绝对不能在任何地方使用GlobalConfiguration.Configuration:

I have an issue with Autofac. The documentation clearly states that when using Web API 2 and OWIN you must not use GlobalConfiguration.Configuration anywhere:

,可在此处(页面底部)找到: http ://autofac.readthedocs.io/en/latest/integration/webapi.html

which can be found here (at the bottom of the page): http://autofac.readthedocs.io/en/latest/integration/webapi.html

但是无论我做什么,我都无法使用 Autofac 来工作:

But no matter what I do, I can not get Autofac to work using:

config.DependencyResolver = new AutofacWebApiDependencyResolver(container);

代替:

GlobalConfiguration.Configuration.DependencyResolver = new AutofacWebApiDependencyResolver(container);

当我使用后者时,它可以工作.有人知道为什么吗?

When I use the latter, it works.Does anyone know why?

推荐答案

好,我知道了.如果有人遇到此问题,我将在此处发布答案.如文档所述,您不能在任何地方使用GlobalConfiguration.所以我进行了搜索,然后在其他地方找到了它.我有这个:

Ok, I figured this out. I will post the answer here incase anyone else has this issue.As the documentation stated, you can't use GlobalConfiguration anywhere....so I did a search and I found it somewhere else.I had this:

GlobalConfiguration.Configure(WebApiConfig.Register); 

应该是:

WebApiConfig.Register(config); 

修复该问题后,我便可以使用正确的

When I fixed that, I was able to use the proper

config.DependencyResolver = new AutofacWebApiDependencyResolver(container);

这篇关于欧文与Autofac的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-15 17:32