Autofac with Owin

强颜欢笑 提交于 2019-12-02 16:18:26

问题


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:

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.

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

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

config.DependencyResolver = new AutofacWebApiDependencyResolver(container);

instead of:

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

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


回答1:


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); 

which should have been:

WebApiConfig.Register(config); 

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

config.DependencyResolver = new AutofacWebApiDependencyResolver(container);


来源:https://stackoverflow.com/questions/44208004/autofac-with-owin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!