C# WebAPI 中的 DependencyResolver.SetResolver 和 HttpConfiguration.DependencyResolver 有什么区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15494920/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 16:59:28  来源:igfitidea点击:

What is the difference between DependencyResolver.SetResolver and HttpConfiguration.DependencyResolver in WebAPI

c#asp.net-mvcasp.net-web-apiautofac

提问by Jevgenij Nekrasov

I have existing project, which uses AutoFac as IoC.

我有现有的项目,它使用 AutoFac 作为 IoC。

In the registration code i have these lines:

在注册码中,我有以下几行:

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

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

所以我的问题是DependencyResolver.SetResolver和之间有什么区别HttpConfiguration.DependecyResolver?为什么我要同时分配它们?

采纳答案by Steven

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。Microsoft 似乎建议这样做,因为用于 Web API 的 Visual Studio 模板自动将项目与 MVC 混合,但这是一个坏主意。

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 是完全不同的。MVC 是一种旨在优化最终用户体验的 UI 技术。Web API 是一种 Web 服务技术,旨在优化(客户端)开发人员的体验。

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.

MVC 和 Web API 不共享任何特定于演示文稿的代码。将它们混合在同一个项目中只会使该项目变得更大更复杂。

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

但也许更重要的是,这两种应用程序类型都有自己的 DI 配置需求。他们有自己的Composition Root,将其混合到单个 DI 配置(单个 DI 容器)中可以使您的配置变得异常困难。

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

最后,在同一个项目中将 Web API 与 MVC 混合会导致痛苦的模糊命名冲突,因为 Web API 程序集包含许多与 MVC 对应物共享完全相同名称的类(但实现略有不同)。

回答by Mark Jones

DependencyResolver.SetResolveris an MVC construct and is required to support IOC using MVC.

DependencyResolver.SetResolver是 MVC 结构,需要使用 MVC 支持 IOC。

The GlobalConfiguration.Configuration.DependencyResolveris WebApi specific.

GlobalConfiguration.Configuration.DependencyResolver是特定于 WebApi 的。

You you will only need both if you want to support both MVC and WebApi within the same project.

如果您想在同一个项目中同时支持 MVC 和 WebApi,则您只需要两者。

It is may also be worth pointing out that you do not normally need to explicitly set the DependencyResolver.SetResolveras the Ninject Mvc3 has a bootstrap for this... see here

可能还值得指出的是,您通常不需要显式设置,DependencyResolver.SetResolver因为 Ninject Mvc3 有一个引导程序...请参阅此处

回答by cuongle

I guess you mixed up between MVC and Web API:

我猜你混淆了 MVC 和 Web API:

DependencyResolver.SetResolver

is for MVCand belongs to assembly System.Web.Mvc. Otherwise:

用于MVC和属于组件 System.Web.Mvc。除此以外:

Configuration.DependencyResolver

for Web APi, it belongs to assembly System.Web.Http.

对于 Web API,它属于 assembly System.Web.Http

So, in your project, it uses both MVC and Web Api, that is why you see two lines to configure IoC for each

因此,在您的项目中,它同时使用 MVC 和 Web Api,这就是为什么您会看到两行来为每个配置 IoC