2012-04-11 53 views
3

获取和错误上kernel.Bind(扫描器 => ... “扫描仪” 已在VS它下面的小错误行2010年Ninject 3.0 MVC kernel.bind错误自动注册

Cannot convert lambda expression to type 'System.Type[]' because it is not a delegate type

Tyring为自动注册像老kernel.scan 2.0英寸 我想不出什么我做错了,添加和删除这么多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); 
      } 

回答

12

你必须取消注释

//using Ninject.Extensions.Conventions; 
+0

已经试过了。我正在尝试评论和取消注释的所有组合。 Wheni取消注释,它不仅有扫描仪错误,它现在说“BindToDefaultInterface”是错误的。错误:'Ninject.Extensions.Conventions.Syntax.IJoinExcludeIncludeBindSyntax'不包含'BindToDefaultInterface'的定义,并且没有找到接受'Ninject.Extensions.Conventions.Syntax.IJoinExcludeIncludeBindSyntax'类型的第一个参数的扩展方法'BindToDefaultInterface'你是否缺少使用指令或程序集引用?) – 2012-04-11 12:00:25

+0

它是BindDefaultInterface – 2012-04-11 13:06:14

+0

在我的设置某处,必须是一个更大的问题。 .BindToDefaultInterface()获得上面的VS2010错误。当Ninject.Extensions.Conventions未被注释时,甚至不会构建。 – 2012-04-11 13:52:26

2

只想凝结一些组件,我需要摆脱我的错误的。 感谢Remos

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() - > .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); 
     } 
    } 
} 
+0

这工作,谢谢 – 2014-11-18 09:11:44