欧文与Autofac [英] Autofac with Owin

查看:75
本文介绍了欧文与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:

OWIN集成中的一个常见错误是使用GlobalConfiguration.Configuration.在OWIN中,您可以从头开始创建配置.使用OWIN集成时,您不应在任何地方引用GlobalConfiguration.Configuration.

A common error in OWIN integration is use of the GlobalConfiguration.Configuration. In OWIN you create the configuration from scratch. You should not reference GlobalConfiguration.Configuration anywhere when using the OWIN integration.

,可在此处(页面底部)找到: 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆