配置
http://www.castleproject.org/container/documentation/v21/manual/windsorconfigref.html
<configuration> <include uri="uri1" /> <include uri="uri2" /> <properties> <connection_string>value here</connection_string> </properties> <!-- used to register components before registering the facilities --> <bootstrap> <component id="" service="" type="" inspectionBehavior="all|declaredonly|none" lifestyle="singleton|thread|transient|pooled|custom" customLifestyleType="type that implements ILifestyleManager" initialPoolSize="<num>" maxPoolSize="<num>"> <forwardedTypes> <add service="" /> </forwardedTypes> <parameters> <paramtername>value</paramtername> </parameters> <interceptors> <interceptor>${interceptor.id}</interceptor> </interceptors> </component> </bootstrap> <facilities> <facility id="" type="Facility full type name"> </facility> </facilities> <components> <component id="" service="" type="" inspectionBehavior="all|declaredonly|none" lifestyle="singleton|thread|transient|pooled|custom" customLifestyleType="type that implements ILifestyleManager" initialPoolSize="<num>" maxPoolSize="<num>"> <forwardedTypes> <add service="" /> </forwardedTypes> <parameters> <paramtername>value</paramtername> </parameters> <interceptors> <interceptor>${interceptor.id}</interceptor> </interceptors> </component> </components> </configuration>
app.config
<?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <section name="castle" type="Castle.Windsor.Configuration.AppDomain.CastleSectionHandler, Castle.Windsor" /> </configSections> <castle> <!--出错<inclue uri="assembly://TestCastleWinsorConsoleApp.config.components.config" ></inclue> <inclue uri="assembly://TestCastleWinsorConsoleApp.config.components2.config" ></inclue>--> <!--用绝对路径可以--> <!--<include uri="file://E:\_Projects\vs2008\TestCastleWinsorConsoleApp\TestCastleWinsorConsoleApp\config\components.config"></include> <include uri="file://E:\_Projects\vs2008\TestCastleWinsorConsoleApp\TestCastleWinsorConsoleApp\config\components2.config"></include>--> <!--<components> <component id="taxcalc.service" type="TestCastleWinsorConsoleApp.TaxCalculator"> <parameters> <Rate>0.2511</Rate> </parameters> </component> <component id="taxcalc.service2" type="TestCastleWinsorConsoleApp.TaxCalculator" > <parameters> <Rate>0.2522</Rate> </parameters> </component> </components>--> </castle> </configuration>
<?xml version="1.0" encoding="utf-8" ?> <configuration> <components> <component id="taxcalc.service1" type="TestCastleWinsorConsoleApp.TaxCalculator"> <parameters> <Rate>0.111</Rate> </parameters> </component> <component id="taxcalc.service2" type="TestCastleWinsorConsoleApp.TaxCalculator" > <parameters> <Rate>0.222</Rate> </parameters> </component> </components> </configuration>
using System; using System.Collections.Generic; using System.Linq; using System.Text; using Castle.Windsor; using Castle.Windsor.Configuration.Interpreters; using Castle.Core.Resource; namespace TestCastleWinsorConsoleApp { public class TaxCalculator { private decimal _rate = 0.125M; public decimal Rate { set { _rate = value; } get { return _rate; } } public decimal CalculateTax(decimal gross) { return Math.Round(_rate * gross, 2); } } class Program { static void Main(string[] args) { //WindsorContainer container = new WindsorContainer(new XmlInterpreter( new FileResource("components2.config" ))) ; //container.AddChildContainer(new WindsorContainer(new XmlInterpreter( new FileResource("components.config" ))) ); WindsorContainer container = new WindsorContainer(new XmlInterpreter()); TaxCalculator calculator = container.Resolve<TaxCalculator>("taxcalc.service1"); decimal gross = 100; decimal tax = calculator.CalculateTax(gross); Console.WriteLine("Gross: {0}, Tax: {1}", gross, tax); Console.ReadKey(); } } }