Note that there are some explanatory texts on larger screens.

plurals
  1. POIntercepting methods called by the Startable facility with Castle Windsor 3.0
    primarykey
    data
    text
    <p>I'm using Castle Windsor 3.0.</p> <p>I have a component that should be started automatically after the registration phase. I would also like to intercept exceptions coming from it's Start/Stop methods and log their details.</p> <p>To make my component startable, I used the Startable facility that comes with Windsor:</p> <pre class="lang-cs prettyprint-override"><code>container.AddFacility&lt;StartableFacility&gt;(f =&gt; f.DeferredStart()); </code></pre> <p>I created a custom interceptor like this:</p> <pre class="lang-cs prettyprint-override"><code>class ExceptionLoggerInterceptor : IInterceptor { IExceptionLogger m_ExceptionLogger; public ExceptionLoggerInterceptor(IExceptionLogger exceptionLogger) { if (exceptionLogger == null) throw new ArgumentNullException("exceptionLogger"); m_ExceptionLogger = exceptionLogger; } public void Intercept(IInvocation invocation) { try { invocation.Proceed(); } catch (Exception ex) { m_ExceptionLogger.Write(ex, invocation.Method.Name, invocation.TargetType.Name); } } } </code></pre> <p>and I registered the component like this:</p> <pre class="lang-cs prettyprint-override"><code>Component.For&lt;IExceptionLogger&gt;() .ImplementedBy&lt;EnterpriseLibraryExceptionLogger&gt;() .LifeStyle.Singleton, Component.For&lt;ExceptionLoggerInterceptor&gt;() .LifeStyle.Singleton, Component.For&lt;IWorkflowService&gt;() .ImplementedBy&lt;WorkflowService&gt;() .LifeStyle.Singleton .StartUsingMethod(c =&gt; c.Start) .StopUsingMethod(c =&gt; c.Stop) .Interceptors(InterceptorReference.ForType&lt;ExceptionLoggerInterceptor&gt;()).Anywhere </code></pre> <p>To make a test, I coded a dirty</p> <pre class="lang-cs prettyprint-override"><code>throw new Exception(); </code></pre> <p>in the implementation of the Start method of the component. At registration phase, when Windsor automatically calls the Start method on the component, the exception is thrown, but never intercepted by my custom interceptor.</p> <p>I made another test, this time without using the Startable facility but rather calling the Start method manually. The exception got thrown and was intercepted by my custom interceptor.</p> <p>So, as the title of this post asks, is there a way to intercept methods called by the Startable facility with Windsor?</p> <p>Regards</p> <p>Louis-Pierre Beaumont</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload