在项目中使用Ninject

时间:2013-11-18 11:55:26

标签: asp.net-mvc asp.net-mvc-4 ninject inversion-of-control ioc-container

我想在我的项目中使用Ninject,这是我的代码:

public class NinjectControllerFactory : DefaultControllerFactory
{
private IKernel ninjectKernel;
public NinjectControllerFactory()
{
  ninjectKernel = new StandardKernel();
  AddBindings();
}

  protected override IController GetControllerInstance(RequestContextrequestContext,      Type controllerType)

{       return(controllerType == null)? null:(IController)ninjectKernel.Get(controllerType);     }

 private void AddBindings()
 {

   ninjectKernel.Bind<IHomeService>().To<HomeService>();
   ninjectKernel.Bind<IHomeBussiness>().To<HomeBussiness>();
 }

  }

我的global.asax是:

        AreaRegistration.RegisterAllAreas();
        WebApiConfig.Register(GlobalConfiguration.Configuration);
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        ControllerBuilder.Current.SetControllerFactory(new NinjectControllerFactory());

我家的控制器是:

    private readonly IHomeService _iHomeService;
    public HomeController(IHomeService iHomeService)
    {
        _iHomeService = iHomeService;
    } 

现在:

当我运行我的项目时,我看到说不存在StandardProvider.cs enter image description here

我的错误是:

Error activating ObjectContext using implicit self-binding of ObjectContext
Several constructors have the same priority. Please specify the constructor using      ToConstructor syntax or add an Inject attribute.

我该如何解决这个问题?

0 个答案:

没有答案