Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/340.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用简单注入器的MVC中没有带模型绑定的无参数构造函数_C#_Asp.net Mvc_Dependency Injection_Simple Injector - Fatal编程技术网

C# 使用简单注入器的MVC中没有带模型绑定的无参数构造函数

C# 使用简单注入器的MVC中没有带模型绑定的无参数构造函数,c#,asp.net-mvc,dependency-injection,simple-injector,C#,Asp.net Mvc,Dependency Injection,Simple Injector,我有一个Asp.NETMVC4应用程序,我使用的是简单的注入器,我配置了应用程序启动时的所有内容,但当我尝试发布一个示例表单(当系统需要注入器时)时,该对象显示“未定义构造函数参数”。这意味着注入不起作用,但我认为所有配置都很好 SimpleInjector初始值设定项类: [assembly: WebActivator.PostApplicationStartMethod(typeof(SimpleInjectorInitializer), "Initialize")] public

我有一个Asp.NETMVC4应用程序,我使用的是简单的注入器,我配置了应用程序启动时的所有内容,但当我尝试发布一个示例表单(当系统需要注入器时)时,该对象显示“未定义构造函数参数”。这意味着注入不起作用,但我认为所有配置都很好

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);
    }
}
容器类(其中注册我的依赖项)

公共静态类SimpleInjector容器
{
公共静态无效注册表容器(容器)
{
container.registerWebRequest();
}
}
我已经有了以下程序集:

简单喷射器 SimpleInjector.Integration.Web SimpleInjector.Integration.Web.Mvc

堆栈跟踪

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
<代码> MISSION MEOXDEXCENT: NENUM结构SEM胡尔达。 System.RuntimeTypeHandle.CreateInstance(RuntimeType类型、Boolean publicOnly、Boolean noCheck、Boolean&canBeCached、RuntimeMethodHandleInternal&ctor、Boolean&bNeedSecurityCheck)+0 System.RuntimeType.CreateInstanceSlow(布尔publicOnly、布尔skipCheckThis、布尔fillCache、StackCrawlMark和stackMark)+114 System.RuntimeType.CreateInstanceDefaultCtor(布尔publicOnly、布尔skipCheckThis、布尔fillCache、StackScrawlMark和stackMark)+232 System.Activator.CreateInstance(类型,布尔非公共)+83 System.Activator.CreateInstance(类型)+11 CreateModel(ControllerContext ControllerContext,ModelBindingContext bindingContext,Type modelType)+197 [ Nex胡MexOutExp:NeHunm Curror SEM Pal.PATIOFOI DECORIDO PARESTEAO对象TIPO de Objeto 'CeloCho.Value.Studio.PasoOa '. 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__显示类21.b__19(AsyncCallback AsyncCallback,Object asyncState)+743 System.Web.Mvc.Async.WrappedAsyncResult`1.CallBeginDelegate(AsyncCallback回调,对象callbackState)+14 System.Web.Mvc.Async.WrappedAsyncResultBase`1.Begin(异步回调,对象状态,Int32超时)+128 System.Web.Mvc.Async.AsyncControllerActionInvoker.BeginInvokeAction(ControllerContext ControllerContext,String actionName,AsyncCallback回调,对象状态)+343 System.Web.Mvc.Controller.b_uu1c(AsyncCallback-AsyncCallback,Object-asyncState,executeRecreat-innerState)+25 System.Web.Mvc.Async.WrappedAsyncVoid`1.CallBeginDelegate(AsyncCallback回调,对象callbackState)+30 System.Web.Mvc.Async.WrappedAsyncResultBase`1.Begin(异步回调,对象状态,Int32超时)+128 System.Web.Mvc.Controller.BeginExecuteCore(异步回调,对象状态)+465 System.Web.Mvc.Controller.b_uu14(异步回调、异步回调、对象回调、控制器控制器)+18 System.Web.Mvc.Async.WrappedAsyncVoid`1.CallBeginDelegate(AsyncCallback回调,对象callbackState)+20 System.Web.Mvc.Async.WrappedAsyncResultBase`1.Begin(异步回调,对象状态,Int32超时)+128 System.Web.Mvc.Controller.BeginExecute(RequestContext RequestContext,AsyncCallback回调,对象状态)+374 System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.BeginExecute(RequestContext RequestContext,AsyncCallback回调,对象状态)+16 System.Web.Mvc.MvcHandler.b_uu4(AsyncCallback AsyncCallback,Object asyncState,ProcessRequestState innerState)+52 System.Web.Mvc.Async.WrappedAsyncVoid`1.CallBeginDelegate(AsyncCallback回调,对象callbackState)+30 System.Web.Mvc.Async.WrappedAsyncResultBase`1.Begin(异步回调,对象状态,Int32超时)+128 System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext,异步回调,对象状态)+384 System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContext HttpContext,异步回调,对象状态)+48 System.Web.Mvc.MvcHandler.System.Web.IHTTPassynchandler.BeginProcessRequest(HttpContext上下文,AsyncCallback cb,Object extraData)+16 System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()+103 System.Web.HttpApplication.ExecuteStep(IExecutionStep步骤,布尔值&同步完成)+155
您正试图将依赖项注入到视图模型对象中。MVC默认不支持这一点,Simple Injector集成包也不支持这一点,因为将运行时数据与依赖项混合是一个坏主意


视图模型不应包含任何行为。一旦您分离了数据和行为,就不需要将依赖项注入到视图模型中,您的问题就会出现

堆栈跟踪我的朋友,堆栈跟踪。请张贴堆栈跟踪。如果没有这些信息,我们所能做的就是猜测,这对您没有帮助。@user3670112,显示给出错误的控制器
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