IoC:温莎城堡和 WebAPI [英] IoC: Castle Windsor and WebAPI

查看:21
本文介绍了IoC:温莎城堡和 WebAPI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用 Castle Windsor 的 MVC4 站点,我想向其中添加一些 WebAPI 调用,因此我开始在互联网上进行一些挖掘.

I have an MVC4 site using Castle Windsor that I want to add some WebAPI calls to, so I start digging around a little bit on the interwebs.

现在我不知道 IoC 的来龙去脉;我遵循了如何在我的项目中设置 Castle Windsor 的教程,将 IUnitOfWorkFactoryIApplicationService 作为公共属性注入到基本控制器中,并根据需要注入了一些其他接口在控制器构造函数中.它运行流畅,所以我从来不需要用它做更多的事情.

Now I don't know the ins and outs of IoC; I followed a tutorial for how to set up Castle Windsor on my project, injecting the IUnitOfWorkFactory and the IApplicationService as public properties in a base controller, and a few other interfaces as needed in the controller constructors. It works swimmingly, so I've never had to do more with it.

在我阅读 WebAPI 的任何地方,我都被告知使用 Castle Windsor 时 DI 不能很好地工作,谈论 IDependencyResolverIDependencyScope 的问题.有几种解决方法和实现来解决这个问题,但我不清楚问题究竟是什么.包含代码片段,但假设您知道它们属于哪个类,以及它们是如何调用的,不幸的是我不知道.此外,我在网上看到的所有示例都指的是一个专有的 WebAPI 项目,而不是一个明智地加入了几个 ApiController 的 MVC4 项目.我不知道这如何或是否会影响任何东西.

Everywhere that I'm reading up on WebAPI, I'm told DI will not work so well using Castle Windsor, talking about problems with the IDependencyResolver and IDependencyScope. There are several workarounds and implementations of how to get around this problem, but what is not clear to me is what exactly the problem is. Code snippets are included, but the assumption is you know what class they belong to, and how they are invoked, which unfortunately I do not. Additionally, all the examples I've seen online refer to an exclusive WebAPI project, and not an MVC4 project with a couple ApiControllers judiciously tossed in. I don't know how, or if, this affects anything.

为什么我的标准控制器不能用于 API 控制器?需要什么样的代码杂技才能让 WebAPI 调用和标准 Web 调用在同一个应用程序中工作?

Why won't what I have working with my standard controllers not work with an API controller? What kind of code acrobatics need to do to get WebAPI calls and standard web calls to work in the same application?

推荐答案

为了使用 Windsor 和 webapi 按照http://blog.ploeh.dk/2012/10/03/DependencyInjectioninASP.NETWebAPIwithCastleWindsor/(BEH:固定链接)

In order to use Windsor with webapi follow http://blog.ploeh.dk/2012/10/03/DependencyInjectioninASP.NETWebAPIwithCastleWindsor/ (BEH: fixed link)

另请阅读Castle Windsor/DelegatingHandler/IPrincipal 依赖注入 (DI)/ASP.NET Web API 中的控制反转 (IoC)

您可以在同一个应用程序中使用 webapi 和 MVC 控制器(我更喜欢将它们分开)但路由和控制器工厂是不同的,您必须设置两个不同的配置并处理路由重叠...对于 MVC &温莎你可以找到一个很棒的

You can use webapi and MVC controllers in the same app (I prefer to keep it separate) but routing and controllers factory are different and you have to set up two different configuration and handle routing overlaps... For MVC & Windsor you can find a great

这篇关于IoC:温莎城堡和 WebAPI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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