ASP.NET Web API - 使用 Castle Windsor 依賴注入

來源:互聯網
上載者:User

標籤:

範例程式碼

項目啟動時,建立依賴注入容器

定義一靜態容器 IWindsorContainer

 1 private static IWindsorContainer _container; 

在 Application_Start() 中,建立該容器

 1 _container = new WindsorContainer(); 

 

調用 Container Install 方法,向容器內註冊組件

 1 _container.Install(FromAssembly.This()); 

該語句會調用整個程式集中所有實現了 IWindsorInstaller 介面類中的Install方法以向容器的註冊組件。如下列代碼:

1 public void Install(IWindsorContainer container, IConfigurationStore store)2 {3     container.Register(4         Component.For<IDPlatypusRepository>().ImplementedBy<DPlatypusRepository>().LifestylePerWebRequest());5 }

特別列出向 Container 中註冊 Controler 的代碼:

1 public void Install(IWindsorContainer container, IConfigurationStore store)2 {3       container.Register(Classes.FromThisAssembly()4              .BasedOn<ApiController>().LifestylePerWebRequest());5 }

 

定義實現 IDependencyResolver 介面的 WindsorDependencyResolver 類,作為全域 HttpConfiguration 的 Resolver

var dependencyResolver = new WindsorDependencyResolver(_container);configuration.DependencyResolver = dependencyResolver;

 

建立一新 IHttpControllerActivator,更改 Controller 建立方式為從容器 Container 中擷取

實現 IHttpControllerActivator 介面 Create 方法

1 public IHttpController Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType)2 {3     var controller = (IHttpController)_container.Resolve(controllerType);4 5     request.RegisterForDispose(6         new Release(() => _container.Release(controller)));7 8     return controller;9 }

並且註冊當 web request 被 Dispose 的時候,Dispose 該 Controller。

替換 Default IHttpControllerActivator 行為

GlobalConfiguration.Configuration.Services.Replace(typeof(IHttpControllerActivator),new WindsorCompositionRoot(container));

 

參考資料:

Simplest Possible ASP.NET Web API Project that Implements IoC/DI using Castle Windsor

http://www.codeproject.com/Articles/710662/Simplest-Possible-ASP-NET-Web-API-Project-that-Imp

WebAPI Dependency Injection

https://github.com/ucdavis/UCDArch/wiki/WebAPI-Dependency-Injection

Difference between inheriting ApiController vs. IHttpController

http://stackoverflow.com/questions/21464909/difference-between-inheriting-apicontroller-vs-ihttpcontroller

ASP.NET WEB API 2: HTTP MESSAGE LIFECYLE

http://www.asp.net/posters/web-api/asp.net-web-api-poster.pdf

使用 Castle Windsor 實現 Web API 依賴注入

http://beginor.github.io/2014/12/21/webapi-dependency-injection-with-castle-windsor.html

ASP.NET Web API - 使用 Castle Windsor 依賴注入

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.