简体   繁体   中英

Dependency Injection using ninject not working

My goal is simply this: I want the already existent instance of class "MainWindowVM" (implementing IMainWindowVM) to be injected into class "StaticTestsResultsViewModel". I do not(!) want a new instance of class "MainWindowVM" to be instantiated. Instead, I want the already existent instance of this class to be injected into class "StaticTestsResultsViewModel".

My goal is simply this: I want the already existent instance of class "MainWindowVM" (implementing IMainWindowVM) to be injected into class "StaticTestsResultsViewModel". I do not(!) want a new instance of class "MainWindowVM" to be instantiated. Instead, I want the already existent instance of this class to be injected into class "StaticTestsResultsViewModel".

In that case you should register it like:

kernel.Bind<IMainWindowVM>().To<MainWindowVM>().InSingletonScope();

Do keep in mind that when you register a class as singleton, this implicitly makes all of its dependencies singleton as well. See Captive Dependency . If that is unacceptable to your application design, you should move the singleton instance where you store the shared property to be a dependency of MainWindowVM and not make MainWindowVM singleton (such as transient).

                          PropertyHolder (Singleton)
                         /
MainWindowVM (Transient)
                        \
                          OtherDependency (Any scope shorter than singleton)

A couple of months I came across to the same problem. Delete the class that was created when you installed Ninject from nugget and paste the following class at the same directory. From as much as I remember it doesn't need any other configuration. Let me know,

Your sincere.

using Ninject;
using Ninject.Modules;
using Project.Data;
using Project.Interfaces;
using Project.Managers;
using Project.Repository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Web;
using System.Web.Http;
using System.Web.Http.Dependencies;


namespace Project.App_Start
{
    /// <summary>
    /// Resolves Dependencies Using Ninject
    /// </summary>
    public class NinjectHttpResolver : IDependencyResolver, IDependencyScope
    {
        public IKernel Kernel { get; private set; }
        public NinjectHttpResolver(params NinjectModule[] modules)
        {
            Kernel = new StandardKernel(modules);
        }

        public NinjectHttpResolver(Assembly assembly)
        {
            Kernel = new StandardKernel();
            Kernel.Load(assembly);
        }

        public object GetService(Type serviceType)
        {
            return Kernel.TryGet(serviceType);
        }

        public IEnumerable<object> GetServices(Type serviceType)
        {
            return Kernel.GetAll(serviceType);
        }

        public void Dispose()
        {
            //Do Nothing
        }

        public IDependencyScope BeginScope()
        {
            return this;
        }
    }


    // List and Describe Necessary HttpModules
    // This class is optional if you already Have NinjectMvc
    public class NinjectHttpModules
    {
        //Return Lists of Modules in the Application
        public static NinjectModule[] Modules
        {
            get
            {
                return new[] { new MainModule() };
            }
        }

        //Main Module For Application
        public class MainModule : NinjectModule
        {
            public override void Load()
            {

                //Configure Your Bindings Here e.g


                Kernel.Bind<ILoginManager>().To<LoginManager>();
               >
    /// Its job is to Register Ninject Modules and Resolve Dependencies
    /// </summary>
    public class NinjectHttpContainer
    {
        private static NinjectHttpResolver _resolver;

        //Register Ninject Modules
        public static void RegisterModules(NinjectModule[] modules)
        {
            _resolver = new NinjectHttpResolver(modules);
            GlobalConfiguration.Configuration.DependencyResolver = _resolver;
        }

        public static void RegisterAssembly()
        {
            _resolver = new NinjectHttpResolver(Assembly.GetExecutingAssembly());
            //This is where the actual hookup to the Web API Pipeline is done.
            GlobalConfiguration.Configuration.DependencyResolver = _resolver;
        }

        //Manually Resolve Dependencies
        public static T Resolve<T>()
        {
            return _resolver.Kernel.Get<T>();
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM