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

2024-03-18

kernel.Bind( 上的获取和错误scanner=> ... 在 VS 2010 中,“scanner”下面有一条小错误线。

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

尝试像 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);
            }

你必须取消注释

//using Ninject.Extensions.Conventions;
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Ninject 3.0 MVC kernel.bind 错误自动注册 的相关文章

随机推荐