Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exception that a constructor parameter doesn't exist when it does

I am trying to inject through the use of Unity two objects based on an interface into a class constructor.

I'm currently receiving the following error when unit testing:

Result Message:
Test method TestProject.TFStests.Check_Interface_CheckOut_Method threw exception: System.InvalidOperationException: The type Adp.Tools.VersionControl.TfsVersionControl.TfsVcPromotionManager does not have a constructor that takes the parameters (TfsVcQaCheckoutWorker).

The following code is my Unity class, this is used to register and resolve the TfsVCPromotionManager object:

public class UnityClass
{
    public static ITfsVcPromotionManager returnNewPromotionManager(
       VersionControlServer tfServer)
    {
        var container = new UnityContainer();

        ITfsVcQaCheckinWorker test1 = CreateUnityCheckInWorker();
        ITfsVcQaCheckoutWorker test2 = CreateUnityCheckOutWorker(tfServer);

        container.RegisterType<ITfsVcPromotionManager, TfsVcPromotionManager>(
            new InjectionConstructor(test2), new InjectionConstructor(test1));
        return container.Resolve<TfsVcPromotionManager>();
    }

    private static ITfsVcQaCheckinWorker CreateUnityCheckInWorker()
    {
        var container = new UnityContainer();

        container.RegisterType<ITfsVcQaCheckinWorker, ITfsVcQaCheckinWorker>();
        return container.Resolve<TfsVcQaCheckinWorker>();
    }

    private static ITfsVcQaCheckoutWorker CreateUnityCheckOutWorker(
        VersionControlServer passedServer)
    {
        var container = new UnityContainer();

        container.RegisterType<ITfsVcQaCheckoutWorker, TfsVcQaCheckoutWorker>(
            new InjectionConstructor(passedServer));
        return container.Resolve<TfsVcQaCheckoutWorker>();
    }
}

This is the constrcutor os the TfsVcPromotionManager class. note that it clearly takes in an instance based on the interfaces ITfsVcQaCheckoutworker and ITfsVcCheckinWorker.

 private ITfsVcQaCheckoutWorker _checkOutWorker;

    private ITfsVcQaCheckinWorker _checkInWorker;

    public TfsVcPromotionManager(ITfsVcQaCheckoutWorker checkOutWorker,
                                 ITfsVcQaCheckinWorker checkInWorker)
    {
        if (checkOutWorker == null || checkInWorker == null)
        {
            throw new NullReferenceException();
        }

        _checkOutWorker = checkOutWorker;
        _checkInWorker = checkInWorker;
    }

Can anyone give me an indication of what I'm doing wrong.

like image 934
user1352057 Avatar asked Jan 03 '14 17:01

user1352057


People also ask

Does not have a constructor that takes the parameters?

A constructor that takes no parameters is called a parameterless constructor. Parameterless constructors are invoked whenever an object is instantiated by using the new operator and no arguments are provided to new . For more information, see Instance Constructors.


2 Answers

It says it can't find a "constructor that takes the parameters (TfsVcQaCheckoutWorker)", which is indeed a true statement. There is no constructor that takes just one parameter. I'm not terrible familiar with Unity, but I bet your problem would be solved by changing this:

container.RegisterType<ITfsVcPromotionManager, TfsVcPromotionManager>(
    new InjectionConstructor(test2),
    new InjectionConstructor(test1))

To this:

container.RegisterType<ITfsVcPromotionManager, TfsVcPromotionManager>(
    new InjectionConstructor(test2, test1))

See the MSDN Reference for the InjectionConstructor's constructor.

like image 111
Jeff B Avatar answered Sep 18 '22 21:09

Jeff B


I believe you should register container with several params the other way. Have you tried this?

container.RegisterType<ITfsVcPromotionManager, TfsVcPromotionManager>(
    new InjectionConstructor(test2, test1)
);
like image 39
Andrei Avatar answered Sep 21 '22 21:09

Andrei