簡體   English   中英

創建了代理,並且攔截器位於__interceptors數組中,但是從未調用該攔截器

[英]Proxy is created, and interceptor is in the __interceptors array, but the interceptor is never called

這是我第一次將攔截器與流利的注冊結合使用,而我卻缺少一些東西。 通過以下注冊,我可以解析一個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());
  }
 }
}

作為Mauricio提示,您似乎正在將組件注冊為類服務,而不是接口服務。 在這種情況下,除非您要攔截的方法是虛擬的,否則您將無法對其進行攔截。 將您的注冊更改為:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM