Getting “MissingMethodException: Cannot create an instance of an interface” when binding generic interface to repository with Ninject

霸气de小男生 提交于 2019-12-10 15:02:28

问题


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; }
}

回答1:


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?




回答2:


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.



来源:https://stackoverflow.com/questions/15881762/getting-missingmethodexception-cannot-create-an-instance-of-an-interface-when

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!