Proxy被创建,拦截器在__interceptors数组中,但是拦截器从未被调用

发布于 2024-08-29 02:07:48 字数 1850 浏览 3 评论 0原文

这是我第一次使用拦截器进行流畅的注册,但我遗漏了一些东西。通过以下注册,我可以解析一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

魂ガ小子 2024-09-05 02:07:48

正如 Mauricio 暗示的那样,您似乎将组件注册为类服务,而不是接口服务。在这种情况下,除非您拦截的方法是虚拟的,否则您将无法拦截它。将您的注册更改为:

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

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:

AllTypes.FromAssembly(Assembly.GetExecutingAssembly())
 .BasedOn<IProcessingStep>()
 .ConfigureFor<IProcessingStep>(c => c
  .Unless(Component.ServiceAlreadyRegistered)
  .LifeStyle.PerThread
  .Interceptors(InterceptorReference.ForType<StepLoggingInterceptor>()).First
  ).WithService.Base(),
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文