Proxy被创建,拦截器在__interceptors数组中,但是拦截器从未被调用
这是我第一次使用拦截器进行流畅的注册,但我遗漏了一些东西。通过以下注册,我可以解析一个 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());
}
}
}
This is the first time I've used interceptors with the fluent registration and I'm missing something. With the following registration, I can resolve an IProcessingStep, and it's a proxy class and the interceptor is in the __interceptors array, but for some reason, the interceptor is not called. Any ideas what I'm missing?
Thanks,
Drew
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());
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如 Mauricio 暗示的那样,您似乎将组件注册为类服务,而不是接口服务。在这种情况下,除非您拦截的方法是虚拟的,否则您将无法拦截它。将您的注册更改为:
As Mauricio hinter you appear to be registering your components as a class service, not interface service. In this case unless method you're intercepting is virtual you won't be able to intercept it. Change your registration to: