2010-04-16 76 views
0

這是我第一次使用流利註冊的攔截器,並且我錯過了一些東西。通過以下注冊,我可以解析IProcessingStep,它是一個代理類,攔截器位於__interceptors數組中,但由於某種原因,攔截器未被調用。任何想法我失蹤?代理被創建,並且攔截器在__interceptors數組中,但是攔截器從未被調用

感謝, 德魯

AllTypes.Of<IProcessingStep>() 
.FromAssembly(Assembly.GetExecutingAssembly()) 
.ConfigureFor<IProcessingStep>(c => c 
    .Unless(Component.ServiceAlreadyRegistered) 
    .LifeStyle.PerThread 
    .Interceptors(InterceptorReference.ForType<StepLoggingInterceptor>()).First 
), 
Component.For<StepMonitorInterceptor>(), 
Component.For<StepLoggingInterceptor>(), 
Component.For<StoreInThreadInterceptor>() 


public abstract class BaseStepInterceptor : IInterceptor 
{ 
public void Intercept(IInvocation invocation) 
{ 
    IProcessingStep processingStep = (IProcessingStep)invocation.InvocationTarget; 
    Command cmd = (Command)invocation.Arguments[0]; 
    OnIntercept(invocation, processingStep, cmd); 
} 

protected abstract void OnIntercept(IInvocation invocation, IProcessingStep processingStep, Command cmd); 
} 

public class StepLoggingInterceptor : BaseStepInterceptor 
{ 
private readonly ILogger _logger; 

public StepLoggingInterceptor(ILogger logger) 
{ 
    _logger = logger; 
} 

protected override void OnIntercept(IInvocation invocation, IProcessingStep processingStep, Command cmd) 
{ 
    _logger.TraceFormat("<{0}> for cmd:<{1}> - begin", processingStep.StepType, cmd.Id); 

    bool exceptionThrown = false; 

    try 
    { 
    invocation.Proceed(); 
    } 
    catch 
    { 
    exceptionThrown = true; 
    throw; 
    } 
    finally 
    { 
    _logger.TraceFormat("<{0}> for cmd:<{1}> - end <{2}> times:<{3}>", 
     processingStep.StepType, cmd.Id, 
     !exceptionThrown && processingStep.CompletedSuccessfully 
     ? "succeeded" : "failed", 
     cmd.CurrentMetric==null ? "{null}" : cmd.CurrentMetric.ToString()); 
    } 
} 
} 
+1

看到http://stackoverflow.com/questions/1188957/castle-interceptors-with-fluent-interface – 2010-04-16 03:58:59

+0

我看這一點,它似乎並不適用。我的攔截器正在實例化並附加到代理。另外,使用任何WithService事件都會導致服務未被註冊。 – drewburlingame 2010-04-16 04:30:00

回答

1

正如毛腹地你似乎是註冊您的部件爲一類服務,而不是接口服務。在這種情況下,除非你攔截的方法是虛擬的,否則你將無法攔截它。您的註冊更改爲:

AllTypes.FromAssembly(Assembly.GetExecutingAssembly()) 
.BasedOn<IProcessingStep>() 
.ConfigureFor<IProcessingStep>(c => c 
    .Unless(Component.ServiceAlreadyRegistered) 
    .LifeStyle.PerThread 
    .Interceptors(InterceptorReference.ForType<StepLoggingInterceptor>()).First 
).WithService.Base(), 
+0

這讓我想到了我的最終答案。 WithService.Base不起作用,因爲我有幾個相同基類型的服務要註冊。我使用WithService.FirstInterface()去創建一個接口層來定義步驟的類型。這使代碼更易於閱讀,並且更容易通過配置覆蓋。謝謝您的幫助。 – drewburlingame 2010-04-28 20:22:59

+0

感謝Krzysztof注意,必須將方法標記爲虛擬以使Windsor攔截器觸發(當註冊爲類服務而不是通過接口時)。直到我做出這個改變,我的Windsor攔截器才工作。後來,我回去重構了一些使用通用接口的東西,然後不需要虛擬修改器。 – 2016-02-07 23:32:52