简体   繁体   中英

Unity constructor objects not setting correctly

Question background:

I am trying to set the constructor of the class I am trying to create an object of through the use of Unity but currently am running into this error:

The error:

Result Message: Test method ABCTestProject.TFStests.Check_Interface_CheckOut_Method threw exception: System.InvalidOperationException: The type ABC.Tools.VersionControl.TfsVersionControl.TfsVcPromotionManager does not have a constructor that takes the parameters (ITfsVcQaCheckoutWorker, ITfsVcQaCheckinWorker, VersionControlServer).

I believe this is something I am miss setting when registering the unity object as the TfsPromotion Manager class does expect the objects being passed to it.

The code:

Here's the Unity creation class:

internal static ITfsVcPromotionManager CreateUnityObjects(VersionControlServer tfServer)
    {
        var unityContainer = new UnityContainer();


        var test = new TfsVcQaCheckoutWorker(tfServer);

        InjectionConstructor injectionConstructor = new InjectionConstructor(test);


        var test1 = new TfsVcQaCheckinWorker();

        InjectionConstructor injectionConstructor1 = new InjectionConstructor(test1);

        unityContainer.RegisterType<ITfsVcPromotionManager, TfsVcPromotionManager>(new InjectionConstructor(new ResolvedParameter(typeof(ITfsVcQaCheckoutWorker)), new ResolvedParameter(typeof(ITfsVcQaCheckinWorker)), new ResolvedParameter(typeof(VersionControlServer))));


        return unityContainer.Resolve<ITfsVcPromotionManager>();   
    }

The TfsPromotionManager constructor:

internal TfsVcPromotionManager(ITfsVcQaCheckoutWorker checkOutWorker, ITfsVcQaCheckinWorker checkInWorker, VersionControlServer tfServer)
    {
        _checkoutWorker = checkOutWorker;
    }

That constructor is internal, but Unity will only look at public constructors. (Which makes sense as when you declare the constructor internal, you are telling the .net CLR that the class should only be created (at least using that constructor) from the same assembly where you declared it). So the easiest way is to declare the constructor as public.

Take a look at this question and its answers in case you cannot make the constructor public. Creating a factory method or class and wiring it with an InjectionFactory will probably be a nice option here.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM