2010-04-16 80 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