无法创建接口的实例

7

安装1.1版本的配置文件后,至少向用户内容类型中添加一个字段,然后转到用户模块并点击“添加新用户”,保存后您应该会收到以下错误信息。如有需要,请寻求帮助。

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

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

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


你尝试联系那个模块的作者了吗?这看起来像是一个错误报告,而不是一个问题。 - Bertrand Le Roy
是的,我确实尝试过,但并没有得到任何支持。 - rspaulino
那很糟糕。我想你唯一的选择就是自己修复他们的代码了。 - Bertrand Le Roy
3个回答

15

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

这是一个视图模型中的bug。有人把一个接口放在那里,而不是(例如IUser而不是UserPartRecord)。模型绑定器报错,因为它无法创建要绑定表单参数的对象实例。

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

编辑:类似的问题已在这里这里讨论并解决。


在视图中,@model指令允许使用接口作为类型,但是如果控制器方法的参数是接口类型,则会引发此错误。 - Ryan Penfold

4

我在一个MVC ASP.Net 4.6应用程序中遇到了这个异常。花费了一些时间,但是我最终意识到我已经改变了我的MVC控制器API调用,从

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

To

[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。 - Adrian Iftode

-1

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

  1. 为您的控制器创建一个默认 CTOR;
  2. 在 CTOR 中添加以下代码:

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

我知道这只是一个权宜之计,但我认为在MVC 5中控制器对象实例化方面已经发生了一些变化。稍后会深入研究。


这并没有真正解决这里讨论的问题...其中的项正在尝试在方法中解决,而不是构造函数中。 - Serj Sagan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接