2013-11-21 67 views
5

我正在测试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文件可能有问题,或者它可能是缺少引用,或者可能是工厂缺失。现在我甚至不知道该怎么做。谢谢你的帮助。

+1

你能添加NinjectWebCommon.cs的内容吗? – treze

+0

你可以添加ISurvey实现的内容吗?顺便说一句,你不需要用Inject属性修饰构造函数,但我认为这对你的问题没有任何影响。 – Hernan

回答

1

您还必须安装包含Ninject控制器工厂的Ninject.MVC3(https://www.nuget.org/packages/Ninject.MVC3/),该工厂负责实例化控制器并向控制器构造函数注入正确的依赖关系。我几乎可以肯定,如果你通过Nuget安装Ninject.MVC3,所有的东西都会自动配置。

没有这个包,MVC使用它的默认控制器工厂,对注入依赖关系一无所知。

0

检查是否在你的web.config你有两行MVC版本,我有这样的事情:

<dependentAssembly> 
    <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" /> 
    <bindingRedirect oldVersion="0.0.0.0-4.0.0.0" newVersion="4.0.0.0" /> 
    </dependentAssembly> 
<dependentAssembly> 
    <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" /> 
    <bindingRedirect oldVersion="4.0.0.0-4.0.0.1" newVersion="4.0.0.1" /> 
</dependentAssembly> 

如果再用这样一行取代他们,重定向到,你是最高版本在我的情况下使用它的4.0.0.1

<dependentAssembly> 
    <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" /> 
    <bindingRedirect oldVersion="0.0.0.0-4.0.0.1" newVersion="4.0.0.1" /> 
    </dependentAssembly> 

这是为我做的。

相关问题