2013-11-21 4 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/)もインストールする必要があります。コントローラはコントローラをインスタンス化し、コントローラのコンストラクタに適切な依存関係を注入します。 Niggetを使ってNinject.MVC3をインストールすると、すべてが自動的に構成されることはほぼ確実です。

このパッケージがないと、MVCはデフォルトのコントローラファクトリを使用します。このコントローラファクトリは、依存関係の注入については何も知らないものです。

0

チェックあなたのWeb.configファイルで使用すると、MVCのバージョン管理のための2つのラインを持っている場合は、私のようなものだった:あなたは、あなたがしている最も高いバージョンにリダイレクトする、のように1行に置き換えるならば

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

これは私のためにそれをした。

関連する問題

 関連する問題