Configuring Castle Windsor using xml/app.config -
i building sample application using castle windsor. motto use xml/app.config switch method interception on/off. had used fluent api earlier , worked charm. next step, trying replace fluent api xml.
the gist of code follows: class called randomoperations 2 virtual methods. loggingaspect class implements iinterceptor. myinterceptorsselector class implements imodelinterceptorsselector program.cs had fluent api syntax earlier , uses make calls methods of randomoperations class. app.config section called has xml syntax of registering components.
when use fluent api, able intercept method calls unable using xml/app.config registration. please throw light on being missed?
the classes follows:
randomoperations.cs
public class randomoperations { public virtual int myrandommethod(int x) { return x * x; } public virtual void writer(string x) { console.writeline(x); } }
loggingaspect.cs
public class loggingaspect : iinterceptor { public void intercept(iinvocation invocation) { console.writeline("intercepted call " + invocation.method.name); invocation.proceed(); console.writeline("after method call, return value " + invocation.returnvalue); } }
myinterceptorsselector.cs
public class myinterceptorsselector : imodelinterceptorsselector { public bool hasinterceptors(componentmodel model) { return typeof(loggingaspect) != model.implementation && model.implementation.namespace.startswith("consoleapplication1") ; } public interceptorreference[] selectinterceptors(componentmodel model, castle.core.interceptorreference[] obj) { var interceptors = new list<interceptorreference>(model.interceptors.count + 1); foreach (interceptorreference inter in model.interceptors) { interceptors.add(inter); } return interceptors.toarray(); } }
main in program.cs
static void main(string[] args) { var container = new windsorcontainer(); //container.register(component.for<randomoperations>().interceptors(typeof(loggingaspect))); //container.register(component.for<loggingaspect>()); //container.kernel.proxyfactory.addinterceptorselector(new myinterceptorsselector()); var service = container.resolve<randomoperations>(); service.myrandommethod(4); service.writer("hello, world"); }
removing commented out fluent api syntax makes application work correctly.
app.config
<?xml version="1.0" encoding="utf-8" ?> <configuration> <configsections> <section name="castle" type="castle.windsor.configuration.appdomain.castlesectionhandler, castle.windsor" /> </configsections> <castle> <components> <component id="myinterceptorsselector" type="myinterceptorsselector"/> <component id="loggingaspect" type="consoleapplication1.loggingaspect, consoleapplication1"> </component> <component type="consoleapplication1.randomoperations, consoleapplication1"> <interceptors selector="${myinterceptorsselector}"> <interceptor>${loggingaspect}</interceptor> </interceptors> </component> </components> </castle> <startup> <supportedruntime version="v4.0" sku=".netframework,version=v4.5" /> </startup> </configuration>
thanks in advance.
you need pass iconfigurationinterpreter
windsor constructor. change:
var container = new windsorcontainer();
to:
var container = new windsorcontainer(new xmlinterpreter());
the xmlinterpreter
(with no parameters) pull configuration app.config/web.config.
for more options on using iconfigurationinterpreter
, see docs.
Comments
Post a Comment