Search code examples
asp.netninjectasp.net-web-apiinterception

Catching exception of ApiController in Ninject Interceptor


Hi i have a problem with exception handling in ApiController.

I have Ninject interceptor bound to Controller actions the problem is that interceptor never sees any exception that was thrown in/bellow Controller.

public class ControllerInterceptor : IInterceptor
    {
        private DbContextTransaction transaction;

        public void Intercept(IInvocation invocation)
        {
            try
            {
                invocation.Proceed() // action will throw exception for sure
            }
            catch (Exception) // never executes even if there was exception in Proceed
            {

                throw;
            }
        }
    }        
}

My only idea is that ApiController catches all exceptions and never forwards them up so for interceptor everything looks like nothing happend. Can anyone confirm that.


Solution

  • Had the same problem. I used Ninject.Extensions.Interception with Ninject.Extensions.Interception.Linfu. Resolved making the action method virtual.