错误Asp.Net Mvc和带有Ninject Ioc容器的WebApi2 - 缺少无参数构造函数

时间:2016-02-18 14:14:58

标签: c# asp.net-mvc asp.net-web-api ninject

我尝试在同一个项目中使用mvc和WebApi设置Asp.Net网络应用程序,但我遇到了麻烦。

当我运行网络应用程序时,它工作得很好,我的所有测试都没问题。

但是,当尝试使用我的WebApi时,它确实无效。

我使用Ninject作为我的Ioc容器,并且我已经安装了Ninject.Mvc5和Ninject.WebApi包。

返回的错误是控制器工厂找不到无参数构造函数,所以我知道Ninject映射失败了。

这是我的代码。

这是我的mvc控制器

public class InventarioController : Controller
{
    private readonly IInventarioAppService _inventarioAppService;

    public InventarioController(IInventarioAppService inventarioAppService)
    {
        _inventarioAppService = inventarioAppService;
    }

    // GET: Inventario
    public ActionResult Index()
    {
        var model = InventarioViewModel.ToViewModel(_inventarioAppService.All(true));
        return View(model);
    }
}

这是我的Api控制器

public class InventarioApiController : ApiController
{
    private readonly IInventarioAppService _inventarioAppService;

    public InventarioApiController(IInventarioAppService inventarioAppService)
    {
        _inventarioAppService = inventarioAppService;
    }

    public IHttpActionResult Get()
    {
        var model = _inventarioAppService.All(true);
        return Ok(model);
    }
}

这是我的NinjectWebCommon

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

    /// <summary>
    /// Starts the application
    /// </summary>
    public static void Start() 
    {
        DynamicModuleUtility.RegisterModule(typeof(OnePerRequestHttpModule));
        DynamicModuleUtility.RegisterModule(typeof(NinjectHttpModule));
        Bootstrapper.Initialize(CreateKernel);
    }

    /// <summary>
    /// Stops the application.
    /// </summary>
    public static void Stop()
    {
        Bootstrapper.ShutDown();
    }

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

            RegisterServices(kernel);
            return kernel;
        }
        catch (Exception ex)
        {
            kernel.Dispose();
            throw new Exception("Erro ao criar o Kernel do Ninject", ex);
        }
    }

    /// <summary>
    /// Load your modules or register your services here!
    /// </summary>
    /// <param name="kernel">The kernel.</param>
    // ReSharper disable once UnusedParameter.Local
    private static void RegisterServices(IKernel kernel)
    {
    }        
}

这是我的packages.config

<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="Antlr" version="3.4.1.9004" targetFramework="net452" />
  <package id="AutoMapper" version="4.2.0" targetFramework="net452" />
  <package id="bootstrap" version="3.0.0" targetFramework="net452" />
  <package id="Gps.Domain.Specification" version="1.0.0.0" targetFramework="net452" />
  <package id="Gps.Domain.Validation" version="1.0.0.0" targetFramework="net452" />
  <package id="jQuery" version="1.10.2" targetFramework="net452" />
  <package id="jQuery.Validation" version="1.8.0.1" targetFramework="net452" />
  <package id="Microsoft.AspNet.Mvc" version="5.2.3" targetFramework="net452" />
  <package id="Microsoft.AspNet.Razor" version="3.2.3" targetFramework="net452" />
  <package id="Microsoft.AspNet.Web.Optimization" version="1.1.3" targetFramework="net452" />
  <package id="Microsoft.AspNet.WebApi" version="5.2.3" targetFramework="net452" />
  <package id="Microsoft.AspNet.WebApi.Client" version="5.2.3" targetFramework="net452" />
  <package id="Microsoft.AspNet.WebApi.Core" version="5.2.3" targetFramework="net452" />
  <package id="Microsoft.AspNet.WebApi.HelpPage" version="5.2.3" targetFramework="net452" />
  <package id="Microsoft.AspNet.WebApi.WebHost" version="5.2.3" targetFramework="net452" />
  <package id="Microsoft.AspNet.WebPages" version="3.2.3" targetFramework="net452" />
  <package id="Microsoft.CodeDom.Providers.DotNetCompilerPlatform" version="1.0.0" targetFramework="net452" />
  <package id="Microsoft.jQuery.Unobtrusive.Validation" version="3.2.3" targetFramework="net452" />
  <package id="Microsoft.Net.Compilers" version="1.0.0" targetFramework="net452" developmentDependency="true" />
  <package id="Microsoft.Web.Infrastructure" version="1.0.0.0" targetFramework="net452" />
  <package id="Modernizr" version="2.6.2" targetFramework="net452" />
  <package id="Newtonsoft.Json" version="6.0.4" targetFramework="net452" />
  <package id="Ninject" version="3.2.0.0" targetFramework="net452" />
  <package id="Ninject.MVC5" version="3.2.1.0" targetFramework="net452" />
  <package id="Ninject.Web.Common" version="3.2.0.0" targetFramework="net452" />
  <package id="Ninject.Web.Common.WebHost" version="3.2.0.0" targetFramework="net452" />
  <package id="Ninject.Web.WebApi" version="3.2.4.0" targetFramework="net452" />
  <package id="Respond" version="1.2.0" targetFramework="net452" />
  <package id="WebActivatorEx" version="2.0" targetFramework="net452" />
  <package id="WebGrease" version="1.5.2" targetFramework="net452" />
</packages>

那么,我做错了什么?

更新

这是我的Ioc类,我在NinjecWebCommon中用来创建Ninject内核

public class Ioc
{
    public IKernel Kernel { get; set; }

    public Ioc()
    {
        Kernel = GetNinjectModules();
        ServiceLocator.SetLocatorProvider(() => new NinjectServiceLocator(Kernel));
    }

    private IKernel GetNinjectModules()
    {
        return new StandardKernel(new ServicesNinjectModule()
            , new InfraestructureNinjectModule()
            , new RepositoryNinjectModule()
            , new ApplicationNinjectModule());
    }

2 个答案:

答案 0 :(得分:1)

好的,在Callum Linington帮助之后,我只对代码进行了一次简单的更改,并且它可以正常工作,就像this link帮助一样

在WebCommon类中,我设置了Ninject.WebApi包的依赖项解析器,并解析了它。

这就是我班上现在的样子。

00:00:00

答案 1 :(得分:0)

您可能还会发现此link有用 - 它涉及添加包Ninject.WebApi.DependencyResolver,然后添加

System.Web.Http.GlobalConfiguration.Configuration.DependencyResolver = new Ninject.WebApi.DependencyResolver.NinjectDependencyResolver(kernel);

...在返回之前进入CreateKernal()方法。