container icon indicating copy to clipboard operation
container copied to clipboard

Different exception is thrown in newer version. Is this a bug or a design decision?

Open SerhatAkkus87 opened this issue 7 years ago • 2 comments

Hello,

I have some code which used Unity 5.8.6. When a resolution of a class failed, it used to throw a Unity.Exceptions.ResolutionFailedException.

In Unity 5.11.1, it throws a System.Reflection.TargetInvocationException now. Is this a design decision or a bug?

Example: The following code registers a class, which throws an exception in the constructor:

using System.Reflection;
using Unity;

namespace UnityNewException
{
    class Program
    {
        static void Main(string[] args)
        {
            UnityContainer container = new UnityContainer();
            container.RegisterType<ITestClass, TestClass>();
            try
            {
                // The implementation should throw an exception in the constructor.
                var config = container.Resolve<ITestClass>();
            }
            // Thrown in Unity 5.8.6
            catch (ResolutionFailedException e)
            {
                System.Console.WriteLine("Exception = " + e.GetType());
                System.Console.WriteLine("Inner exception = " + e.InnerException.GetType());
            }
            // Thrown in Unity 5.11.1
            catch (TargetInvocationException e)
            {
                System.Console.WriteLine("Exception = " + e.GetType());
                System.Console.WriteLine("Inner exception = " + e.InnerException.GetType());
            }
            System.Console.ReadKey();  
        }
    }
}

Is there any difference between ResolutionFailedException and TargetInvocationException? Is ResolutionFailedException outdated or is this a bug?

Thanks.

SerhatAkkus87 avatar Nov 29 '19 12:11 SerhatAkkus87

It would help if you could create a test case I can execute.

ENikS avatar Nov 30 '19 00:11 ENikS

@ENikS

Thank you for the fast response.

You can download my test solution containing two projects containing the same code but using different unity container versions.

UnityException.zip

SerhatAkkus87 avatar Dec 02 '19 09:12 SerhatAkkus87