这是我第一次使用流畅的注册拦截器而且我遗漏了一些东西。通过以下注册,我可以解析IProcessingStep,它是一个代理类,拦截器在__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());
}
}
}
答案 0 :(得分:1)
作为Mauricio hinter,您似乎将组件注册为类服务,而不是接口服务。在这种情况下,除非你拦截的方法是虚拟的,否则你将无法拦截它。将您的注册更改为:
AllTypes.FromAssembly(Assembly.GetExecutingAssembly())
.BasedOn<IProcessingStep>()
.ConfigureFor<IProcessingStep>(c => c
.Unless(Component.ServiceAlreadyRegistered)
.LifeStyle.PerThread
.Interceptors(InterceptorReference.ForType<StepLoggingInterceptor>()).First
).WithService.Base(),