dimanche 13 mars 2016

Castle windsor property injection and componentnotfoundexception exception

I register my components as below in castle windsor container :

    public class ServicesInstaller : IWindsorInstaller
{
    public void Install(IWindsorContainer container, IConfigurationStore store)
    {

        var appDomain = System.AppDomain.CurrentDomain;
        var basePath = appDomain.RelativeSearchPath ?? appDomain.BaseDirectory;
        var path = basePath + "\\";

        var bizPath = path + "Business.dll";
        var asm = Assembly.LoadFile(bizPath);

        foreach (Type type in asm.GetTypes().Where(q => q.Name.EndsWith("Business") && !q.IsInterface))
        {
            var interfaceName = type.GetInterfaces().FirstOrDefault(q => q.Name == "I" + type.Name);
            if (interfaceName != null)
            {
                container.Register(Component.For(interfaceName).ImplementedBy(type).LifestyleTransient());
            }
        }

    }

}

and then I try to resolve a component which is a property that has InjectDependency attribute :

[InjectDependency]
public IPostBusiness postBiz { get; set; }

with this code :

foreach (var prop in this.GetType().GetProperties())
        {
            object[] attrs = prop.GetCustomAttributes(true);
            foreach (object attr in attrs)
            {
                InjectDependencyAttribute authAttr = attr as InjectDependencyAttribute;
                if (authAttr != null)
                {
                    var instance = ContainerManager.Container.Resolve(prop.PropertyType);
                    prop.SetValue(this, instance);
                }
            }
        }

here an exception throws when it wants to resolve the component "No component for supporting the service Business.IPostBusiness was found"

Point : I used the below code too but it didn't work .

 var allInterfaces = asm.GetTypes().Where(q => q.Name.EndsWith("Business") && q.IsInterface);
        container.Register(Classes.FromAssembly(asm).BasedOn(allInterfaces).WithServiceBase().LifestyleTransient());

but when I register as below it works :

container.Register(Component.For<IPostBusiness>().ImplementedBy<PostBusiness>().LifestyleTransient());

Am I doing something wrong ? Any help?





Aucun commentaire:

Enregistrer un commentaire