1

由于 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 方法,但对我没有用。

4

1 回答 1

1

我不认为AllTypes.FromThisAssembly()它自己实际上注册了任何东西......

尝试Pick()

container.Register(AllTypes.FromThisAssembly()
            .Pick());

不过,我仍然不是 100% 使用 Fluent API :)

这似乎清除了它:

Castle Windsor Fluent 注册 - Pick() 做什么?

所以你可以使用Pick()or AllTypes().Of<object>()- 指定AllTypes()而不选择任何类型实际上不会向容器添加任何类型

您还需要指定组件实现的服务:

container.Register(AllTypes.FromThisAssembly()
            .Pick().WithService.FirstInterface());

或者

container.Register(AllTypes.FromThisAssembly()
            .Pick().WithService.DefaultInterfaces());
于 2013-04-26T09:32:32.037 回答