error: no parameterless constructor

Jun 23, 2011 at 7:14 AM

Hi,

Lately I get the following error "no parameterless constructor".

Restarting IIS (also in express) It seems to work until it throws this error again.

I have implemented the latest version of MvcExtensions .

Any ideas what can cause MVC to try and load the default constructor for the controller?

Thanks

Developer
Jun 23, 2011 at 11:07 AM

Can you post a exception stacktrace?

Which version of MvcExtensions are you using (MVC2 or MVC3)?

Which IoC container adapter?

Jun 23, 2011 at 11:34 AM

I am using MVC 3 (mvcextensions_5c8beedb4146)

Unity as IOC 

this is the Global.ascx.cs:

 

 public class MvcApplication : UnityMvcApplication
    {
        public MvcApplication()
        {
            Bootstrapper.BootstrapperTasks
                .Include<RegisterModelMetadata>()
                .Include<RegisterControllers>()
                .Include<ConfigureFilters>()
                .Include<RegisterModelBinders>()
                .Include<RegisterRoutes>()
                .Include<ConfigureAssets>()
                ;
        }

Stack trace :

Server Error in '/' Application.
--------------------------------------------------------------------------------

No parameterless constructor defined for this object. 
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.MissingMethodException: No parameterless constructor defined for this object.

Source Error: 

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.  

Stack Trace: 


[MissingMethodException: No parameterless constructor defined for this object.]
   System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck) +0
   System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache) +98
   System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipVisibilityChecks, Boolean skipCheckThis, Boolean fillCache) +241
   System.Activator.CreateInstance(Type type, Boolean nonPublic) +69
   System.Activator.CreateInstance(Type type) +6
   MvcExtensions.ExtendedControllerActivator.Create(RequestContext requestContext, Type controllerType) +275
   System.Web.Mvc.DefaultControllerFactory.GetControllerInstance(RequestContext requestContext, Type controllerType) +80
   System.Web.Mvc.DefaultControllerFactory.CreateController(RequestContext requestContext, String controllerName) +74
   System.Web.Mvc.MvcHandler.ProcessRequestInit(HttpContextBase httpContext, IController& controller, IControllerFactory& factory) +196
   System.Web.Mvc.<>c__DisplayClass6.b__2() +49
   System.Web.Mvc.<>c__DisplayClassb`1.b__a() +13
   System.Web.Mvc.SecurityUtil.b__0(Action f) +7
   System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust(Action action) +22
   System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust(Func`1 func) +124
   System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, Object state) +98
   System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContext httpContext, AsyncCallback callback, Object state) +50
   System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData) +16
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +8889476
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +184
 


--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.225 

 

 

 

 

 

Developer
Jun 23, 2011 at 12:30 PM

Ok, thanks. I'll check this.

Developer
Jun 24, 2011 at 7:12 PM

MvcExtensions trying to create controller with activator if it cannot be created with ioc container. I gues somthing wrong with lifetime of one or more component on which controller depends.

I've added debug information for situation when container throws exception on type resolution. please check this http://mvcextensions.codeplex.com/SourceControl/changeset/changes/bea5a08df651 

Jun 24, 2011 at 8:30 PM

Thanks I will check it out and let you know

Aug 25, 2011 at 6:48 AM

Has there been any update on this?  I get this same error when one of our Entities has a private constructor.

Exception thrown on Line 30 of MvcExtensions.ExtendedControllerActivator.

      Controller controller = (activator != null) ? (activator.Create(requestContext, controllerType) as Controller) : ((this.Container.GetService(controllerType) ?? Activator.CreateInstance(controllerType)) as Controller);

Aug 25, 2011 at 7:14 AM

Sorry, not an Entity but a Controller that doesn't have a public constructor will cause this error.  Perhaps we could throw a more specific error?

Aug 25, 2011 at 8:56 AM

After using the latest version the error was gone.

Developer
Aug 25, 2011 at 2:59 PM

@rentvillas, please try latest version (1006ddccf98a)

Developer
Sep 26, 2011 at 6:05 AM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.