0
votes

I am Having this issue

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.

I am create a new project in mvc and used Layered architecture for data and repository. so when i called account controller i ZGit this error

This is My Code :- of Account Controller

public class AccountController : Controller
    {
        private readonly IAuthentication repositoryIAuthentication;
        private readonly IUserRepository repositoryUser;
        private readonly IPostRepository repositoryPost;
        private readonly ICommentRepository repositoryComment;
        private readonly ICategoryRepository repositoryCategory;
        private readonly MembershipProvider repositoryMemberShipProvider;
        private readonly IDEncryptionRepository repositoryDEncryption;
        private readonly IEmailSettingRepository repositoryEmailSetting;
        private readonly ISettingRepository repositorySetting;
        public AccountController(IUserRepository repoUser,
                                 IAuthentication repoIAuthentication,
                                 IPostRepository repoPost,
                                 ICommentRepository repoComment,
                                 ICategoryRepository repoCategory,
                                 MembershipProvider repoMemberShipProvider,
                                 IDEncryptionRepository repoDEncryption,
                                 IEmailSettingRepository repoEmailSetting,
                                 ISettingRepository repoSetting)
        {
            repositoryUser = repoUser;
            repositoryIAuthentication = repoIAuthentication;
            repositoryCategory = repoCategory;
            repositoryComment = repoComment;
            repositoryPost = repoPost;
            repositoryMemberShipProvider = repoMemberShipProvider;
            repositoryDEncryption = repoDEncryption;
            repositoryEmailSetting = repoEmailSetting;
            repositorySetting = repoSetting;
        }

This is the detailed error which shows on page : -

[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, StackCrawlMark& stackMark) +119
System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark) +232 System.Activator.CreateInstance(Type type, Boolean nonPublic) +83 System.Activator.CreateInstance(Type type) +11 System.Web.Mvc.DefaultControllerActivator.Create(RequestContext requestContext, Type controllerType) +55

[InvalidOperationException: An error occurred when trying to create a controller of type 'WebAppBlog.Controllers.AccountController'. Make sure that the controller has a parameterless public constructor.]
System.Web.Mvc.DefaultControllerActivator.Create(RequestContext requestContext, Type controllerType) +178
System.Web.Mvc.DefaultControllerFactory.GetControllerInstance(RequestContext requestContext, Type controllerType) +76
System.Web.Mvc.DefaultControllerFactory.CreateController(RequestContext requestContext, String controllerName) +88
System.Web.Mvc.MvcHandler.ProcessRequestInit(HttpContextBase httpContext, IController& controller, IControllerFactory& factory) +194 System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, Object state) +50
System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContext httpContext, AsyncCallback callback, Object state) +48
System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData) +16
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +103 System.Web.HttpApplication.ExecuteStepImpl(IExecutionStep step) +48 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +159

1
Just add public AccountController() { ... } as parameterless constructor should fine. - Tetsuya Yamamoto
I was do it but it solves my error. but when I called my login function it doesn't initlize the repository class so I get another error - mitesh
If you configure DI properly the DI container will inject those interfaces. Please post your DI configuration code. The fact you inject so many repos though should be an VERY STRONG INDICATION that something is wrong. Either the controller is doing too much or the repos are doing too little. - Panagiotis Kanavos
If those repos end up calling EF, you've actually broken the repo pattern. A DbContext is a repo, more or less. You don't need anything more to load or query entities. To justify its cost, a Repository class should provide functionality on top of the ORM and combine entities, perform complex queries etc. Right now, it acts as if it were a level below the ORM, at the level of the DbSet or even below that. With a proper repository you would probably need only one interface - Panagiotis Kanavos
Poor design -- I wouldn't expect to be seeing any repositories at this level. The controllers should be light weight and basically used as a switching mechanism (gateway between the client and server). The controllers should push data through to a service layer that interacts with a repository layer. It should feed data from the service layer to the client. - bilpor

1 Answers

0
votes

I suspect the issue lies with the huge amount of dependencies your controller has. One of the repositories probably has a dependency which has not been registered with your container.

I would look into the unit of work pattern and if using Entity Framework use it’s dbcontext (which implements the unit of work pattern). Or even better abstract to an IAccountService and in the implementation use the unit of work pattern there.