I need to implement DI in my Web API Project. I’m using a Ninject and got a little problem.
This is Global.asax:
public class WebApiApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
NinjectModule registrations = new NinjectRegistrations();
var kernel = new StandardKernel(registrations);
DependencyResolver.SetResolver(new NinjectDependencyResolver(kernel));
AreaRegistration.RegisterAllAreas();
GlobalConfiguration.Configure(WebApiConfig.Register);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
}
This is another class for ninject:
public class NinjectRegistrations : NinjectModule
{
public override void Load()
{
Bind<ICountriesRepository>().To<CountriesRepository>();
}
}
And this code can send an Interface object to HomeController, but not to other controllers.
public class CountriesController : ApiController
{
public ICountriesRepository db;
// CONSTRUCTOR DO NOT RECEIVE AN INTERFACE OBJECT
public CountriesController(ICountriesRepository a) // CONSTRUCTOR DO NOT RECIEVE AN INTERFACE OBJECT
{
db = a;
}
}
I just need to set another controller-support, to receive an Interface object
So, i will appreciate your help.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
Your implementation is different from what you needed to inject something on the constructor.
You need to create a DependecyResolver to inject it on the constructors. You can have something like this:
public class NinjectRegistrations : NinjectModule
{
public override void Load()
{
Bind<IProductRepository>().To<ProductRepository>();
}
}
public class NinjectDependencyResolver : NinjectDependencyScope, IDependencyResolver, System.Web.Mvc.IDependencyResolver
{
private readonly IKernel kernel;
public NinjectDependencyResolver(IKernel kernel)
: base(kernel)
{
this.kernel = kernel;
}
public IDependencyScope BeginScope()
{
return new NinjectDependencyScope(this.kernel.BeginBlock());
}
}
public class NinjectDependencyScope : IDependencyScope
{
private IResolutionRoot resolver;
internal NinjectDependencyScope(IResolutionRoot resolver)
{
Contract.Assert(resolver != null);
this.resolver = resolver;
}
public void Dispose()
{
var disposable = this.resolver as IDisposable;
if (disposable != null)
{
disposable.Dispose();
}
this.resolver = null;
}
public object GetService(Type serviceType)
{
if (this.resolver == null)
{
throw new ObjectDisposedException("this", "This scope has already been disposed");
}
return this.resolver.TryGet(serviceType);
}
public IEnumerable<object> GetServices(Type serviceType)
{
if (this.resolver == null)
{
throw new ObjectDisposedException("this", "This scope has already been disposed");
}
return this.resolver.GetAll(serviceType);
}
}
On Your Global.asax
NinjectModule registrations = new NinjectRegistrations(); var kernel = new StandardKernel(registrations); var ninjectResolver = new NinjectDependencyResolver(kernel); // If you are using MVC DependencyResolver.SetResolver(ninjectResolver); // If you are using API GlobalConfiguration.Configuration.DependencyResolver = ninjectResolver;
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0