Accessing the Ninject Kernel Globally

后端 未结 4 494
名媛妹妹
名媛妹妹 2021-02-05 09:25

This question is not specifically related to Ninject. It\'s more of a general coding question, but I\'m posting it here in case there might be a better way entirely of handling

4条回答
  •  长发绾君心
    2021-02-05 09:58

    Newer version of Ninject has this method if used with System.Web.MVC:

    var obj = DependencyResolver.Current.GetService();
    

    Unless you need to manipulate DI bindings on the fly, but instantiating a StandardKernel is a little heavy.

    IKernel kernel = new StandardKernel();
    var obj = DependencyResolver.Current.GetService();
    

提交回复
热议问题