2013-11-21 65 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> 

這是爲我做的。

相關問題