Ninject 3.0 MVC kernel.bind error Auto Registration
Posted
by
user295734
on Stack Overflow
See other posts from Stack Overflow
or by user295734
Published on 2012-04-11T01:55:33Z
Indexed on
2012/04/11
23:30 UTC
Read the original article
Hit count: 813
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);
}
© Stack Overflow or respective owner