【问题标题】:Cannot create an instance of an interface无法创建接口的实例
【发布时间】:2012-01-19 10:23:40
【问题描述】:

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

“/OrchardLocal”应用程序中的服务器错误。

无法创建接口的实例。 说明:执行当前 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

【问题讨论】:

  • 您是否尝试联系该模块的作者?这看起来像是错误报告,而不是问题。
  • 是的,但没有得到任何类型的支持。
  • 这太糟糕了。好吧,我想你唯一的选择就是自己修复他们的代码。

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


【解决方案1】:

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

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

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

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

【讨论】:

  • 似乎允许接口作为视图中@model指令的类型,但是如果接口是控制器方法中参数的类型,则会引发此错误。
【解决方案2】:

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

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

    repository=DependencyResolver.Current.GetService(typeof(IYourinterface)) as IYourinterface;
    

我知道这只是一种解决方法,但我认为 MVC 5 中关于控制器对象实例化的某些内容已经改变。以后会挖出来的。

【讨论】:

  • 这并不能真正解决这里正在讨论的问题......其中项目试图在方法中解决,而不是构造函数
【解决方案3】:

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

更改了我的 MVC 控制器 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

【讨论】:

  • 我自己也参与其中。你可以使用 IList 或 ICollection
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多