MVc5回购模式中没有为此对象定义无参数构造函数

时间:2019-02-19 07:39:47

标签: c# asp.net-mvc default-constructor

我遇到了这个问题

  

没有为此对象定义无参数构造函数。说明:   当前Web执行期间发生未处理的异常   请求。请查看堆栈跟踪以获取有关   错误及其在代码中的起源。

     

异常详细信息:System.MissingMethodException:无无参数   为此对象定义的构造函数。

我正在mvc中创建一个新项目,并使用了用于数据和存储库的分层体系结构。因此,当我致电帐户控制器时,我会ZGit这个错误

这是我的代码:-帐户控制员

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;
        }

这是在页面上显示的详细错误:-

  

[MissingMethodException:没有为此定义的无参数构造函数   对象。] System.RuntimeTypeHandle.CreateInstance(RuntimeType类型,   布尔publicOnly,布尔noCheck,布尔&canBeCached,   RuntimeMethodHandleInternal&ctor,Boolean&bNeedSecurityCheck)+0
  System.RuntimeType.CreateInstanceSlow(布尔publicOnly,布尔   skipCheckThis,布尔值fillCache,StackCrawlMark和stackMark)+119
  System.RuntimeType.CreateInstanceDefaultCtor(布尔publicOnly,   布尔skipCheckThis,布尔fillCache,StackCrawlMark和stackMark)   +232 System.Activator.CreateInstance(类型,布尔型非公共)+83 System.Activator.CreateInstance(类型类型)+11 System.Web.Mvc.DefaultControllerActivator.Create(RequestContext)   requestContext,类型为controllerType)+55

     

[InvalidOperationException:尝试创建一个   类型为“ WebAppBlog.Controllers.AccountController”的控制器。使   确保控制器具有无参数的公共构造函数。]
  System.Web.Mvc.DefaultControllerActivator.Create(RequestContext   requestContext,类型controllerType)+178
  System.Web.Mvc.DefaultControllerFactory.GetControllerInstance(RequestContext   requestContext,类型controllerType)+76
  System.Web.Mvc.DefaultControllerFactory.CreateController(RequestContext   requestContext,字符串controllerName)+88
  System.Web.Mvc.MvcHandler.ProcessRequestInit(HttpContextBase   httpContext,IController&控制器,IControllerFactory&工厂)   +194 System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext,AsyncCallback回调,对象状态)+50
  System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContext httpContext,   AsyncCallback回调,对象状态)+48
  System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext   上下文,AsyncCallback cb,对象extraData)+16
  System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()   +103 System.Web.HttpApplication.ExecuteStepImpl(IExecutionStep步骤)+48 System.Web.HttpApplication.ExecuteStep(IExecutionStep   步骤,布尔值和已同步完成)+159

1 个答案:

答案 0 :(得分:0)

我怀疑问题出在控制器的大量依赖关系上。其中一个存储库可能具有未在您的容器中注册的依赖项。

我将研究工作单元模式,如果使用Entity Framework,请使用它的dbcontext(实现工作单元模式)。甚至可以更好地抽象到IAccountService并在实现中使用那里的工作单元模式。