Dependency injection Ninject、WebAPI 2依赖项注入不工作

Dependency injection Ninject、WebAPI 2依赖项注入不工作,dependency-injection,ninject,asp.net-web-api2,Dependency Injection,Ninject,Asp.net Web Api2,我正在尝试将Ninject集成到我的WebAPI 2项目中,但出现以下错误: { "message": "An error has occurred.", "exceptionMessage": "An error occurred when trying to create a controller of type 'BrandController'. Make sure that the controller has a parameterless public constr

我正在尝试将Ninject集成到我的WebAPI 2项目中,但出现以下错误:

{
    "message": "An error has occurred.",
    "exceptionMessage": "An error occurred when trying to create a controller of type 'BrandController'. Make sure that the controller has a parameterless public constructor.",
    "exceptionType": "System.InvalidOperationException",
    "stackTrace": "   at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType)\r\n   at System.Web.Http.Controllers.HttpControllerDescriptor.CreateController(HttpRequestMessage request)\r\n   at System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__1.MoveNext()",
    "innerException": {
        "message": "An error has occurred.",
        "exceptionMessage": "Type 'ADAS.GoTango.WebApi.Controllers.BrandController' does not have a default constructor",
        "exceptionType": "System.ArgumentException",
        "stackTrace": "   at System.Linq.Expressions.Expression.New(Type type)\r\n   at System.Web.Http.Internal.TypeActivator.Create[TBase](Type instanceType)\r\n   at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.GetInstanceOrActivator(HttpRequestMessage request, Type controllerType, Func`1& activator)\r\n   at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType)"
    }
}
而NinjectWebCommon.cs文件是

/// <summary>
/// Creates the kernel that will manage your application.
/// </summary>
/// <returns>The created kernel.</returns>
private static IKernel CreateKernel()
{
    var kernel = new StandardKernel();
    try
    {
        kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
        kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();
        GlobalConfiguration.Configuration.DependencyResolver = new NinjectDependencyResolver(kernel);

        RegisterServices(kernel);
        return kernel;
    }
    catch
    {
        kernel.Dispose();
        throw;
    }
}

/// <summary>
/// Load your modules or register your services here!
/// </summary>
/// <param name="kernel">The kernel.</param>
private static void RegisterServices(IKernel kernel)
{
    var configuration = new HttpConfiguration();
    kernel.Bind<DefaultModelValidatorProviders>().ToConstant(new DefaultModelValidatorProviders(configuration.Services.GetServices(typeof(ModelValidatorProvider)).Cast<ModelValidatorProvider>()));
    kernel.Bind<BrandsBusiness>().ToSelf().InRequestScope();
    kernel.Bind<IBrandManagement>().To<BrandEfStore>().InRequestScope();
}    
//
///创建将管理应用程序的内核。
/// 
///创建的内核。
私有静态IKernel CreateKernel()
{
var kernel=新的标准内核();
尝试
{
kernel.Bind().ToMethod(ctx=>()=>newbootstrapper().kernel);
kernel.Bind().To();
GlobalConfiguration.Configuration.DependencyResolver=新的NinjectDependencyResolver(内核);
注册服务(内核);
返回内核;
}
抓住
{
Dispose();
投掷;
}
}
/// 
///在这里加载您的模块或注册您的服务!
/// 
///内核。
私有静态无效注册服务(IKernel内核)
{
var配置=新的HttpConfiguration();
kernel.Bind().ToConstant(新的DefaultModelValidatorProviders(configuration.Services.GetServices(typeof(ModelValidatorProvider)).Cast());
kernel.Bind().ToSelf().InRequestScope();
kernel.Bind().To().InRequestScope();
}    
我已经试过了:


但是它们都不起作用。

虽然我不知道Ninject,但我认为您需要确保调用了
CreateKernel
方法。您通常会在
global.asax
中添加一个
Application\u Start
方法


您可能需要使
CreateKernel
方法
internal
public
,以便能够从那里调用由ninject处理的if。

。这是私人的。此类也是由Ninject生成的。
public class BrandController : BaseApiController
{
    readonly BrandsBusiness _brandsBusiness;

    public BrandController(BrandsBusiness brandsBusiness)
    {
        _brandsBusiness = brandsBusiness;
    }

    //public BrandController()
    //{
    //    _brandsBusiness = new BrandsBusiness(new BrandEfStore());
    //}

    public IHttpActionResult Get()
    {
        try
        {
            var allActiveBrands = _brandsBusiness.GetAllActiveBrands();
            return Ok(allActiveBrands);
        }
        catch (Exception exception)
        {
            Logger.Error(exception);
            return InternalServerError();
        }
    }

}
/// <summary>
/// Creates the kernel that will manage your application.
/// </summary>
/// <returns>The created kernel.</returns>
private static IKernel CreateKernel()
{
    var kernel = new StandardKernel();
    try
    {
        kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
        kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();
        GlobalConfiguration.Configuration.DependencyResolver = new NinjectDependencyResolver(kernel);

        RegisterServices(kernel);
        return kernel;
    }
    catch
    {
        kernel.Dispose();
        throw;
    }
}

/// <summary>
/// Load your modules or register your services here!
/// </summary>
/// <param name="kernel">The kernel.</param>
private static void RegisterServices(IKernel kernel)
{
    var configuration = new HttpConfiguration();
    kernel.Bind<DefaultModelValidatorProviders>().ToConstant(new DefaultModelValidatorProviders(configuration.Services.GetServices(typeof(ModelValidatorProvider)).Cast<ModelValidatorProvider>()));
    kernel.Bind<BrandsBusiness>().ToSelf().InRequestScope();
    kernel.Bind<IBrandManagement>().To<BrandEfStore>().InRequestScope();
}