简体   繁体   English

WebAPI。 在继承的情况下使用Ninject进行依赖注入

[英]WebAPI. Dependency injection with Ninject in case of inheritance

I'm pretty new to ASP.NET WebApi project, but hopefully I'll put everything straight enough. 我是ASP.NET WebApi项目的新手 ,但希望我会把所有事情讲得足够清楚 After creating couple CRUD Controllers a brilliant idea come to my mind - write generic base CRUD-web-API controller for all of them and do not mess with rewriting same code. 在创建了两个CRUD控制器之后,我想到了一个绝妙的主意-为所有这些都编写通用的基本CRUD-web-API控制器,并且不要为重写相同的代码而烦恼。
After successful implementation of such class I faced problem with dependency resolving which is still working fine for non-generic/-inherited controllers. 在成功实现此类后,我遇到了依赖关系解析的问题,该解析对于非泛型/继承的控制器仍然可以正常工作。
Simple request (GET, POST, etc.) gives: 简单的请求(GET,POST等)给出:
Type 'UsersController' does not have a default constructor","ExceptionType":"System.ArgumentException"

Default constructor without injections works fine. 没有注入的默认构造函数可以正常工作。 Obviously I have a problem with Ninject configuration. 显然我对Ninject配置有问题。

public abstract class BaseCRUDController<T> : ApiController where T : class, IClientEntity 
   {
      private readonly Repository<T> _repo;
      private readonly IDbContextDataProvider _context;

      // With this ctor everything works well
      public BaseCRUDController()
      {
         this._context = new ModelContext();
         this._repo = new Repository<T>(this._context);
      }

      // Injection is not working ((
      public BaseCRUDController(IDbContextDataProvider context)
      {
         this._context = context;
         this._repo = new Repository<T>(context);
      }

And concrete Controller for User entity: User实体的具体Controller

public class UsersController : BaseCRUDController<User>
{      
  UsersController(IDbContextDataProvider context) : base(context) { }

  UsersController() : base() { }
}

And Ninject config itself: 和Ninject配置本身:

public class DataProviderModule : NinjectModule
{
  public override void Load()
  {
     this.Bind<IDbContextDataProvider>().To<ModelContext>().InSingletonScope();
  }
}
public class NinjectResolver
{
  // Instantinate Ninject dependencies resolver
  public static System.Web.Http.Dependencies.IDependencyResolver GetConfiguredDependencyResolver()
  {
     IKernel kernel = new StandardKernel(new DataProviderModule());
     System.Web.Http.Dependencies.IDependencyResolver njResolver = new NinjectResolver(kernel);
     return njResolver;
  }
}

And Application_Start Application_Start

protected void Application_Start()
{
    AreaRegistration.RegisterAllAreas();

    var config = GlobalConfiguration.Configuration;
    config.DependencyResolver = NinjectResolver.GetConfiguredDependencyResolver();

    WebApiConfig.Register(config);

What am I doing wrong here? 我在这里做错了什么?


NOTE: This approach works well if I have: 注意:如果我有以下方法,此方法效果很好:

 public class UsersController : ApiController { UsersController(IDbContextDataProvider context) { .... } ... 

Oh.. I've spent hours trying different approaches. 哦..我已经花了数小时尝试不同的方法。 It was madness. 太疯狂了。 And the funny part here is that Ninject is working well and code is correct except one accessibility modifier. 而且有趣的是,除了一个可访问性修饰符之外, Ninject运行良好并且代码正确。 Adding public modifier to UsersController ctor fixed the issue. public修饰符添加到UsersController ctor可以解决此问题。

public class UsersController : BaseCRUDController<User>
{      
  public UsersController(IDbContextDataProvider context) : base(context) { }

...

PS. PS。 Write your code carefully... 仔细编写代码...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM