- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
我有一个使用 CaSTLe Windsor 的 MVC4 站点,我想向其添加一些 WebAPI 调用,因此我开始在互联网上进行一些挖掘。
现在我不知道 IoC 的来龙去脉;我遵循了如何在我的项目上设置温莎城堡的教程,将 IUnitOfWorkFactory
和 IApplicationService
作为公共(public)属性注入(inject)到基本 Controller 中,并根据需要注入(inject)一些其他接口(interface)在 Controller 构造函数中。它运行顺畅,所以我从来不需要用它做更多的事情。
我在阅读有关 WebAPI 的所有内容时,都被告知使用 CaSTLe Windsor 时 DI 将无法很好地工作,并谈论 IDependencyResolver
和 IDependencyScope
的问题。有几种解决方法和实现方法可以解决这个问题,但我不清楚问题到底是什么。包含代码片段,但前提是您知道它们属于哪个类,以及它们是如何被调用的,不幸的是我不知道。此外,我在网上看到的所有示例都指的是一个独有的 WebAPI 项目,而不是明智地加入了几个 ApiController
的 MVC4 项目。我不知道这会如何影响,或者是否会影响任何东西。
为什么我在标准 Controller 上使用的功能不能在 API Controller 上使用?要让 WebAPI 调用和标准 Web 调用在同一个应用程序中工作,需要执行什么样的代码技巧?
最佳答案
假设您有类似于 Castle Windsor MVC tutorial 的 MVC 和 CaSTLe Windsor 设置,使用 Mark Seemann's post 添加 IoC 以使 Web API Controller 利用依赖注入(inject)非常简单(注意他解释了为什么不使用 IDependencyResolver)。
在 CaSTLe Windsor 教程中,您应该在 Global.asax.cs
中有类似的内容。
private static IWindsorContainer container;
protected void Application_Start()
{
//... MVC / Web API routing etc.
BootStrapper bs = new BootStrapper();
container = bs.ConfigureCastleWindsorMVC();
}
BootStrapper.ConfigureCaSTLeWindsorMVC()
片段
IWindsorContainer container = new WindsorContainer()
.Install(
new LoggerInstaller()
//...,
, new ControllersInstaller()
);
var controllerFactory = new WindsorControllerFactory(container.Kernel);
ControllerBuilder.Current.SetControllerFactory(controllerFactory);
return container;
根据 Mark Seemann 的帖子,您需要进入 Web API 的 entry point (Composition Root)通过 IHttpControllerActivator
。这是您需要的他的适配器实现。
public class WindsorCompositionRoot : IHttpControllerActivator
{
private readonly IWindsorContainer container;
public WindsorCompositionRoot(IWindsorContainer container)
{
this.container = container;
}
public IHttpController Create(HttpRequestMessage request,
HttpControllerDescriptor controllerDescriptor, Type controllerType)
{
var controller =
(IHttpController)this.container.Resolve(controllerType);
request.RegisterForDispose(
new Release(() => this.container.Release(controller)));
return controller;
}
private class Release : IDisposable
{
private readonly Action release;
public Release(Action release) { this.release = release; }
public void Dispose()
{
this.release();
}
}
}
有了 IHttpControllerActivator 适配器和 MVC CaSTLe Windsor 容器实现,您只需在 Global.asax.cs
(或者如果您使用 BootStrapper)中配置它。它必须在 MVC 初始化之后,因为 MVC 初始化具有所有安装程序。
private static IWindsorContainer container;
protected void Application_Start()
{
// MVC / Web API routing etc.
BootStrapper bs = new BootStrapper();
container = bs.ConfigureCastleWindsorMVC();
// Web API Castle Windsor ++ ADD THIS ++
GlobalConfiguration.Configuration.Services.Replace(
typeof(IHttpControllerActivator),
new WindsorCompositionRoot(container));
}
Web API Controller 可以像您的 MVC Controller 一样使用您注入(inject)的依赖项。
public class TestController : ApiController
{
private readonly ITestService TestService;
public TestController(ITestService testService)
{
this.TestService = testService;
}
// GET api/<controller>
public IEnumerable<string> Get()
{
return TestService.GetSomething();
//return new string[] { "value1", "value2" };
}
}
关于c# - 国际奥委会 : CaSTLe Windsor and WebAPI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16154566/
我需要一个正则表达式来匹配所有标点符号,例如标准的 [,!@#$%^&*()],但包括国际标记,例如倒置的西类牙语问号、中文句号等.我的 google-fu 快用完了。有人手头有这样一个与 Javas
跟进之前的 question我有关于水平数组和垂直数组的问题,我对它们各自的分隔符有疑问。 问题定义: 这里是比较两个数组的错误方法的示例: {=SUMPRODUCT(--({"Apple","Pea
我正在尝试对手机号码进行验证检查,有点类似于 gmail 的实现。 Gmail signup page link 但是各个国家/地区的电话号码差异很大,因此很难为此构建正则表达式。 我在这里查看了一些
这就是我们所拥有的: 数据库中的用户,包括 phone_number 字段。当用户创建他们的帐户时,他们必须输入他们的电话号码。我们可以要求特定的格式,我们会这样做: + [countrycode]
对于Google-Chrome扩展程序,我想在所有Google页面上加载内容脚本。做这个的最好方式是什么? 我在manifest.json中尝试了此操作,但它不起作用: "matches": ["ht
在 JavaScript 中使用数字格式化程序时,是否可以在值之前使用欧元符号来格式化值? this.formatter = new Intl.NumberFormat('nl-be', { st
我们有一个 iOS 应用程序,在美国拥有数百万用户,需要国际化并在 11 个不同的国家/地区(许多国家/地区使用不同的语言)提供。已制定出适用于特定国家/地区以及不同货币等的公开隐藏功能的所有技术细节
如何在使用 react-intl 时使用 来控制断字。我有一条像 Rs 这样的消息。 100,00,000 但它正在打破卢比。无论如何可以将消息写为 Rs. 100,00,000当我尝试这样做时,
我正在研究具有端点的 swagger API 规范: /authorizations 我也想为这个端点定义一个替代拼写(授权)。这可能吗?或者我是否需要为每个拼写定义一个单独的路由? /authori
我正在研究具有端点的 swagger API 规范: /authorizations 我也想为这个端点定义一个替代拼写(授权)。这可能吗?或者我是否需要为每个拼写定义一个单独的路由? /authori
我正在尝试安装 CakePHP 3-0-0 版本并且我已经从 here 下载了源代码.但它显示启用 intl 扩展 的错误。因为我已经在 php.ini 文件中将 ;extension=php_int
#include #include #include int main() { const wchar_t *str = L"\u041F\u043E\u0440\u044F\u0434
Transliterator::listIDs()将列出 ID,但显然它不是完整列表。 在example from this page ,ID 看起来像: Any-Latin; NFD; [:Nons
任务是在不同的语言环境中显示订阅价格,例如 5$/month。 我可以用 Intl 管理价格部分很好,但坚持时间段翻译部分。 我可以使用自己的翻译字符串,但如果可能,我更愿意使用默认的 Intl 版本
任务是在不同的语言环境中显示订阅价格,例如 5$/month。 我可以用 Intl 管理价格部分很好,但坚持时间段翻译部分。 我可以使用自己的翻译字符串,但如果可能,我更愿意使用默认的 Intl 版本
我正在使用 React 制作一个多语言通用应用程序,但我很难找到处理区域设置数据的最佳方法。该应用程序将提供 16 种语言版本,并且翻译的消息量非常大,因此我无法将所有消息加载到一个大 json 中(
我是一名优秀的程序员,十分优秀!