Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dependency Injection Outside Controller (MVC 4 Web API) #28

Closed
Teagan42 opened this issue Jun 5, 2013 · 1 comment
Closed

Dependency Injection Outside Controller (MVC 4 Web API) #28

Teagan42 opened this issue Jun 5, 2013 · 1 comment

Comments

@Teagan42
Copy link

Teagan42 commented Jun 5, 2013

I have an ApiController with the following constructor signature:

public BlazrController(BlazrDBContext dbContext, IAuthProvider authProvider, ILogger logger) { ... }

My bindings:

kernel.Bind<BlazrDBContext>().ToSelf().InRequestScope();
kernel.Bind<IHasher>().To<PasswordHasher>().InRequestScope();
kernel.Bind<IIPContextProvider>().To<DefaultIPContextProvider>().InRequestScope();
kernel.Bind<ILocationContextProvider>().To<DefaultLocationContextProvider>().InRequestScope();
kernel.Bind<ILogger>().To<FileLogger>().InRequestScope();
kernel.Bind<IAuthProvider>().To<BlazrAuthProvider>().InRequestScope();

And the IAuthProvider implementation (BlazrAuthProvider) has a constructor with signature:

public BlazrAuthProvider(BlazrDBContext dbcontext, IHasher hasher) {....}

When I run my application and navigate to a route under the ApiController above, I get the whole "No default constructor" error. I have removed the injected parameters from the constructors as to narrow down which one was causing the problem - and it was the IHasher under the BlazrAuthprovider. If I remove that from the constructor, it loads just fine.

I implemented a NinjectDependencyResolver (IDependencyResolver) as others have suggested but still can't get this to work. Am I missing something?

Also worth noting: the Controller is under it's own Web API Project and the other classes/interfaces are under a Class Library Project in the same solution.

@remogloor
Copy link
Member

WebApi support is provided by https://github.com/ninject/Ninject.Web.WebApi not the MVC extension.

Questions about custom IDependencyResolver implementation are better asked in the user list or on stackoverflow.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants