ASP.NET Web API (Ninject) 中的依赖注入

Dependency Injection in ASP.NET Web API (Ninject)

我需要在我的 Web API 项目中实施 DI。我正在使用 Ninject 并遇到了一个小问题。

这是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);
   }
}

这是ninject的另一个class:

public class NinjectRegistrations : NinjectModule
{
   public override void Load()
   {
      Bind<ICountriesRepository>().To<CountriesRepository>();
   }
}

并且此代码可以将接口对象发送到 HomeController,但不能发送到其他控制器。

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;
   }
}

我只需要设置另一个控制器支持,以接收一个接口对象 所以,我会感谢你的帮助。

您的实现与您需要在构造函数中注入某些东西的实现不同。

您需要创建一个 DependecyResolver 以将其注入到构造函数中。你可以有这样的东西:

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);
    }
}

在你的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;