Ninject 3.0 MVC kernel.bind错误自动注册

时间:2012-04-11 01:55:33

标签: asp.net-mvc ninject structuremap

在kernel.Bind上获取和出错( scanner => ... “扫描仪”在VS 2010中有一个小错误线。

  

无法将lambda表达式转换为'System.Type []'类型   因为它不是代表   输入

Tyring to Auto Register就像2.0中的旧kernel.scan一样。 我无法弄清楚我做错了什么。添加和删​​除了这么多Ninject包。 完全失去了,浪费时间。

using System;
using System.Web;

using Microsoft.Web.Infrastructure.DynamicModuleHelper;

using Ninject;
using Ninject.Web.Common;
//using Ninject.Extensions.Conventions;
using Ninject.Web.WebApi;
using Ninject.Web.Mvc;
using CommonServiceLocator.NinjectAdapter;
using System.Reflection;
using System.IO;
using LR.Repository;
using LR.Repository.Interfaces;
using LR.Service.Interfaces;
using System.Web.Http;

public static class NinjectWebCommon 
{
    private static readonly Bootstrapper bootstrapper = new Bootstrapper();

    /// <summary>
    /// Starts the application
    /// </summary>
    public static void Start() 
    {
        DynamicModuleUtility.RegisterModule(typeof(OnePerRequestHttpModule));
        DynamicModuleUtility.RegisterModule(typeof(NinjectHttpModule));
        bootstrapper.Initialize(CreateKernel);
    }

    /// <summary>
    /// Stops the application.
    /// </summary>
    public static void Stop()
    {
        bootstrapper.ShutDown();
    }

    /// <summary>
    /// Creates the kernel that will manage your application.
    /// </summary>
    /// <returns>The created kernel.</returns>
    private static IKernel CreateKernel()
    {
        var kernel = new StandardKernel();
        kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
        kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();

        RegisterServices(kernel);
        return kernel;
    }


    /// <summary>
    /// Load your modules or register your services here!
    /// </summary>
    /// <param name="kernel">The kernel.</param>
            private static void RegisterServices(IKernel kernel)
            {

                kernel.Bind(scanner => scanner.FromAssembliesInPath(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location))
                    .Select(IsServiceType)
                    .BindToDefaultInterface()
                    .Configure(binding => binding.InSingletonScope())
                    );

            }

            private static bool IsServiceType(Type type)
            {
                // temp return true;
                // .Any() is not recognized either.
                return true; // type.IsClass && type.GetInterfaces().Any(intface => intface.Name == "I" + type.Name);
            }

2 个答案:

答案 0 :(得分:12)

您必须取消注释

//using Ninject.Extensions.Conventions;

答案 1 :(得分:2)

只是想缩小我需要的一些程序集来摆脱我的错误。 谢谢雷莫斯

using System.Linq;  //correct the .Any() error in the IsServiceType
using Ninject;
using Ninject.Web.Common;
using Ninject.Extensions.Conventions; //Corrected the error with kernel.bind

已更改.BindToDefaultInterface() - &gt; .BindDefaultInterface()

这是我的完整代码。希望能帮助别人。

[assembly: WebActivator.PreApplicationStartMethod(typeof(LongRanch.App_Start.NinjectWebCommon), "Start")]
[assembly: WebActivator.ApplicationShutdownMethodAttribute(typeof(LongRanch.App_Start.NinjectWebCommon), "Stop")]

namespace LongRanch.App_Start
{
    using System;
    using System.Web;
    using System.Linq;
    using Microsoft.Web.Infrastructure.DynamicModuleHelper;
    using Ninject;
    using Ninject.Web.Common;
    using Ninject.Extensions.Conventions;
    using System.Reflection;
    using System.IO;
    using LR.Repository;
    using LR.Repository.Interfaces;
    using LR.Service.Interfaces;
    using System.Web.Http;
    using LR.Service;

    public static class NinjectWebCommon
    {
        private static readonly Bootstrapper bootstrapper = new Bootstrapper();

        /// <summary>
        /// Starts the application
        /// </summary>
        public static void Start()
        {
            DynamicModuleUtility.RegisterModule(typeof(OnePerRequestHttpModule));
            DynamicModuleUtility.RegisterModule(typeof(NinjectHttpModule));

            bootstrapper.Initialize(CreateKernel);

        }

        /// <summary>
        /// Stops the application.
        /// </summary>
        public static void Stop()
        {
            bootstrapper.ShutDown();
        }

        /// <summary>
        /// Creates the kernel that will manage your application.
        /// </summary>
        /// <returns>The created kernel.</returns>
        private static IKernel CreateKernel()
        {
            var kernel = new StandardKernel();
            kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
            kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();

            RegisterServices(kernel);

            GlobalConfiguration.Configuration
               .ServiceResolver
               .SetResolver(t => kernel.TryGet(t),
                            t => kernel.GetAll(t));

            return kernel;
        }

        /// <summary>
        /// Load your modules or register your services here!
        /// </summary>
        /// <param name="kernel">The kernel.</param>
        private static void RegisterServices(IKernel kernel)
        {
            //kernel.Bind(scanner => scanner.FromAssembliesInPath(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location))
            kernel.Bind(scanner => scanner.From("LR.Repository", "LR.Service")
                .Select(IsServiceType)
                .BindDefaultInterface()
                .Configure(binding => binding.InSingletonScope())
            );

        }

        private static bool IsServiceType(Type type)
        {
            return type.IsClass && type.GetInterfaces().Any(intface => intface.Name == "I" + type.Name);
        }
    }
}