3

Getting and error on kernel.Bind(scanner => ... "scanner" has the little error line under it in VS 2010.

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

Tyring to Auto Register like the old kernel.scan in 2.0. I can not figure out what i am doing wrong. Added and removed so many Ninject packages. completely lost, getting to be a big waste of time.

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);
            }
tereško
  • 58,060
  • 25
  • 98
  • 150
Yogurt The Wise
  • 4,379
  • 4
  • 34
  • 42

2 Answers2

12

You have to uncomment

//using Ninject.Extensions.Conventions;
Remo Gloor
  • 32,665
  • 4
  • 68
  • 98
  • Tried that already. I was trying all combinations of commenting and uncommenting stuff. Wheni Uncomment it, it not only has the Scanner error, it now says "BindToDefaultInterface" is wrong. error: 'Ninject.Extensions.Conventions.Syntax.IJoinExcludeIncludeBindSyntax' does not contain a definition for 'BindToDefaultInterface' and no extension method 'BindToDefaultInterface' accepting a first argument of type 'Ninject.Extensions.Conventions.Syntax.IJoinExcludeIncludeBindSyntax' could be found (are you missing a using directive or an assembly reference?) – Yogurt The Wise Apr 11 '12 at 12:00
  • Must be a bigger issue with my setup somewhere. .BindToDefaultInterface() gets a VS2010 error above. Won't even build, when the Ninject.Extensions.Conventions is uncommented. – Yogurt The Wise Apr 11 '12 at 13:52
  • 2
    Again it is not BindToDwfaultInterface but BindDefaultInterface – Remo Gloor Apr 11 '12 at 22:39
  • Thanks, did not pick up on the spelling. Errors have disappeared. – Yogurt The Wise Apr 11 '12 at 22:53
  • Got another bug. wiht the .Select(IsServiceType) --- return type.IsClass && type.GetInterfaces().Any(intface => intface.Name == "I" + type.Name); The .Any is now erroring. 'System.Array' does not contain a definition for 'Any' and no extension method 'Any' accepting a first argument of type 'System.Array' could be found (are you missing a using directive or an assembly reference?) trying to figure out what assembly i am missing. – Yogurt The Wise Apr 11 '12 at 23:13
  • Got it. using System.Linq; needed to be added. Found that answer alot easier then the other stuff. – Yogurt The Wise Apr 11 '12 at 23:17
2

Just wanted to condense some of assemblies i needed to get rid of my errors. Thanks 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

changed .BindToDefaultInterface() --> .BindDefaultInterface()

Heres my full code thats works. Hopefully will help someone lse.

[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);
        }
    }
}
Yogurt The Wise
  • 4,379
  • 4
  • 34
  • 42