views:

391

answers:

1

I do not understand the error I am getting as listed at the bottom of this email. Why is the container trying to cast the object, especially if the compiler is doing it without error? I am using v2.0.0.5642.

I’m sure it’s in the configuration, but I am lost. I would really appreciate any help.

Thanks, Lars

  <component id="cipherMaster" type="Demo.View.UserControls.CipherMaster, Demo.View" />
  <component id="cipherVariation" service="Demo.View.UserControls.CipherMaster, Demo.View"
             type="Demo.View.UserControls.CipherVariation, Demo.View" />
  <component id="presenterVariation" service="Demo.Model.Interfaces.IDemoTypePresenter, Demo.Model"
     type="Demo.Presenter.PresenterVariation, Demo.Presenter" >
    <cipherPanel>${cipherVariation}</cipherPanel>
  </component>

namespace Demo.Presenter { public class PresenterCipherMaster : IDemoTypePresenter { ... } }

namespace Demo.Presenter { public class PresenterVariation : PresenterCipherMaster { private readonly CipherVariation _variation;

    public PresenterVariation(IMasterDemo view, CipherMaster cipherPanel, FeaturesVariation features,
        IEncryptionEngine engine):base(view, cipherPanel, features, engine)
    {
        _variationLog = new List<CipherVariationLog>();
         _variation = (CipherVariation) cipherPanel; //<<< Cast error points here line 18
         ...
    }
}

}

public static class IocContainer
{
    public static T Resolve<T>(string key)
    {
        T presenter = Container.Resolve<T>(key);  //<<< Error occurs here
        return presenter;
    }
}

namespace Demo.View.UserControls { public partial class CipherMaster : UserControl { ... } }

namespace Demo.View.UserControls { public partial class CipherVariation : CipherMaster { ... } }

=====================

Castle.MicroKernel.ComponentActivator.ComponentActivatorException was unhandled
  Message="ComponentActivator: could not instantiate Demo.Presenter.PresenterVariation"
  Source="Castle.MicroKernel"
  StackTrace:
  ...
  InnerException: System.Reflection.TargetInvocationException
       Message="Exception has been thrown by the target of an invocation."
       Source="mscorlib"
       StackTrace:
       ...
       InnerException: System.InvalidCastException
            Message="Unable to cast object of type 'Demo.View.UserControls.CipherMaster' to type 'Demo.View.UserControls.CipherVariation'."
            Source="Demo.Presenter"
            StackTrace:
                 at Demo.Presenter.PresenterVariation..ctor(IMasterDemo view, CipherMaster cipherPanel, FeaturesVariation features, IEncryptionEngine engine) in E:\Development\MainStreamDemo\Demo.Presenter\PresenterVariation.cs:line 18
            InnerException:
A: 

I've seen situations where Castle resolves against the other registered services first, and then uses the parameters you specify. In this case since you are using a concrete type as the argument (CipherMaster) and it is registered, it's probably using the registered component.

I would try either creating an interface for the two controls to implement or just changing the type on the constructor to just "Object" or "UserControl" so that it's not a registered type.

Eric Nicholson

related questions