让我们从Web API的集成点开始,它们是IDependencyResolverIDependencyScope接口。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的更多相关文章

  1. 对Castle Windsor的Resolve方法的解析时new对象的探讨

    依赖注入框架Castle Windsor从容器里解析一个实例时(也就是调用Resolve方法),是通过调用待解析对象的构造函数new一个对象并返回,那么问题是:它是调用哪个构造函数呢? 无参的构造函数 ...

  2. 避免Castle Windsor引起的内存泄露

    原文地址: http://nexussharp.wordpress.com/2012/04/21/castle-windsor-avoid-memory-leaks-by-learning-the-u ...

  3. Castle Windsor常用介绍以及其在ABP项目的应用介绍

    最近在研究ABP项目,有关ABP的介绍请看阳光铭睿 博客,ABP的DI和AOP框架用的是Castle Windsor下面就对Castle Windsor项目常用方法介绍和关于ABP的使用总结 1.下载 ...

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

  5. 说说ABP项目中的AutoMapper,Castle Windsor(痛并快乐着)

    这篇博客要说的东西跟ABP,AutoMapper和Castle Windsor都有关系,而且也是我在项目中遇到的问题,最终解决了,现在的感受就是“痛并快乐着”. 首先,这篇博客不是讲什么新的知识点,而 ...

  6. 小白初学Ioc、DI、Castle Windsor依赖注入,大神勿入(不适)

    过了几天,我又来了.上一篇中有博友提到要分享下属于我们abp初学者的历程,今天抽出点时间写写吧.起初,我是直接去看阳光铭睿的博客,看了一遍下来,感觉好多东西没接触过,接着我又去下了github 里面下 ...

  7. [Castle Windsor]学习依赖注入

    初次尝试使用Castle Windsor实现依赖注入DI,或者叫做控制反转IOC. 参考: https://github.com/castleproject/Windsor/blob/master/d ...

  8. Castle Windsor 使MVC Controller能够使用依赖注入

    以在MVC中使用Castle Windsor为例 1.第一步要想使我们的Controller能够使用依赖注入容器,先定义个WindsorControllerFactory类, using System ...

  9. IoC - Castle Windsor 2.1

    找过一些Windsor教程的文章,博客园上TerryLee有写了不少,以及codeproject等也有一些例子,但都讲的不太明了.今天看到Alex Henderson写的一个系列,非常简单明了.下面是 ...

  10. 在ABP项目的应用Castle Windsor

    Castle Windsor常用介绍以及其在ABP项目的应用介绍 最近在研究ABP项目,有关ABP的介绍请看阳光铭睿 博客,ABP的DI和AOP框架用的是Castle Windsor下面就对Castl ...

随机推荐

  1. ffmpeg超详细综合教程——摄像头直播

    本文的示例将实现:读取PC摄像头视频数据并以RTMP协议发送为直播流.示例包含了1.ffmpeg的libavdevice的使用2.视频解码.编码.推流的基本流程具有较强的综合性.要使用libavdev ...

  2. 分布式缓存系统 Memcached slab和item的主要操作

    上节在分析slab内存管理机制时分析Memcached整个Item存储系统的初始化过程slabs_init()函数:分配slabclass数组空间,到最后将各slab划分为各种级别大小的空闲item并 ...

  3. Velodyne 线性激光雷达数据合成

    坐标系旋转 如果想用字母表示角度,有两个方法: 1.  用三角函数sind(θ4).cosd(θ4).tand(θ4).atand(θ4)进行表示,注意:θ4在输入时是角度,只是没有度数特有的符号(° ...

  4. 杂项-公司:摩根大通百科-un

    ylbtech-杂项-公司:摩根大通百科 摩根大通集团(JPMorgan Chase & Co,NYSE:JPM:),2000年12月由J.P.摩根公司和大通-曼哈顿公司合并而成,是美国主要的 ...

  5. PHP中GD库的使用

    1.基本步骤 <?php /** * Created by PhpStorm. * User: jiqing * Date: 18-4-9 * Time: 上午9:34 * 熟悉步骤 */ // ...

  6. 如果axios请求失败,如何获取后端接口返回的状态码及错误信息

    这两天在工作中遇到一个问题,一个请求返回400错误,我需要向用户展示后端返回的错误信息,但是用普通的catch方法只能获取到浏览器返回的400错误提示,不能获取到后端返回的,后经查阅得出下面方法: a ...

  7. JavaEE笔记——BaseDao的使用

    在Hibernate框架中使用BaseDao主要的作用是减少冗余代码,在对Dao的操作中CRUD可以说是最普通最常见的操作了,基本上面对不同的数据表都会有类似的CRUD操作,BaseDao的思想就是把 ...

  8. UseSubmitBehavior="false" 防止页面重复提交bug

    OnClientClick="this.disabled=true;" UseSubmitBehavior="false" 注: 1.当设置UseSubmitB ...

  9. Metasploit自动化脚本Ezsploit

    打开文件夹并赋予权限 ┌─[root@sch01ar]─[~] └──╼ #cd /sch01ar/ezsploit/ && ls ezsploit.sh README.md ┌─[r ...

  10. python's ninth day for me

    函数 函数的定义与调用: #def  关键字  定义一个函数. # my_len  函数名, 函数名的书写规则与变量的命名一致. # def  与函数名中间一个空格. # 函数名() :  加上冒号. ...