这是我的情况: 我有一个非常简单的MVC4应用程序。一个视图,一个控制器,有限的责任。它充当供应商应用程序的登录页面,负责:
我遇到#3问题,但只是因为我已经将Ninject添加到项目中(需要依赖注入以允许编写单元测试)。从那以后,我得到了#34;没有为这个对象定义无参数构造函数"用于HTTP Post控制器操作,仅用于帖子。在这个控制器上GET工作得很好。这个控制器只有一个构造函数,它可以成功调用GET或POST - 添加一个断点我可以逐步调用构造函数,只有在传递完最后一个大括号后才能得到"无参数construtor"错误。
我已尝试过这些StackOverflow答案中的解决方案: https://stackoverflow.com/a/14027722/1139007
https://stackoverflow.com/a/17909609/1139007
https://stackoverflow.com/a/21010851/1139007
我已确保所有绑定都在NinjectWebCommon.cs中正确设置(它们是)。我已经验证(每this answer)Web.Config具有正确的配置。我已经卸载并重新安装了Ninject.MVC3(以及所有依赖项)。我把头发拉了出来。
这是默认构造函数:
public HomeController(IAlerts alertRepository, IStore storeRepository, IMemberRecord memberRepository, IHTTPUserRequest userRequest, IActiveDirectory ad)
{
_alertRepository = alertRepository;
_storeRepository = storeRepository;
_memberRepository = memberRepository;
_userRequest = userRequest;
_ad = ad;
}
这是HttpPost控制器操作:
[HttpPost]
public HttpStatusCodeResult InsertMember(AVMemberRecord profile)
{
bool validStore = _storeRepository.isValidStore(profile.defaultStoreBU);
if (validStore)
{
bool success = _memberRepository.insertMemberRecord(profile);
if (success)
{
return new HttpStatusCodeResult(200);
}
else
{
return new HttpStatusCodeResult(500, "Error inserting Member record. Please open a ticket to request access.");
}
}
else
{
return new HttpStatusCodeResult(500, "Invalid store association for user. Please open a ticket to request access.");
}
}
每个存储库都有一个构造函数,它接受一个IDataAccess实例作为参数:
public StoreRepository(IDataAccess db)
{
_db = db;
stores = db.getStores();
}
绑定到IDataAccess的DataAccess类只有一个无参数构造函数,并使用web.config作为其连接字符串:
public DataAccess()
{
this.connectionString = ConfigurationManager.ConnectionStrings["DEV_ConnectionString"].ToString();
}
NinjectWebCommon.cs中的绑定:
private static void RegisterServices(IKernel kernel)
{
kernel.Bind<IDataAccess>().To<DataAccess>();
kernel.Bind<IActiveDirectory>().To<ActiveDirectory>();
kernel.Bind<IStore>().To<StoreRepository>();
kernel.Bind<IMemberRecord>().To<MemberRepository>();
kernel.Bind<IAlerts>().To<AlertRepository>();
kernel.Bind<IHTTPUserRequest>().To<HTTPUserRequest>();
}
关于我应该在这看什么的任何建议或指导?如果这种情况持续太长时间,我可能只是重构整个项目来剔除Ninject(不幸的是,失去了很多单元测试)。
编辑:评论中请求完整错误,因此我将在此处添加。
[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) +113
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) +66
System.Web.Mvc.DefaultModelBinder.CreateModel(ControllerContext controllerContext, ModelBindingContext bindingContext, Type modelType) +183
System.Web.Mvc.DefaultModelBinder.BindComplexModel(ControllerContext controllerContext, ModelBindingContext bindingContext) +564
System.Web.Mvc.DefaultModelBinder.BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) +416
System.Web.Mvc.ControllerActionInvoker.GetParameterValue(ControllerContext controllerContext, ParameterDescriptor parameterDescriptor) +317
System.Web.Mvc.ControllerActionInvoker.GetParameterValues(ControllerContext controllerContext, ActionDescriptor actionDescriptor) +117
System.Web.Mvc.Async.<>c__DisplayClass25.<BeginInvokeAction>b__1e(AsyncCallback asyncCallback, Object asyncState) +446
System.Web.Mvc.Async.WrappedAsyncResult1.Begin(AsyncCallback callback, Object state, Int32 timeout) +130
System.Web.Mvc.Async.AsyncControllerActionInvoker.BeginInvokeAction(ControllerContext controllerContext, String actionName, AsyncCallback callback, Object state) +302
System.Web.Mvc.<>c__DisplayClass1d.<BeginExecuteCore>b__17(AsyncCallback asyncCallback, Object asyncState) +30
System.Web.Mvc.Async.WrappedAsyncResult
1.Begin(AsyncCallback callback, Object state, Int32 timeout) +130
System.Web.Mvc.Controller.BeginExecuteCore(AsyncCallback callback, Object state) +381
System.Web.Mvc.Async.WrappedAsyncResult1.Begin(AsyncCallback callback, Object state, Int32 timeout) +130
System.Web.Mvc.Controller.BeginExecute(RequestContext requestContext, AsyncCallback callback, Object state) +317
System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.BeginExecute(RequestContext requestContext, AsyncCallback callback, Object state) +15
System.Web.Mvc.<>c__DisplayClass8.<BeginProcessRequest>b__2(AsyncCallback asyncCallback, Object asyncState) +71
System.Web.Mvc.Async.WrappedAsyncResult
1.Begin(AsyncCallback callback, Object state, Int32 timeout) +130
System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, Object state) +249
System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContext httpContext, AsyncCallback callback, Object state) +49
System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData) +16
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +301
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155
答案 0 :(得分:1)
您需要确保所有绑定的具体类都具有构造函数。
在这种情况下,看起来罪魁祸首将是ActiveDirectory
和/或HTTPUserRequest
。
只需向每个构造函数添加一个无参数构造函数,就应该解决错误。即:
public class ActiveDirectory : IActiveDirectory
{
public ActiveDirectory() {}
....
}
当没有具体类的构造函数时,大多数(如果不是全部)DI引擎将抛出此异常,因此这并不罕见。
<强>更新强>
在这种情况下Ninject不是问题,@ ascary的评论下面突出显示AVMemberRecord
作为罪魁祸首类,它被用作Post
行动的参数。在这种情况下会抛出异常,因为Model Binder无法找到一个无参数的构造函数来实例化该类型。