I am attempting to use Ninject on my current project, and up to now, have been loving it. I am in the middle of attempting to configure an IInterceptor object to intercept and handle a failed method call to my service layer. This is hosted in an ASP.NET MVC 5 application.
In the IInterceptor
, I've tried several things, such as:
Setting private variables using constructor injection, but came to discover that it appears Ninject will reuse an
IInterceptor
instance for a method indefinitely, and I haven't found a way to stop that. Since one of the things I bring into scope is aDbContext
which gets disposed elsewhere, it ends up failing on any future requests than the one it was created on.I found that the
IInvocation
has aRequest.Kernel
property. However, when I attempt to resolve my UOW from the container, which is.InRequestScope()
, it fails, since it attempts to resolve theIUowService
dependencies, (one of the dependencies depends on the HttpContext which is null at this point), but appears to be doing so outside the Request scope. It is ignoring the fact that the dependencies it needs have already been created within the ASP.NET request, and is attempting to create new ones.Setting a binding for the interceptor
this.Bind<NinjectExceptionHandler>().ToSelf().InTransientScope()
, yet this didn't seem to stop the caching of the interceptor.
I imagine there is something I am missing. I understand wanting to cache IInterceptor
objects for performance, but I find it irksome that I can't easily use the IOC container or Injection to get the objects I need for my request.
This is the last issue I am having with getting interception up and running as I need, so any help is greatly appreciated!