由于Autofac不支持Mono,我试图切换到Windsor IoC框架。 我想搜索dll以获取我的接口IDataLoader的实现,并将所有这些解析为实例。
这是我的解析代码:
var container = new WindsorContainer();
System.Reflection.Assembly asm = System.Reflection.Assembly.LoadFrom("/home/konrad/DataLoader.dll");
container.Register(AllTypes.FromAssembly(asm));
IDataLoader loader = container.Resolve<IDataLoader>();
界面如下所示:
namespace Viewer.Core.Data
{
public interface IDataLoader
{
PointControl[] GetPositionData(string filePath);
}
}
和实施:
using OpenTK;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Viewer.Core.Data;
using Castle.Windsor;
using Castle.MicroKernel.Registration;
namespace DataLoader
{
public class StandardDataLoader : IDataLoader
{
public StandardDataLoader ()
{
}
public PointControl[] GetPositionData(string filePath)
{
return CreateCloud(filePath);
}
private PointControl[] CreateCloud(string path)
{
//loading data from file code
return points;
}
}
}
解决后我收到错误:
{Castle.MicroKernel.ComponentNotFoundException: No component for supporting the service Viewer.Core.Data.IDataLoader was found at Castle.MicroKernel.DefaultKernel.Castle.MicroKernel.IKernelInternal.Resolve (System.Type service, IDictionary arguments, IReleasePolicy policy) [0x00000] in <filename unknown>:0 at Castle.MicroKernel.DefaultKernel.Resolve (System.Type service, IDictionary arguments) [0x00000] in <filename unknown>:0 at Castle.Windsor.WindsorContainer.Resolve[IDataLoader] () [0x00000] in <filename unknown>:0 at (wrapper remoting-invoke-with-check) Castle.Windsor.WindsorContainer:Resolve () at ViewerMultiplatform.DataModel.LoadModel (System.String modelPath) [0x00027] in /home/konrad/hg/ViewerPrototype/OpenTKMultithread/ViewerMultiplatform/Models/DataModel.cs:103 }
我需要做些额外的工作才能使我的类可以被Windsor框架解析吗?我也尝试使用register和resolveall方法,但没有什么对我有用。
答案 0 :(得分:1)
我认为AllTypes.FromThisAssembly()
实际上没有注册任何东西......
尝试Pick()
container.Register(AllTypes.FromThisAssembly()
.Pick());
我仍然不是100%使用Fluent API但是:)
这似乎清除了它:
Castle Windsor Fluent Registration - What does Pick() do?
因此,您可以使用Pick()
或AllTypes().Of<object>()
- 指定AllTypes()
而不挑选任何类型,但实际上并未向容器添加任何类型
您还需要指定组件实现的服务:
container.Register(AllTypes.FromThisAssembly()
.Pick().WithService.FirstInterface());
或
container.Register(AllTypes.FromThisAssembly()
.Pick().WithService.DefaultInterfaces());