RE: [ninject] Strange ActivationException

20 views
Skip to first unread message

Remo Gloor

unread,
May 2, 2013, 5:07:20 AM5/2/13
to nin...@googlegroups.com

Yes you are right conditions are intentionally not considered when choosing the constructor to simplify activation and because it is usually a bug when you have a bindings for a type but none for the current situation.

 

Usually you shouldn’t have multiple constructors anyway. Inject a Null (NoOp) implementation instead of not assigning a dependency. This has the additional advantage that the implementation is simpler.

 

From: nin...@googlegroups.com [mailto:nin...@googlegroups.com] On Behalf Of Mark Reid
Sent: Donnerstag, 2. Mai 2013 06:11
To: nin...@googlegroups.com
Subject: [ninject] Strange ActivationException

 

Take this code snippet.

 

When it tries to instantiate Test2, the below exception is raised.

This seems incorrect to me.  It's like the constructor is being chosen based upon the available bindings without considering the when clause.

 

When it tries to create an IA, it fails because of the when clause.

Or have I missed something here?

 

 

Error activating IA

 

No matching bindings are available, and the type is not self-bindable.

 

Activation path:

 

  2) Injection of dependency IA into parameter blurt of constructor of type Test2

 

  1) Request for Test2

 

 

 

Suggestions:

 

  1) Ensure that you have defined a binding for IA.

 

  2) If the binding was defined in a module, ensure that the module has been loaded into the kernel.

 

  3) Ensure you have not accidentally created more than one kernel.

 

  4) If you are using constructor arguments, ensure that the parameter name matches the constructors parameter name.

 

  5) If you are using automatic module loading, ensure the search path and filters are correct.

 

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using Ninject;

 

namespace ConsoleApplication2

{

            class Program

            {

                        static void Main(string[] args)

                        {

                                   var k = new StandardKernel();

                                   k.Bind<IA>().To<A>().WhenInjectedExactlyInto<Test1>();

                                   var test1 = k.Get<Test1>();

                                   var test2 = k.Get<Test2>();

                                   Console.ReadLine();

 

                        }

            }

 

            public class Test1

            {

                        public Test1()

                        {

                                   Console.WriteLine("Test1::Parameterless");

                        }

                        public Test1(IA blurt)

                        {

                                   Console.WriteLine("Test1::A");

 

                        }

            }

 

            public class Test2

            {

                        public Test2()

                        {

                                   Console.WriteLine("Test2::Parameterless");

 

                        }

 

                        public Test2(IA blurt)

                        {

                                   Console.WriteLine("Test2::A");

 

                        }

            }

 

            public class A : IA

            {

 

            }

 

            public interface IA

            {

            }

}

 

--
You received this message because you are subscribed to the Google Groups "ninject" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ninject+u...@googlegroups.com.
To post to this group, send email to
nin...@googlegroups.com.
Visit this group at http://groups.google.com/group/ninject?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Mark Reid

unread,
May 2, 2013, 5:28:57 PM5/2/13
to nin...@googlegroups.com
Thanks for the explanation, I had figured it was an optimisation decision.

I'm dealing with generated code, so I don't really have the choice of removing constructors!
In the end I've worked around this by using some specific constructor bindings.

Just checking that I hadn't missed something obvious!

To unsubscribe from this group and stop receiving emails from it, send an email to ninject+unsubscribe@googlegroups.com.


To post to this group, send email to

Reply all
Reply to author
Forward
0 new messages