Ninject error in WebAPI 2.1 - Make sure that the controller has a parameterless public constructor

后端 未结 10 2048
心在旅途
心在旅途 2020-12-31 00:39

I have the following packages and their dependencies installed in my WebAPI project:

Ninject.Web.WebApi Ninject.Web.WebApi.OwinHost

10条回答
  •  离开以前
    2020-12-31 01:06

    For me, what caused this error was that for the interface I was passing into the constructor, I had a named binding and I called it with the wrong name. As you can see the below, the binding says it's named "Warehouse", but I got things mixed up and put "DataWarehouse" in the constructor. Getting this straightened out caused the error about a parameterless constructor to go away.

    private static void RegisterServices(IKernel kernel)
        {
            kernel.Bind().To().InThreadScope();
            kernel.Bind().To().InThreadScope();
            kernel.Bind().To().Named("Warehouse").WithConstructorArgument("connectionString", ConfigurationManager.ConnectionStrings["DataWarehouse"].ConnectionString);
        }
    

    constructor:

    //WRONG NAME
    public VisitProcessor([Named("DataWarehouse")] IDbConnectionFactory connection)
        {
            _database = connection;
        }
    

提交回复
热议问题