无法获取Ninject.Extensions.Conventions工作 [英] Cannot get Ninject.Extensions.Conventions to work

查看:225
本文介绍了无法获取Ninject.Extensions.Conventions工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在试图获得(Ninject 3+)工作Ninject.Extensions.Conventions,没有运气。我煮下来到一个找到的样本控制台应用程序,而我甚至不能得到持续。下面是我有:

I've been trying to get Ninject.Extensions.Conventions for (Ninject 3+) working, with no luck. I boiled it down to a found sample console app, and I can't even get that going. Here's what I have:

 class Program
    {
        static void Main(string[] args)
        {
            var kernel = new StandardKernel();
            kernel.Bind(x => x
               .FromThisAssembly()
               .SelectAllClasses()
               .BindAllInterfaces());

            var output = kernel.Get<IConsoleOutput>();
            output.HelloWorld();

            var service = kernel.Get<Service>();
            service.OutputToConsole();

            Console.ReadLine();
        }

        public interface IConsoleOutput
        {
            void HelloWorld();
        }

        public class ConsoleOutput : IConsoleOutput
        {
            public void HelloWorld()
            {
                Console.WriteLine("Hello world!");
            }
        }

        public class Service
        {
            private readonly IConsoleOutput _output;
            public Service(IConsoleOutput output)
            {
                _output = output;
            }

            public void OutputToConsole()
            {
                _output.HelloWorld();
            }
        }
    }



我也尝试过各种连击的 FromAssembliesMatching SelectAllTypes BindDefaultInterfaces 等,一切都抛出错误激活。没有匹配的绑定是可用的,并且类型是不言可绑定

I've also tried various combos of FromAssembliesMatching, SelectAllTypes, BindDefaultInterfaces, etc. Everything throws the Error activating . No matching bindings are available, and the type is not self-bindable.

只是理智,如果我做一个手工与绑定:

Just for sanity, if I do a manual binding with:

kernel.Bind<IConsoleOutput>().To<ConsoleOutput>();



一切工作就好了。所以很明显我只是失去了一些东西。

Everything works just fine. So clearly I'm just missing something.

推荐答案

这是造成的,山姆的建议,由非公职的类型。他们是内部的类型本次非公开程序类的。

It is caused, as sam suggested, by the types not being public. They are inner types of the non-public "Program" class.

请计划的公共或添加 .IncludingNonePublicTypes()

kernel.Bind(x => x
    .FromThisAssembly()
    .IncludingNonePublicTypes()
    .SelectAllClasses()
    .BindAllInterfaces());



(我已经验证,无论是作品,而你的代码没有)。

(I have verified that either works, and your code doesn't).

和这里是你的官方消息:
https://github.com/ninject/ninject.extensions.conventions/blob/master/src/Ninject.Extensions.Conventions.Test/IntegrationTests/ NonePublicTypesTests.cs

And here is your official source: https://github.com/ninject/ninject.extensions.conventions/blob/master/src/Ninject.Extensions.Conventions.Test/IntegrationTests/NonePublicTypesTests.cs

这篇关于无法获取Ninject.Extensions.Conventions工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆