我在城堡windsor容器中注册我的组件如下:
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());
}
}
}
}
然后我尝试解析一个组件,该组件是具有InjectDependency属性的属性:
[InjectDependency]
public IPostBusiness postBiz { get; set; }
使用此代码:
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);
}
}
}
这里有一个异常,当它想要解析组件时“没有找到支持Business.IPostBusiness的服务的组件”
点:我也使用了以下代码,但它没有用。
var allInterfaces = asm.GetTypes().Where(q => q.Name.EndsWith("Business") && q.IsInterface);
container.Register(Classes.FromAssembly(asm).BasedOn(allInterfaces).WithServiceBase().LifestyleTransient());
但是当我注册时如下:
container.Register(Component.For<IPostBusiness>().ImplementedBy<PostBusiness>().LifestyleTransient());
我做错了吗?有什么帮助吗?