2

Ninject Interceptions Extentions と Dynamic Proxy v3.0 を使用してインターセプトを行っています。MVC 3 コントローラーをクラス プロキシ インターセプトしようとしています。コントローラーはインターセプトされますが、動作は正しくありません。インターセプターは、クラス ControllerBase および Controller のパブリック仮想メソッドへの呼び出しのみをインターセプトします。私の HomeController パブリック仮想メソッドは傍受されません。これが私のコードです。Ninject Interception の代わりに MVC の Filters を使用してこれを達成することを考えています。

public class AuditAttribute : InterceptAttribute
{
    public override IInterceptor CreateInterceptor(IProxyRequest request)
    {
        return request.Context.Kernel.Get<AuditInterceptor>();            
    }
}

    [Audit] //HomeController method not intercepted.
    public virtual ActionResult Index()
    {
     return View();
    }

 public class AuditInterceptor : SimpleInterceptor
{
    public AuditInterceptor(IAuditor auditor)
    {
        if (auditor == null)
            throw new ArgumentNullException("auditor");
        this.auditor = auditor;            
    }        

    protected override void OnError(IInvocation invocation, Exception exception)
    {
        stopWatch.Stop();
        AuditEvent auditEvent = new AuditEvent();
        auditEvent.ExceptionDescription = exception.Message;
        auditEvent.FormName = string.Format("class: {0}, method: {1}", invocation.Request.Method.DeclaringType, invocation.Request.Method.Name);
        auditEvent.AppName = appName;            
        this.auditor.WriteAudit(auditEvent);
        auditEvent.LengthOfMethodCall = stopWatch.Elapsed;
        base.OnError(invocation, exception);
    }

    protected override void AfterInvoke(IInvocation invocation)
    {
        stopWatch.Stop();
        AuditEvent auditEvent = new AuditEvent();
        auditEvent.ExceptionDescription = defaultExp;
        auditEvent.FormName = string.Format("class: {0}, method: {1}", invocation.Request.Method.DeclaringType, invocation.Request.Method.Name);
        auditEvent.AppName = appName;            
        auditEvent.LengthOfMethodCall = stopWatch.Elapsed;
        this.auditor.WriteAudit(auditEvent);            
    }

    protected override void BeforeInvoke(IInvocation invocation)
    {
        stopWatch.Start();           
    } 

}

4

0 に答える 0