Castle Windsor
让我们从Web API的集成点开始,它们是IDependencyResolver和IDependencyScope接口。IDependencyResolver和其他接口的名称可能与MVC中的接口相同,但不要混淆,因为它们位于不同的名称空间中,并且与MVC中使用的接口不同。

那么这个IDependencyResolver是什么?基本上它表示依赖注入容器,用于解析请求范围之外的所有内容。为了简化,它只是意味着使用此解析WebApi的所有接口。那么它在代码中看起来如何。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Http.Dependencies;
using Castle.MicroKernel; internal class DependencyResolver : System.Web.Http.Dependencies.IDependencyResolver
{
private readonly IKernel kernel; public DependencyResolver(IKernel kernel)
{
this.kernel = kernel;
} public IDependencyScope BeginScope()
{
return new DependencyScope(kernel);
} public object GetService(Type type)
{
return kernel.HasComponent(type) ? kernel.Resolve(type) : null;
} public IEnumerable<object> GetServices(Type type)
{
return kernel.ResolveAll(type).Cast<object>();
} public void Dispose()
{
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Http.Dependencies;
using Castle.MicroKernel;
using Castle.MicroKernel.Lifestyle; public class DependencyScope : IDependencyScope
{
private readonly IKernel kernel; private readonly IDisposable disposable; public DependencyScope(IKernel kernel)
{
this.kernel = kernel;
disposable = kernel.BeginScope();
} public object GetService(Type type)
{
return kernel.HasComponent(type) ? kernel.Resolve(type) : null;
} public IEnumerable<object> GetServices(Type type)
{
return kernel.ResolveAll(type).Cast<object>();
} public void Dispose()
{
disposable.Dispose();
}
}

using System.Web.Http;
using Castle.MicroKernel.Registration;
using Castle.MicroKernel.SubSystems.Configuration;
using Castle.Windsor; public class WebApiInstaller : IWindsorInstaller
{
public void Install(IWindsorContainer container, IConfigurationStore store)
{
container.Register(
Classes
.FromThisAssembly()
.BasedOn<ApiController>()
.LifestyleScoped()
);
}
}
global
var container = new WindsorContainer();
container.Install(FromAssembly.This());
GlobalConfiguration.Configuration.DependencyResolver = new CastleWindsor.DependencyResolver(container.Kernel);
原文http://www.macaalay.com/2014/08/27/using-castle-windsor-with-web-api-apicontroller/
Castle Windsor的更多相关文章
- 对Castle Windsor的Resolve方法的解析时new对象的探讨
依赖注入框架Castle Windsor从容器里解析一个实例时(也就是调用Resolve方法),是通过调用待解析对象的构造函数new一个对象并返回,那么问题是:它是调用哪个构造函数呢? 无参的构造函数 ...
- 避免Castle Windsor引起的内存泄露
原文地址: http://nexussharp.wordpress.com/2012/04/21/castle-windsor-avoid-memory-leaks-by-learning-the-u ...
- Castle Windsor常用介绍以及其在ABP项目的应用介绍
最近在研究ABP项目,有关ABP的介绍请看阳光铭睿 博客,ABP的DI和AOP框架用的是Castle Windsor下面就对Castle Windsor项目常用方法介绍和关于ABP的使用总结 1.下载 ...
- Aspect Oriented Programming using Interceptors within Castle Windsor and ABP Framework AOP
http://www.codeproject.com/Articles/1080517/Aspect-Oriented-Programming-using-Interceptors-wit Downl ...
- 说说ABP项目中的AutoMapper,Castle Windsor(痛并快乐着)
这篇博客要说的东西跟ABP,AutoMapper和Castle Windsor都有关系,而且也是我在项目中遇到的问题,最终解决了,现在的感受就是“痛并快乐着”. 首先,这篇博客不是讲什么新的知识点,而 ...
- 小白初学Ioc、DI、Castle Windsor依赖注入,大神勿入(不适)
过了几天,我又来了.上一篇中有博友提到要分享下属于我们abp初学者的历程,今天抽出点时间写写吧.起初,我是直接去看阳光铭睿的博客,看了一遍下来,感觉好多东西没接触过,接着我又去下了github 里面下 ...
- [Castle Windsor]学习依赖注入
初次尝试使用Castle Windsor实现依赖注入DI,或者叫做控制反转IOC. 参考: https://github.com/castleproject/Windsor/blob/master/d ...
- Castle Windsor 使MVC Controller能够使用依赖注入
以在MVC中使用Castle Windsor为例 1.第一步要想使我们的Controller能够使用依赖注入容器,先定义个WindsorControllerFactory类, using System ...
- IoC - Castle Windsor 2.1
找过一些Windsor教程的文章,博客园上TerryLee有写了不少,以及codeproject等也有一些例子,但都讲的不太明了.今天看到Alex Henderson写的一个系列,非常简单明了.下面是 ...
- 在ABP项目的应用Castle Windsor
Castle Windsor常用介绍以及其在ABP项目的应用介绍 最近在研究ABP项目,有关ABP的介绍请看阳光铭睿 博客,ABP的DI和AOP框架用的是Castle Windsor下面就对Castl ...
随机推荐
- 有趣的java小项目------猜拳游戏
package com.aaa; //总结:猜拳游戏主要掌握3个方面:1.人出的动作是从键盘输入的(System.in)2.电脑是随机出的(Random随机数)3.双方都要出(条件判断) import ...
- jdk8的扩展注解
对于注解(也被称做元数据),Java 8 主要有两点改进:类型注解和重复注解. 1.类型注解 1)Java 8 的类型注解扩展了注解使用的范围. 在java 8之前,注解只能是在声明的地方所使用,ja ...
- [Kingdom Rush]团队分享:如何做塔防手游
转自:http://www.gamelook.com.cn/2015/03/207324 GameLook报道/2014年11月,乌拉圭开发商Ironhide Studios发布的<Kingdo ...
- ansible安装配置zabbix客户端
安装软件 ansible host -m apt -a "name=zabbix-agent state=present" ansible host -m shell -a ...
- maven项目依赖包问题
问题 maven传递依赖 解决方案 前段时间,开发中遇到一个关于maven依赖包的问题:由于业务需要,支付网关对账代码中的slf4j-api包需要更新,原包为1.5.8版本,需要更新到1.6.4版 ...
- Vue 简单的总结一
let 变量 1. 局部作用域 2. 不会存在变量提升 3. 变量不能重复声明 const 变量 1. 局部作用域 2. 不会存在变量提升 3. 变量不能重复声明 4. 只能声明常量,不可变得量 th ...
- Hadoop Serialization -- hadoop序列化详解 (3)【ObjectWritable,集合Writable以及自定义的Writable】
前瞻:本文介绍ObjectWritable,集合Writable以及自定义的Writable TextPair 回顾: 前面了解到hadoop本身支持java的基本类型的序列化,并且提供相应的包装实现 ...
- spring mvc default-servlet mvc:resources mvc:default-servlet-handler区别
mvc:default-servlet-handler其实就是default-servlet 交由web容器自己处理 mvc:resources spring来处理 没有被映射的url web容器来处 ...
- oscache.properties文件配置
1.cache.memory是否使用内存缓存:值为:true或false.默认为true:如设置为false,那cache只能缓存到数据库或硬盘中. 2.cache.capacity缓存的最大数量.默 ...
- Location - BOM对象
Location 对象 Location 对象包含有关当前 URL 的信息. Location 对象是 Window 对象的一个部分,可通过 window.location 属性来访问. 例子 把用户 ...