让我们从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. datatables ajax异步分页

    $('#sample_1').dataTable({ "sAjaxSource": "../table/data", // "bProcessing& ...

  2. 用python编写简单爬虫

    需求:抓取百度百科python词条相关词条网页的标题和简介,并将数据输出在一个html表格中 入口页:python的百度词条页 https://baike.baidu.com/item/Python/ ...

  3. QQ市场总监分享:黏住90后的独门攻略

    转自:http://www.gameres.com/476003.html 90后的关键词 1. 品质生活 90后是怎么样的一群人?他们注重生活的品质. 他们比我们更爱享受,或者说他们不像我们一样认为 ...

  4. Spring Cloud与分布式系统

    本文不是讲解如何使用spring Cloud的教程,而是探讨Spring Cloud是什么,以及它诞生的背景和意义. 背景 2008年以后,国内互联网行业飞速发展,我们对软件系统的需求已经不再是过去” ...

  5. Java面向对象-对象的多态性

    Java面向对象-对象的多态性 Java中的多态性表现: 1,方法的重载和重写(覆盖): 2,可以用父类的引用指向子类的具体实现,而且可以随时更换为其他子类的具体实现: 我们先搞个父类Animal: ...

  6. leetcode343

    public class Solution { public int IntegerBreak(int n) { ) { ; } ) { ; } var max = int.MinValue; ; i ...

  7. C#向pdf 添加水印

    调用直接这样用: //PDFHelper.AddImageWatermarkPDF(path, "D://my.pdf", Server.MapPath("/HtmlTo ...

  8. 老外畅想C# 5.0这个可以有

    C# 5.0 - not quite there yet! 老外大胆的YY了一下,感觉挺有意思转发过来. 回顾C#发展的历史,C#1.0模仿了Java,并保留了C/C++的一些特性如struct,新学 ...

  9. AJAX省市县三级联动的实现

    省市县数据 本例子中省市县数据保存在MySQL数据库中,部分数据截图如下: 从数据库中读取数据 导入需要的jar包 连接池配置文件 <c3p0-config> <!-- 默认配置,如 ...

  10. 【bzoj2818】Gcd

    2818: Gcd Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 4344  Solved: 1912[Submit][Status][Discuss ...