プロキシが作成され、インターセプターは__インテルコースアレイにありますが、インターセプターは決して呼び出されません

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

質問

流fluent登録でインターセプターを使用したのはこれが初めてで、何かが足りません。以下の登録を使用すると、iProcessingStepを解決できます。これはプロキシクラスであり、インターセプターは__インテラクターアレイにありますが、何らかの理由でインターセプターは呼び出されません。私が欠けているものはありますか?

ありがとう、ドリュー

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