Proxy è stato creato, e intercettore è nella matrice __interceptors, ma l'intercettore non viene mai chiamato

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

Domanda

Questa è la prima volta che ho usato intercettori con la registrazione fluente e mi manca qualcosa. Con la seguente registrazione, posso risolvere un IProcessingStep, ed è una classe proxy e l'intercettore è nella matrice __interceptors, ma per qualche ragione, l'intercettore non è chiamato. Tutte le idee che cosa mi manca?

Grazie, 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());
  }
 }
}
È stato utile?

Soluzione

Come Mauricio hinter si sembra essere registrare i componenti come un servizio di classe, non il servizio di interfaccia. In questo caso, a meno che il metodo si sta intercettando è virtuale non sarà in grado di intercettarlo. Cambia la tua registrazione a:

AllTypes.FromAssembly(Assembly.GetExecutingAssembly())
 .BasedOn<IProcessingStep>()
 .ConfigureFor<IProcessingStep>(c => c
  .Unless(Component.ServiceAlreadyRegistered)
  .LifeStyle.PerThread
  .Interceptors(InterceptorReference.ForType<StepLoggingInterceptor>()).First
  ).WithService.Base(),
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top