0

I'm pretty new to ASP.NET WebApi project, but hopefully I'll put everything straight enough. 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.
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:
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.

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:

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

  UsersController() : base() { }
}

And Ninject config itself:

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

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)
  {
     .... 
  }    

...
3
  • Is it possible that the MVC resolver is being used, rather than the Web API dependency resolver? I wonder if it is possible that the framework chooses the wrong resolver here because your controller doesn't derive immediately from ApiController. You could track this down through the stack trace - I've come across a vaguely similar problem where this was the issue. Commented Mar 8, 2013 at 22:56
  • I have tried this out on a test project of mine and it works without issue. It looks like I'm implementing Ninject a little different from how you're doing it. I used a combination of this and this approach. Commented Mar 9, 2013 at 0:04
  • @BrianS Even with approach from link that you provided I have same result( Commented Mar 9, 2013 at 11:31

1 Answer 1

1

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. Adding public modifier to UsersController ctor fixed the issue.

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

...

PS. Write your code carefully...

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.