这是我第一次使用具有流畅注册的拦截器,但我遗漏了一些东西。通过以下注册,我可以解析一个 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());
}
}
}