是什么在的WebAPI DependencyResolver.SetResolver和HttpConfiguration.DependecyResolver之间的区别 [英] What is the difference between DependencyResolver.SetResolver and HttpConfiguration.DependecyResolver in WebAPI

查看:99
本文介绍了是什么在的WebAPI DependencyResolver.SetResolver和HttpConfiguration.DependecyResolver之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现有的项目,该项目采用AutoFac为国际奥委会。

I have existing project, which uses AutoFac as IoC.

在登记code,我有以下行:

In the registration code i have these lines:

var resolver = builder.Build();
DependencyResolver.SetResolver(new AutofacDependencyResolver(resolver));
config.DependencyResolver = new AutofacWebApiDependencyResolver(resolver);

所以我的问题是什么 DependencyResolver.SetResolver 之间的差异, HttpConfiguration.DependecyResolver ?我为什么要他们两个分配?

So my question is what is the difference between DependencyResolver.SetResolver and HttpConfiguration.DependecyResolver? Why should I assign both of them?

推荐答案

prevent在同一个项目中混合MVC和Web API。微软似乎在暗示这一点,因为对Web API的Visual Studio的模板,自动MVC混合项目,但是这是一个坏主意。

Prevent mixing MVC and Web API in the same project. Microsoft seems to suggest this, because the Visual Studio template for Web API mixes the project automatically with MVC, but this is a bad idea.

从建筑的角度来看,MVC和Web API是完全不同的。 MVC是一个用户界面技术,旨在优化最终用户体验。网页API是一个Web服务技术,该技术旨在优化的(客户端)的开发经验。

From an architectural point of view, MVC and Web API are completely different. MVC is a UI technology aimed to optimize end user experience. Web API is a web service technology that is aimed to optimize the experience for the (client) developer.

MVC和Web API不共享任何presentation具体code。在同一项目中混合它们只是使该项目更大,更复杂的。

MVC and Web API don't share any presentation specific code. Mixing them in the same project just makes that project bigger and more complicated.

但也许更重要的是,这两种类型的应用程序有自己的DI配置需求。他们有自己的成分根并混合了成一个单一的DI配置(一个DI容器)就可以了让您的配置非凡的努力。

But perhaps even more importantly, both application types have their own DI configuration needs. They have their own Composition Root and mixing that into a single DI configuration (a single DI container) can make your configuration extraordinary hard.

而在去年,在同一个项目,MVC混合的Web API导致痛苦的暧昧命名冲突,因为网络API组件包含了很多共享完全相同的名称为MVC同行(但略有不同的实现)班。

And last, mixing Web API with MVC in the same project leads to painful ambiguous naming conflicts, since Web API assemblies contains a lot of classes that share the exact same name as MVC counterparts (but with slightly different implementations).

这篇关于是什么在的WebAPI DependencyResolver.SetResolver和HttpConfiguration.DependecyResolver之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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