Autofac with Owin

问题描述:

我有一个问题Autofac。该文件明确指出,使用Web API 2和OWIN你的时候切不可使用GlobalConfiguration.Configuration任何地方:Autofac with Owin

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

可以在这里找到(在页面的底部):http://autofac.readthedocs.io/en/latest/integration/webapi.html

但无论我做什么,我不能得到Autofac使用工作:

config.DependencyResolver = new AutofacWebApiDependencyResolver(container); 

而不是:

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

当我使用后者,它的工作原理。 有谁知道为什么?

好的,我明白了这一点。我会在这里发布答案,因为任何人都有这个问题。 正如文件所述,你不能在任何地方使用GlobalConfiguration .... 所以我做了一个搜索,我发现它在其他地方。 我有这样的:

GlobalConfiguration.Configure(WebApiConfig.Register); 

这应该是:

WebApiConfig.Register(config); 

当我固定的,我能够使用正确的

config.DependencyResolver = new AutofacWebApiDependencyResolver(container);