我正在测试Ninject,但按照操作方法,我发现无法使其工作。网络上的信息甚至是相互矛盾的。我正在开发Visual Studio 2012的MVC 4网站,并且我使用Nuget安装了Ninject。Ninject“没有为此对象定义的无参数构造函数”。
所以我得到一个错误:“没有无参数的构造函数为这个对象定义。”。只要我进入我的控制器。
我做了必要的步骤:
- 的NuGet安装
- 在NinjectWebCommon.cs,我做我的注册在RegisterServices方法接口。
在我的HomeController设置我的对象是这样的:
public ISurvey _survey { get; set; } [Inject] public HomeController(ISurvey s) { _survey = s; }
我也得到了以下错误消息:
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, 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) +6 System.Web.Mvc.DefaultControllerActivator.Create(RequestContext requestContext, Type controllerType) +110 [InvalidOperationException: An error occurred when trying to create a controller of type 'Surveys.Controllers.HomeController'. Make sure that the controller has a parameterless public constructor.] System.Web.Mvc.DefaultControllerActivator.Create(RequestContext requestContext, Type controllerType) +247 System.Web.Mvc.DefaultControllerFactory.GetControllerInstance(RequestContext requestContext, Type controllerType) +438 System.Web.Mvc.DefaultControllerFactory.CreateController(RequestContext requestContext, String controllerName) +226 System.Web.Mvc.MvcHandler.ProcessRequestInit(HttpContextBase httpContext, IController& controller, IControllerFactory& factory) +326 System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, Object state) +177 System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContext httpContext, AsyncCallback callback, Object state) +88 System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData) +50 System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +301 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155
所以我不知道我做了什么错? 4小时就可以了,我发现我的web.config文件可能有问题,或者它可能是缺少引用,或者可能是工厂缺失。现在我甚至不知道该怎么做。谢谢你的帮助。
你能添加NinjectWebCommon.cs的内容吗? – treze
你可以添加ISurvey实现的内容吗?顺便说一句,你不需要用Inject属性修饰构造函数,但我认为这对你的问题没有任何影响。 – Hernan