Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have MVC 3 app with this same problem. I use Ninject for dependency injection. I cannot trace which controller has a problem, it broke yesterday and I have spent an entire day trying to locate. There are over 60 controllers in the application. The exception cannot be caught or debugged nor the trace provide any meaningful clue as to the problematic source controller. No action for any controller can be accessed without this exception being thrown.

Has anyone any idea how I can determine the actual controller having the problem?

Thanks

Inner Exception:

System.MissingMethodException: No parameterless constructor defined for this object.
at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean
noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck)
at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache)
at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipVisibilityChecks, Boolean skipCheckThis, Boolean fillCache)
at System.Activator.CreateInstance(Type type, Boolean nonPublic)
at System.Web.Mvc.DefaultControllerFactory.DefaultControllerActivator.Create(RequestContext requestContext, Type controllerType)

Stack Trace:

at System.Web.Mvc.DefaultControllerFactory.DefaultControllerActivator.Create(RequestContext requestContext, Type controllerType)
at System.Web.Mvc.DefaultControllerFactory.GetControllerInstance(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.<GetCallInAppTrustThunk>b__0(Action f)
at System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust(Action action)
at System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust[TResult](Func`1 func)
at System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, Object state)
at System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContext httpContext, AsyncCallback callback, Object state)
at System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData)
at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)
share|improve this question
1  
No action for any controller can be accessed without this exception being thrown.. It sounds like something you're injecting in every controller or an action filter that is used in all controllers is broken (ie missing a dependency). A single broken controller should not break your whole system. – Joachim Isaksson 2 days ago
Hm, this may be nothing since I'm not sure what the stack trace would look like if it's actually using NInject to resolve the controller, but to me it looks like it's using the default activator instead of NInject. Are you sure NInject is correctly registered? – Joachim Isaksson 2 days ago

2 Answers

Seems NInject either is not registered correctly to resolve anything at all, or it cannot resolve any of your controller types. From the source of MVC3, this is the call to Activator.CreateInstance that shows in the stack trace;

return (IController)(_resolverThunk().GetService(controllerType) ??
                     Activator.CreateInstance(controllerType));

In other words, it will not call Activator.CreateInstance at all unless the controller cannot be resolved correctly by NInject.

In other words, you should probably stop hunting the problem in your controllers, and start checking up on the NInject registration. Simplest first step would be to make a dead stupid controller with a default constructor, set a breakpoint in the constructor and see that the stack trace reflects that the constructor is being called by NInject. If it's not, NInject is not registered correctly.

share|improve this answer
Thanks for reply. No breakpoint in a constructor ever gets hit. – Gordon Hickley 2 days ago
@GordonHickley Not even when using a parameterless constructor on your test controller? Still getting the same stack trace? – Joachim Isaksson 2 days ago

oh, you could do a little test method.

First, find the Assembly of your controllers (hoping they're all in the same, or do this for each assembly).

Then

var noParameterLessControllers = <yourAssembly>
                                 .GetTypes()
                                 .Where(m => 
                                       m.IsSubclassOf(typeof(Controller)) &&
                                       m.GetContstructor(Type.EmptyTypes)  == null);
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.