Autofac 与 Owin

Autofac with Owin

我对 Autofac 有疑问。文档明确指出,在使用 Web API 2 和 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

但无论我做什么,我都无法让 Autofac 使用:

config.DependencyResolver = new AutofacWebApiDependencyResolver(container);

而不是:

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

当我使用后者时,它起作用了。 有谁知道为什么?

好的,我明白了。我会 post 在这里回答,以防其他人遇到这个问题。 如文档所述,您不能在任何地方使用 GlobalConfiguration.... 所以我进行了搜索,然后在其他地方找到了它。 我有这个:

GlobalConfiguration.Configure(WebApiConfig.Register); 

应该是:

WebApiConfig.Register(config); 

当我解决这个问题时,我能够使用正确的

config.DependencyResolver = new AutofacWebApiDependencyResolver(container);