Getting "MissingMethodException: Cannot create an instance of an interface" when binding generic interface to repository with Ninject
Asked Answered
G

2

6

Following the guide here, but instead of StructureMap attempting to use Ninject.

It throws up the "MissingMethodException: Cannot create an instance of an interface" error any time I attempt to inject an IRepository<SomeEntityType> into a parameter in an action method.

Update: Also giving bootstrapper.cs not found, I used the MVC3 Ninject Nuget package.

 public ActionResult Index(IRepository<SomeEntityType> repo)
        {


            return View();
        }

NinjectWebCommon.cs

        private static void RegisterServices(IKernel kernel)
    {
        string Cname = "VeraDB";
        IDbContext context = new VeraContext("VeraDB");
        kernel.Bind<IDbContext>().To<VeraContext>().InRequestScope().WithConstructorArgument("ConnectionStringName", Cname);
        kernel.Bind(typeof(IRepository<>)).To(typeof(EFRepository<>)).WithConstructorArgument("context",context);

    }      

IRepository

    public interface IRepository<T> where T : class
{
    void DeleteOnSubmit(T entity);
    IQueryable<T> GetAll();
    T GetById(object id);
    void SaveOrUpdate(T entity);
}

EFRepository

    public class EFRepository<T> : IRepository<T> where T : class, IEntity
{
    protected readonly IDbContext context;
    protected readonly IDbSet<T> entities;

    public EFRepository(IDbContext context)
    {
        this.context = context;
        entities = context.Set<T>();
    }

    public virtual T GetById(object id)
    {
        return entities.Find(id);
    }

    public virtual IQueryable<T> GetAll()
    {
        return entities;
    }

    public virtual void SaveOrUpdate(T entity)
    {
        if (entities.Find(entity.Id) == null)
        {
            entities.Add(entity);
        }

        context.SaveChanges();
    }

    public virtual void DeleteOnSubmit(T entity)
    {
        entities.Remove(entity);

        context.SaveChanges();
    }
}

IEntity just acts as a generic constraint.

   public interface IEntity
{
    Guid Id { get; set; }
}
Gammon answered 8/4, 2013 at 14:28 Comment(4)
Where is the line of code that triggers the exception?Serdab
@KirkWoll As stated the said error will come up anytime I attempt pass an IRepository<SomeEntityType> typed parameter in an action method etc. Now clearly because Ninject isn't configured properly and nothing is binding. Perhaps something to do with the MVC3 Nuget package not playing well with MVC4.Gammon
You haven't answered my question. Please show that line of code.Serdab
Please add a more detailed stack trace (exact exception and salient details)Peculium
E
18

I made the same simple mistake. Ninject injects parameters into your constructor, but you added parameters to the Index Controller action.

It should look like this:

public class HomeController : Controller
{
    private IRepository<SomeEntityType> _repo;

    public HomeController(IRepository<SomeEntityType> repo)
    {
        _repo= repo;
    }

    public ActionResult Index()
    {
        ViewBag.Message = "Modify this template to jump-start your ASP.NET MVC application. " +
                          _repo.HelloWorld();

        return View();
    }
}

Make sense?

Equipotential answered 16/10, 2013 at 20:52 Comment(3)
Excactly the same mistake I did :)Aron
Me too, exactly the same thing!!Echinoderm
And...me too ;) Thanx!Biddable
C
1

That sort of error usually indicates that you have a different version of the dll at runtime to the one you are referencing in the project.

Try just manually copying all the relevant dlls from your project dirs to your bin directory.

Failing that, check this (admittedly, very old) post for some ideas on how to debug the problem.

Cabot answered 8/4, 2013 at 14:48 Comment(1)
This seems the most likely, esp as we've yet to have the stacktrace or full message shared with us.Peculium

© 2022 - 2024 — McMap. All rights reserved.