SzilardD
SzilardD

Reputation: 1751

Ninject cannot create controller in MVC 3 site

I'm using Ninject DI in an MVC 3 website and only from time to time I get the exception below:

System.InvalidOperationException: An error occurred when trying to create a controller of type 'SampleMVCApp.Controllers.OrderController'. Make sure that the controller has a parameterless public constructor. ---> System.NullReferenceException: Object reference not set to an instance of an object.
       at SampleMVCApp.Controllers.CRUDController`3..ctor()
       at SampleMVCApp.Controllers.OrderController..ctor()
       at DynamicInjector4625402b6de6431a8914a00f1cecee0d(Object[] )
       at Ninject.Activation.Context.Resolve()
       at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
       at System.Linq.Enumerable.SingleOrDefault[TSource](IEnumerable`1 source)
       at Ninject.ResolutionExtensions.TryGet[T](IEnumerable`1 iterator)
       at SampleMVCApp.MvcApplication.NinjectDependencyResolver.GetService(Type serviceType)
       at System.Web.Mvc.DefaultControllerFactory.DefaultControllerActivator.Create(RequestContext requestContext, Type controllerType)
       --- End of inner exception stack trace ---
       at System.Web.Mvc.DefaultControllerFactory.DefaultControllerActivator.Create(RequestContext requestContext, Type controllerType)
       at System.Web.Mvc.DefaultControllerFactory.CreateController(RequestContext requestContext, String controllerName)
       at System.Web.Mvc.MvcHandler.ProcessRequestInit(HttpContextBase httpContext, IController& controller, IControllerFactory& factory)
       at System.Web.Mvc.MvcHandler.<>c__DisplayClass6.<BeginProcessRequest>b__2()
       at System.Web.Mvc.SecurityUtil.<>c__DisplayClassb`1.<ProcessInApplicationTrust>b__a()
       at System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust[TResult](Func`1 func)
       at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
       at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

I'm using ELMAH to catch all exceptions so maybe this is only an internal error and doesn't affect the final outcome of the request, but I would like to fix it nonetheless. I haven't seen this error on the site while testing or developing, it only appears in ELMAH's logs.

From what I can see, the error occurs when Ninject tries to instantiate the controller called OrderController, which is inherited from CRUDController.

My guess is that this happens after I deploy a new version and the site is being recompiled, because most of the time there is no problem with it and the site works fine.

I'm wondering if this is a known issue with Ninject or the problem is in my code. Thanks.

Upvotes: 0

Views: 413

Answers (1)

Remo Gloor
Remo Gloor

Reputation: 32725

The stack trace tells that it is the CRUDController that throws some kind of exception. It is caught by MVC and a completely different exception is rethrown. Unfortunately the type of inner exception is not logged which means it will get difficult to tell what kind of exception you have to look for.,

Upvotes: 1

Related Questions