2016-10-08 9 views
2

私はAsp.Net MVC 4アプリケーションを持っていますが、単純なインジェクタを使用しています。注入が必要です)は、このオブジェクトに対して "パラメータが定義されていないコンストラクタはありません"と表示されます。それは注射が機能していないことを意味しますが、私はすべてがうまく構成されていると思います。単純なインジェクタを持つMVCのモデルバインディングを持つパラメータのないコンストラクタなし

SimpleInjectorInitializerクラス:

SimpleInjector SimpleInjector.Integration.Web SimpleInjector:

[assembly: WebActivator.PostApplicationStartMethod(typeof(SimpleInjectorInitializer), "Initialize")] 
    public static class SimpleInjectorInitializer 
    { 
     public static void Initialize() 
     { 
      var container = new Container(); 
      container.Options.DefaultScopedLifestyle = new WebRequestLifestyle(); 

      InitializeContainer(container); 
      container.RegisterMvcControllers(Assembly.GetExecutingAssembly()); 
      container.Verify(); 

      DependencyResolver.SetResolver(new SimpleInjectorDependencyResolver(container)); 
     } 

    private static void InitializeContainer(Container container) 
    { 
     SimpleInjectorContainer.RegisterContainer(container); 
    } 
} 

Containerクラス(どこの依存関係を登録する)

public static class SimpleInjectorContainer 
    { 
     public static void RegisterContainer(Container container) 
     { 
      container.RegisterPerWebRequest<IPessoaRepository, PessoaRepository>(); 
     } 

    } 

私はすでにアセンブリを持っています。 Integration.Web.Mvc

のStackTrace

MissingMethodException: Nenhum construtor sem parâmetros foi definido para este objeto.] 
    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) +114 
    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) +11 
    System.Web.Mvc.DefaultModelBinder.CreateModel(ControllerContext controllerContext, ModelBindingContext bindingContext, Type modelType) +197 

[MissingMethodException: Nenhum construtor sem parâmetros foi definido para este objeto. Tipo de objeto 'Cerocha.Domain.Entities.Pessoa'.] 
    System.Web.Mvc.DefaultModelBinder.CreateModel(ControllerContext controllerContext, ModelBindingContext bindingContext, Type modelType) +233 
    System.Web.Mvc.DefaultModelBinder.BindComplexModel(ControllerContext controllerContext, ModelBindingContext bindingContext) +531 
    System.Web.Mvc.DefaultModelBinder.BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) +330 
    System.Web.Mvc.ControllerActionInvoker.GetParameterValue(ControllerContext controllerContext, ParameterDescriptor parameterDescriptor) +330 
    System.Web.Mvc.ControllerActionInvoker.GetParameterValues(ControllerContext controllerContext, ActionDescriptor actionDescriptor) +105 
    System.Web.Mvc.Async.<>c__DisplayClass21.<BeginInvokeAction>b__19(AsyncCallback asyncCallback, Object asyncState) +743 
    System.Web.Mvc.Async.WrappedAsyncResult`1.CallBeginDelegate(AsyncCallback callback, Object callbackState) +14 
    System.Web.Mvc.Async.WrappedAsyncResultBase`1.Begin(AsyncCallback callback, Object state, Int32 timeout) +128 
    System.Web.Mvc.Async.AsyncControllerActionInvoker.BeginInvokeAction(ControllerContext controllerContext, String actionName, AsyncCallback callback, Object state) +343 
    System.Web.Mvc.Controller.<BeginExecuteCore>b__1c(AsyncCallback asyncCallback, Object asyncState, ExecuteCoreState innerState) +25 
    System.Web.Mvc.Async.WrappedAsyncVoid`1.CallBeginDelegate(AsyncCallback callback, Object callbackState) +30 
    System.Web.Mvc.Async.WrappedAsyncResultBase`1.Begin(AsyncCallback callback, Object state, Int32 timeout) +128 
    System.Web.Mvc.Controller.BeginExecuteCore(AsyncCallback callback, Object state) +465 
    System.Web.Mvc.Controller.<BeginExecute>b__14(AsyncCallback asyncCallback, Object callbackState, Controller controller) +18 
    System.Web.Mvc.Async.WrappedAsyncVoid`1.CallBeginDelegate(AsyncCallback callback, Object callbackState) +20 
    System.Web.Mvc.Async.WrappedAsyncResultBase`1.Begin(AsyncCallback callback, Object state, Int32 timeout) +128 
    System.Web.Mvc.Controller.BeginExecute(RequestContext requestContext, AsyncCallback callback, Object state) +374 
    System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.BeginExecute(RequestContext requestContext, AsyncCallback callback, Object state) +16 
    System.Web.Mvc.MvcHandler.<BeginProcessRequest>b__4(AsyncCallback asyncCallback, Object asyncState, ProcessRequestState innerState) +52 
    System.Web.Mvc.Async.WrappedAsyncVoid`1.CallBeginDelegate(AsyncCallback callback, Object callbackState) +30 
    System.Web.Mvc.Async.WrappedAsyncResultBase`1.Begin(AsyncCallback callback, Object state, Int32 timeout) +128 
    System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, Object state) +384 
    System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContext httpContext, AsyncCallback callback, Object state) +48 
    System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData) +16 
    System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +103 
    System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155 
+0

スタックトレース私の友人、スタックトレース。スタックトレースを投稿してください。そのような情報がなければ、私たちができることは推測です。それは本当にあなたを助けません。 – Steven

+0

@ user3670112、エラーを出すコントローラを表示する – Nkosi

答えて

2

あなたはあなたのビューモデルオブジェクトに依存関係を注入しようとしています。これは、デフォルトではMVCでサポートされておらず、ランタイムデータと不確定性を混在させることは悪い考えであるため、Simple Injector統合パッケージではサポートされていません。

あなたのビューモデルには何も動作してはいけません。データとビヘイビアを分離したら、ビューモデルに依存関係を注入する必要はなく、問題は消えてしまいます。

関連する問題