rarrosi
rarrosi

Reputation: 1

Ninject does not propagate exception

I'm using Ninject to create an instance of class which has constructor parameters. This class checks it's parameters value and if it's not valid it throws an exception. The only problem I'm facing is that Ninject swallows the exception and returns null for my object. I would like to have my excpetion propagated like other exceptions in c#. Is there a way to do this?

The code looks like this (just an example, this is not the actual code)



    public class Module : NinjectModule
    {
        public override void Load()
        {
            Bind<IMyClass>().To<MyClass>();
        }
    }

    public class MyClass : IMyClass
    {
        public MyClass(object value)
        {
            if(value == null)
                throw new ArgumentNullException("value");
        }
    }

    public class Program
    {
       public void Method()
       {
           object myObject = null;
           IMyClass instance = GetKernel().
               Get<IMyClass>(new ConstructorArgument("value", myObject));

           // here the instance variable is null and my 
           // exception has been swallowed
       }
    }


Upvotes: 0

Views: 171

Answers (1)

Remo Gloor
Remo Gloor

Reputation: 32725

I cannot reproduce this behavior. At least the current released version throws the exception to the caller. Additionally, your code won't even compile as null is ambiguous as argument for ConstructorArgument

Upvotes: 2

Related Questions