title: Configuring Castle Windsor using xml/app.config
body: <p>I am currently building a sample application using Castle Windsor. The motto is to use xml/app.config to switch method interception on/off. I had used the Fluent API earlier and it worked as a charm. As the next step, I am trying to replace the fluent API with my xml.</p>
  
  <p>The gist of the code is as follows:
  A class called RandomOperations with two virtual methods.
  A LoggingAspect class which implements IInterceptor.
  A MyInterceptorsSelector  class which implements IModelInterceptorsSelector
  A Program.cs which had the fluent api syntax earlier and is now uses to only make calls to methods of RandomOperations class.
  An app.config with a section called  which has the xml syntax of registering components.</p>
  
  <p>When I use the fluent api, I am able to intercept the method calls but I am unable to do it using the xml/app.config registration. Could someone please throw some light on what is being missed?</p>
  
  <p>The classes are as follows:</p>
  
  <p>RandomOperations.cs</p>
  
  <pre><code>public class RandomOperations 
      {
          public virtual int MyRandomMethod(int x)
          {
              return x * x;
          }
  
          public virtual void Writer(string x)
          {
              Console.WriteLine(x);
          }
      }
  </code></pre>
  
  <p>LoggingAspect.cs</p>
  
  <pre><code>public class LoggingAspect : IInterceptor
      {
          public void Intercept(IInvocation invocation)
          {
              Console.WriteLine("Intercepted the call to " + invocation.Method.Name);
              invocation.Proceed();
              Console.WriteLine("After the method call, the return value is " + invocation.ReturnValue);
          }
      }
  </code></pre>
  
  <p>MyInterceptorsSelector.cs</p>
  
  <pre><code>public class MyInterceptorsSelector : IModelInterceptorsSelector
      {
  
          public bool HasInterceptors(ComponentModel model)
          {
              return typeof(LoggingAspect) != model.Implementation &amp;&amp;
                  model.Implementation.Namespace.StartsWith("ConsoleApplication1") ;
          }
  
          public InterceptorReference[] SelectInterceptors(ComponentModel model, Castle.Core.InterceptorReference[] obj)
          {
              var interceptors = new List&lt;InterceptorReference&gt;(model.Interceptors.Count + 1);
              foreach (InterceptorReference inter in model.Interceptors)
              {
                  interceptors.Add(inter);
              }
  
              return interceptors.ToArray();
  
          }
      }
  </code></pre>
  
  <p>Main in Program.cs</p>
  
  <pre><code>static void Main(string[] args)
          {
              var container = new WindsorContainer();
              //container.Register(Component.For&lt;RandomOperations&gt;().Interceptors(typeof(LoggingAspect)));
              //container.Register(Component.For&lt;LoggingAspect&gt;());
              //container.Kernel.ProxyFactory.AddInterceptorSelector(new MyInterceptorsSelector());
              var service = container.Resolve&lt;RandomOperations&gt;();
              service.MyRandomMethod(4);
              service.Writer("Hello, World");
          }
  </code></pre>
  
  <p>Removing the commented out fluent api syntax makes the application work correctly.</p>
  
  <p>App.config</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8" ?&gt;
  &lt;configuration&gt;
    &lt;configSections&gt;
      &lt;section name="castle" type="Castle.Windsor.Configuration.AppDomain.CastleSectionHandler, Castle.Windsor" /&gt;
    &lt;/configSections&gt;
  
    &lt;castle&gt;
      &lt;components&gt;
  
        &lt;component id="MyInterceptorsSelector" type="MyInterceptorsSelector"/&gt;
        &lt;component
          id="LoggingAspect"
          type="ConsoleApplication1.LoggingAspect, ConsoleApplication1"&gt;
        &lt;/component&gt;
        &lt;component
          type="ConsoleApplication1.RandomOperations, ConsoleApplication1"&gt;
          &lt;interceptors selector="${MyInterceptorsSelector}"&gt;
            &lt;interceptor&gt;${LoggingAspect}&lt;/interceptor&gt;
          &lt;/interceptors&gt;
        &lt;/component&gt;
  
      &lt;/components&gt;
    &lt;/castle&gt;
    &lt;startup&gt;
      &lt;supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /&gt;
    &lt;/startup&gt;
  
  &lt;/configuration&gt;
  </code></pre>
  
  <p>Thanks in advance.</p>