This is a migrated thread and some comments may be shown as answers.

RadDatePicker with Unity.WebForms

2 Answers 75 Views
Calendar
This is a migrated thread and some comments may be shown as answers.
Don
Top achievements
Rank 1
Don asked on 20 Nov 2014, 08:37 PM
Hello,

I am using Unity.WebForms to enable dependency injection in my WebForms app. Whenever I attempt to access a page that contains a RadDatePicker, I get an exception telling me "The type CalendarPopupButton has multiple constructors of length 1. Unable to disambiguate."

Can you help me understand why Unity is not playing well with the Telerik tools? Thank you bery much,

Don

2 Answers, 1 is accepted

Sort by
0
Kyle
Top achievements
Rank 1
answered on 24 Nov 2014, 04:32 AM
Without knowing the particulars of how your code is written, here is my best guess at what is going on...

Unity likes to be helpful and will try to resolve a dependency chain if it can, even if one of the types in that chain hasn't already been registered within the container. What this means is that if class A depends on class B and you have only registered class A within the container, Unity will attempt to construct a new instance using reflection to see if it has enough information to do so. It will look for a default/parameter-less constructor or any constructor whose parameter types are already registered within the container. Failing that, it attempts the same process with each of the parameters of the class B constructors until it either builds up a full dependency tree or fails.

There are 2 potential solutions that I can think of really quickly:

1) Register the RadDatePicker in the Unity container and specify how it should resolve the constructor parameters. See the Unity documentation for how you register a type that doesn't have a default/parameter-less constructor for more information.

2) Fork the code for Unity.WebForms (https://bitbucket.org/KyleK/unity.webforms) and edit line 92 of the UnityHttpModule.cs class the include a check for the 'Telerik' namespace in addition to the 'System' namespace. This will prevent Unity.WebForms from attempting to dependency inject anything from Telerik (unless you explicitly register it with the container).

Hope that helps!
0
John
Top achievements
Rank 1
answered on 22 Feb 2017, 05:33 PM

Define an extension and strategy as below and add the extension to your unity container as follows.

 

IUnityContainer container = new UnityContainer();

....

container.AddNewExtension<SkipBuildupExtension>();

 

 

 

public class SkipBuildupExtension : UnityContainerExtension
    {
        protected override void Initialize()
        {
            //Position at the end of the Lifetime strategies ensures it will be considered prior to
            //the Creation stage, which is where injection occurs.
            Context.Strategies.Add(new SkipBuildupStrategy(), UnityBuildStage.Lifetime);
        }
    }

 

public class SkipBuildupStrategy : BuilderStrategy
    {
        public override void PreBuildUp(IBuilderContext context)
        {
            if (context.OriginalBuildKey.Type.FullName.StartsWith("Telerik.Web.UI."))
                context.BuildComplete = true;  //flag that we're not going to continue building up these types
        }
    }
Tags
Calendar
Asked by
Don
Top achievements
Rank 1
Answers by
Kyle
Top achievements
Rank 1
John
Top achievements
Rank 1
Share this question
or