How to iterate over Ninject StandardKernel's configured bindings to debug?
Asked Answered
M

1

6

In my Ninject binding module,

public class CarModule : NinjectModule 
{
    public override void Load()
    {
        Kernel.Bind(scanner => scanner.FromThisAssembly().SelectAllClasses()
              .InheritedFrom<ICar>().BindAllInterfaces());

        foreach (var binding in kernel.GetBindings(typeof(ICar)))
        {
           Trace.WriteLine(string.Format("[{0}] Service bound to [{1}]",
                        binding.Service.Name,
                        binding.Target.GetType().Name));
        }

        // Output looks like: 
        //[ICar] Service bound to [BindingTarget]
        //[ICar] Service bound to [BindingTarget]
        //[ICar] Service bound to [BindingTarget]
    }
}

I need a sanity check to see types I've successfully bound to my services. I'm having trouble accessing the type names of the bound types. Instead of [BindingTarget], I'd like to see [Mercedes], [Ferrari], etc...

Is there a good and/or common way to do this?

Madid answered 9/4, 2013 at 2:38 Comment(2)
Were you able to resolve this issue? I am having the same problem, is it possible? The best I could find was in: #4680803Hypocoristic
@Hypocoristic your link is the best thing I've seen so far. To answer your question, I've not yet found a solution to this problem.Madid
R
0

In your IocKernel, I d just add a method to GetAll the Instances of a given Type T, and call it back after Loading your Module:

    public static class IocKernel
    {
        private static StandardKernel _kernel;                         

        public static IEnumerable<T> GetAll<T>()
        {
            return _kernel.GetAll<T>();
        }    

        public static void Initialize(params INinjectModule[] modules)
        {
            if (_kernel == null)
            {
                _kernel = new StandardKernel(modules);

                List<ICar> bindingsList= _kernel.GetAll<ICar>().ToList();
                foreach(ICar binding in bindingsList)
                { 
                    Trace.WriteLine(string.Format("Service bound to [{1}]", binding.GetType().Name)
                }
            }
        }              
    }
Roodepoortmaraisburg answered 12/8, 2016 at 10:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.