1

我按照本文中的说明使用 Ninject 进行 MVC 4 Web API 控制器构造函数注入:

http://www.peterprovost.org/blog/2012/06/19/adding-ninject-to-web-api/

我面临的问题是,当我调用 api 方法时,我收到错误消息“类型'CarController'没有默认构造函数”。

我什至在 NinjectWebCommon.CreateKernel 设置了断点,以查看是否正在调用它。当应用程序运行时,它会被调用。

我错过了什么吗?

顺便说一句,我从 nuget 安装了 Ninject.Web.Common 和 Ninject 来执行此操作。这是我的代码:

MVC WEB API 控制器:

public class CarController : ApiController
{
    private ICarService carService;

    public CarController(ICarService carService)
    {
        this.carService = carService;
    }

    [AcceptVerbs("GET")]
    public CarsResponse GetCars([FromUri] CarsRequest request)
    {         
        return this.carService.GetCars(request);
    }
}

在 App_Start 中:

public class NinjectDependencyScope : IDependencyScope
{
    IResolutionRoot resolver;

    public NinjectDependencyScope(IResolutionRoot resolver)
    {
        this.resolver = resolver;
    }

    public object GetService(Type serviceType)
    {
        if (resolver == null)
            throw new ObjectDisposedException("this", "This scope has been disposed");

        return resolver.TryGet(serviceType);
    }

    public System.Collections.Generic.IEnumerable<object> GetServices(Type serviceType)
    {
        if (resolver == null)
            throw new ObjectDisposedException("this", "This scope has been disposed");

        return resolver.GetAll(serviceType);
    }

    public void Dispose()
    {
        IDisposable disposable = resolver as IDisposable;
        if (disposable != null)
            disposable.Dispose();

        resolver = null;
    }
}


public class NinjectDependencyResolver : NinjectDependencyScope, IDependencyResolver
{
    IKernel kernel;

    public NinjectDependencyResolver(IKernel kernel)
        : base(kernel)
    {
        this.kernel = kernel;
    }

    public IDependencyScope BeginScope()
    {
        return new NinjectDependencyScope(kernel.BeginBlock());
    }
}

我的 NinjectWebCommon 看起来像这样:

public static class NinjectWebCommon 
{
    private static readonly Bootstrapper bootstrapper = new Bootstrapper();

    public static void Start() 
    {
        DynamicModuleUtility.RegisterModule(typeof(OnePerRequestHttpModule));
        DynamicModuleUtility.RegisterModule(typeof(NinjectHttpModule));
        bootstrapper.Initialize(CreateKernel);
    }

    public static void Stop()
    {
        bootstrapper.ShutDown();
    }

    private static IKernel CreateKernel()
    {
        var kernel = new StandardKernel();
        kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
        kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();

        RegisterServices(kernel);

        GlobalConfiguration.Configuration.DependencyResolver = new NinjectDependencyResolver(kernel);

        return kernel;
    }

    private static void RegisterServices(IKernel kernel)
    {
        kernel.Bind<ICarService>().To<CarService>();
    }        
}
4

1 回答 1

1

我遇到了同样的问题。我通过执行以下操作找到了解决方案。我忘记了找到 NinjectMVCDependencyResolver 类的网页。

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

        RegisterServices(kernel);
        // Install our Ninject-based IDependencyResolver into the Web API config
        GlobalConfiguration.Configuration.DependencyResolver 
            = new NinjectDependencyResolver(kernel);
        // Install into the MVC dependency resolver
        System.Web.Mvc.DependencyResolver.SetResolver(
            new NinjectMVCDependencyResolver(kernel));
        return kernel;
    }

public class NinjectMVCDependencyResolver : NinjectDependencyScope
                                            , System.Web.Mvc.IDependencyResolver
{
    private IKernel kernel;

    public NinjectMVCDependencyResolver(IKernel kernel)
        : base(kernel)
    {
        this.kernel = kernel;
    }

    public IDependencyScope BeginScope()
    {
        return new NinjectDependencyScope(kernel.BeginBlock());
    }
}
于 2013-07-27T01:19:18.807 回答