NInject内核GetAll返回空

时间:2015-07-09 17:46:48

标签: ninject ninject-extensions

我实现了一个接口的两个项目(类库项目):

第一个:

public class MailPlugin : Extensibility.IProductorPlugin
{
    ...
}

第二个:

public class FileSystemPlugin : Extensibility.IProductorPlugin
{
    ...
}

Extensibility.IProductorPlugin,是第三个项目的接口:

namespace Extensibility
{
    public delegate void NotifyDigitalInputs(List<Domain.DigitalInput> digital_inputs);


    public interface IProductorPlugin
    {


        String Name { get; }
        String Description { get; }
        String Version { get; }


        List<Domain.Channel> AvailableChannels { get; }
        IEnumerable<Guid> TypeGuids { get; }


        event NotifyDigitalInputs OnDigitalInputs;


    }
}

在我的作文根中,我创建了这个类:

namespace UI
{
    public sealed class NinjectServiceLocator
    {


        private static readonly Lazy<NinjectServiceLocator> lazy = new Lazy<NinjectServiceLocator>(() => new NinjectServiceLocator());

        public static NinjectServiceLocator Instance { get { return lazy.Value; } }


        public Ninject.IKernel Kernel { get; private set; }


        private NinjectServiceLocator()
        {
            using (var k = this.Kernel = new Ninject.StandardKernel())
            {
                k.Bind(b => b.FromAssembliesMatching("*")
                    .SelectAllClasses()
                    .InheritedFrom(typeof(Extensibility.IProductorPlugin))
                    .BindAllInterfaces()
                );
            }
        }
    }
}

所以,当我想查找所有插件时,我只是执行此操作:

protected void initialize()
{
    foreach (Extensibility.IProductorPlugin productor_plugin in NinjectServiceLocator.Instance.Kernel.GetAll(typeof(Extensibility.IProductorPlugin)))
    {
        using (var channel_tile = new DevExpress.XtraBars.Docking2010.Views.WindowsUI.Tile() { Group = "Plugin Channels" })
        {
            foreach (Domain.Channel channel in productor_plugin.AvailableChannels)
            {
                channel_tile.Elements.Add(new DevExpress.XtraEditors.TileItemElement() { Text = channel.Name });
                channel_tile.Elements.Add(new DevExpress.XtraEditors.TileItemElement() { Text = channel.Description });

                this.tileContainer1.Items.Add(channel_tile);
            }
        }
    }
}

但是,GetAll会返回任何内容。 我究竟做错了什么? 我很感激你的帮助。

谢谢大家。

1 个答案:

答案 0 :(得分:1)

尝试从内核实例化中删除using()。一个using将把对象放在范围的末尾,这是我们不想要的内核。

using (var k = this.Kernel = new Ninject.StandardKernel())