Cannot create an instance of an interface

17,747

Solution 1

Wow, ran into exactly the same error today (but not sure if that was the same module)!

It's a bug in one of the view models. Someone had put an interface there instead of class one (eg. IUser instead of UserPartRecord). Model binder shouts because it cannot create an instance of an object to bind form parameter(s) to.

Your only option is to change the type of that parameter (which one - you have to find out) to a concrete type. The quickest way would be to create a simple class implementing that interface and replace the parameter type with it.

EDIT: A similar problem was discussed (and solved) here and here.

Solution 2

I started getting this exception in an MVC ASP.Net 4.6 application. It took me some time, but I finally realized that I had change my MVC Controller API call from

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

To

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

I was avoiding the multiple enumeration of an IEnumerable that R# warns about. Lesson Learned: Don't use IReadOnlyList in this instance.

Here's the full exception:

[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
Share:
17,747
rspaulino
Author by

rspaulino

Updated on July 06, 2022

Comments

  • rspaulino
    rspaulino almost 2 years

    after install profile 1.1, add at least one field to the User Content Types then go to the Users module and click on Add a new user, click save and you should get this error. any help would be appreciate.

    Server Error in '/OrchardLocal' Application.

    Cannot create an instance of an interface. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    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


    Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.237

  • Ryan Penfold
    Ryan Penfold about 10 years
    It seems that an interface is allowed as the type for the @model directive in views, however this error is raised if an interface is the type of a parameter in controller methods.
  • Adrian Iftode
    Adrian Iftode over 5 years
    Got into that myself too. You could use IList or ICollection
  • Serj Sagan
    Serj Sagan about 5 years
    This doesn't really solve the problem being discussed here... where the item is trying to be resolved in the method, not the constructor