0

我在 MVC 3 网站中使用 Ninject DI,但有时会出现以下异常:

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)

我正在使用 ELMAH 来捕获所有异常,所以这可能只是一个内部错误,不会影响请求的最终结果,但我还是想修复它。我在测试或开发时没有在网站上看到这个错误,它只出现在 ELMAH 的日志中。

据我所见,当 Ninject 尝试实例化名为 OrderController 的控制器时发生错误,该控制器继承自 CRUDController。

我的猜测是,这发生在我部署了新版本并且正在重新编译站点之后,因为大多数时候它没有问题并且站点运行良好。

我想知道这是否是 Ninject 的一个已知问题,或者问题出在我的代码中。谢谢。

4

1 回答 1

1

堆栈跟踪告诉它CRUDController抛出某种异常。它被 MVC 捕获并重新抛出一个完全不同的异常。不幸的是,内部异常的类型没有被记录,这意味着很难判断你必须寻找什么样的异常。,

于 2012-05-04T12:00:07.473 回答