无法创建接口的实例

时间:2011-12-13 14:41:14

标签: asp.net asp.net-mvc asp.net-mvc-3 model-binding orchardcms

安装配置文件1.1后,在用户内容类型中添加至少一个字段,然后转到“用户”模块并单击“添加新用户”,单击“保存”,您将收到此错误。任何帮助都会很感激。

服务器错误。

无法创建接口的实例。 描述:执行当前Web请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。

Exception Details: System.MissingMethodException: Cannot create an instance of an interface.

Source Error: 


Line 146: public void EndProcessRequest(IAsyncResult result) {
Line 147: try {
Line 148: _httpAsyncHandler.EndProcessRequest(result);
Line 149: }
Line 150: finally {


Source File: C:\Users\rspaulino\Desktop\src\Orchard\Mvc\Routes\ShellRoute.cs Line: 148 

Stack Trace: 


    [MissingMethodException: Cannot create an instance of an interface.]
    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) +98
    System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipVisibilityChecks, Boolean skipCheckThis, Boolean fillCache) +241
    System.Activator.CreateInstance(Type type, Boolean nonPublic) +69
    System.Web.Mvc.DefaultModelBinder.CreateModel(ControllerContext controllerContext, ModelBindingContext bindingContext, Type modelType) +199
    System.Web.Mvc.DefaultModelBinder.BindComplexModel(ControllerContext controllerContext, ModelBindingContext bindingContext) +572
    System.Web.Mvc.DefaultModelBinder.BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) +449
    System.Web.Mvc.DefaultModelBinder.GetPropertyValue(ControllerContext controllerContext, ModelBindingContext bindingContext, PropertyDescriptor propertyDescriptor, IModelBinder propertyBinder) +17
    System.Web.Mvc.DefaultModelBinder.BindProperty(ControllerContext controllerContext, ModelBindingContext bindingContext, PropertyDescriptor propertyDescriptor) +399
    System.Web.Mvc.DefaultModelBinder.BindProperties(ControllerContext controllerContext, ModelBindingContext bindingContext) +93
    System.Web.Mvc.DefaultModelBinder.BindComplexElementalModel(ControllerContext controllerContext, ModelBindingContext bindingContext, Object model) +53
    System.Web.Mvc.DefaultModelBinder.BindComplexModel(ControllerContext controllerContext, ModelBindingContext bindingContext) +1367
    System.Web.Mvc.DefaultModelBinder.BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) +449
    System.Web.Mvc.ControllerActionInvoker.GetParameterValue(ControllerContext controllerContext, ParameterDescriptor parameterDescriptor) +317
    System.Web.Mvc.ControllerActionInvoker.GetParameterValues(ControllerContext controllerContext, ActionDescriptor actionDescriptor) +117
    System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +343
    System.Web.Mvc.Controller.ExecuteCore() +116
    System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) +97
    System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext) +10
    System.Web.Mvc.<>c__DisplayClassb.<BeginProcessRequest>b__5() +37
    System.Web.Mvc.Async.<>c__DisplayClass1.<MakeVoidDelegate>b__0() +21
    System.Web.Mvc.Async.<>c__DisplayClass8`1.<BeginSynchronous>b__7(IAsyncResult _) +12
    System.Web.Mvc.Async.WrappedAsyncResult`1.End() +62
    System.Web.Mvc.<>c__DisplayClasse.<EndProcessRequest>b__d() +50
    System.Web.Mvc.SecurityUtil.<GetCallInAppTrustThunk>b__0(Action f) +7
    System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust(Action action) +22
    System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +60
    System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +9
    Orchard.Mvc.Routes.HttpAsyncHandler.EndProcessRequest(IAsyncResult result) in C:\Users\rspaulino\Desktop\src\Orchard\Mvc\Routes\ShellRoute.cs:148
    System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +8963149
    System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +

184


版本信息:Microsoft .NET Framework版本:4.0.30319; ASP.NET版本:4.0.30319.237

3 个答案:

答案 0 :(得分:12)

哇,今天碰到了完全相同的错误(但不确定那是不是同一个模块)!

这是其中一个视图模型中的错误。有人把接口放在而不是类一个(例如IUser而不是UserPartRecord)。模型绑定器大喊,因为它无法创建将表单参数绑定到的对象实例。

您唯一的选择是将该参数的类型(您必须找到哪一个)更改为具体类型。最快的方法是创建一个实现该接口的简单类,并用它替换参数类型。

编辑:讨论(并解决了)herehere类似的问题。

答案 1 :(得分:2)

我开始在MVC ASP.Net 4.6应用程序中获得此异常。我花了一些时间,但我终于意识到我已经从

更改了我的MVC Controller API调用
[HttpPost]
public async Task<ActionResult> DoStuff(IEnumerable<string> ids){ ... }

[HttpPost]
public async Task<ActionResult> DoStuff(IReadOnlyList<string> ids){ ... }

我避免了R#警告的IEnumerable的多次枚举。经验教训:在这种情况下不要使用IReadOnlyList。

以下是完整的例外情况:

[MissingMethodException: Cannot create an instance of an interface.]
System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean&amp; canBeCached, RuntimeMethodHandleInternal&amp; ctor, Boolean&amp; bNeedSecurityCheck) +0
System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark&amp; stackMark) +119
System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark&amp; stackMark) +247
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

答案 2 :(得分:-1)

我遇到与(MVC 5)+(Ninject3.2)完全相同的问题。整个DI准备是非常典型和标准的,只是不能使它工作。最后,我这样做了:

  1. 为您的控制器创建默认CTOR;
  2. 在CTOR中,添加如下所示的行:

    repository=DependencyResolver.Current.GetService(typeof(IYourinterface)) as IYourinterface;
    
  3. 我知道这只是一种解决方法,但我认为MVC 5中有关控制器对象实例化的更改。稍后会把它挖出来。