Прокси создается, и перехватчик находится в массиве __interceptors, но перехватчик никогда не вызывается

StackOverflow https://stackoverflow.com/questions/2650008

Вопрос

Это первый раз, когда я использовал перехватчики с свободной регистрацией, и я что-то упускаю. Со следующей регистрацией я могу разрешить iProcessingstep, и это прокси-класс, а перехватчик находится в массиве __interceptor, но по какой-то причине перехватчик не вызывается. Любые идеи, чего я скучаю?

Спасибо, Дрю

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 Hinter вы, кажется, регистрируют ваши компоненты в качестве класса обслуживания, а не сервис интерфейса. В этом случае, если метод, который вы не перехватываете, является виртуальным, вы не сможете его перехватить. Измените свою регистрацию в:

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 с атрибуция
Не связан с StackOverflow
scroll top