如何在ASP.NET MVC Web App中使用Ninject?

我已经创建了一个新的MVC Web应用程序,并且我引用了Ninject.dll,Ninject.Web.Common.dll和Ninject.Web.MVC.dll。

的Global.asax.cs:

public class MvcApplication : NinjectHttpApplication { public static void RegisterGlobalFilters(GlobalFilterCollection filters) { filters.Add(new HandleErrorAttribute()); } public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( "Default", // Route name "{controller}/{action}/{id}", // URL with parameters new { controller = "Home", action = "Index", id = UrlParameter.Optional }); } protected override IKernel CreateKernel() { var kernel = new StandardKernel(); kernel.Load(Assembly.GetExecutingAssembly()); return kernel; } protected override void OnApplicationStarted() { base.OnApplicationStarted(); AreaRegistration.RegisterAllAreas(); RegisterGlobalFilters(GlobalFilters.Filters); RegisterRoutes(RouteTable.Routes); } } 

App_start \ NinjectWebCommon:

 public static class NinjectWebCommon { private static readonly Bootstrapper bootstrapper = new Bootstrapper(); ///  /// Starts the application ///  public static void Start() { DynamicModuleUtility.RegisterModule(typeof(OnePerRequestHttpModule)); DynamicModuleUtility.RegisterModule(typeof(NinjectHttpModule)); bootstrapper.Initialize(CreateKernel); } ///  /// Stops the application. ///  public static void Stop() { bootstrapper.ShutDown(); } ///  /// Creates the kernel that will manage your application. ///  /// The created kernel. private static IKernel CreateKernel() { var kernel = new StandardKernel(); kernel.Bind<Func>().ToMethod(ctx => () => new Bootstrapper().Kernel); kernel.Bind().To(); RegisterServices(kernel); return kernel; } ///  /// Load your modules or register your services here! ///  /// The kernel. private static void RegisterServices(IKernel kernel) { } } 

我收到错误“序列不包含任何元素”。 我究竟做错了什么?

错误详情:

 Description: An unhandled exception occurred during the execution of the current web request. Examine the stack trace for more information about this error and where it originated in the code. Exception Details: System.InvalidOperationException: Sequence contains no elements Source Error: Unhandled exception occurred during execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below. Stack Trace: [InvalidOperationException: Последовательность не содержит элементов] System.Linq.Enumerable.Single(IEnumerable`1 source) +320 Ninject.Web.Mvc.NinjectMvcHttpApplicationPlugin.Start() in c:\Projects\Ninject\ninject.web.mvc\mvc3\src\Ninject.Web.Mvc\NinjectMvcHttpApplicationPlugin.cs:53 Ninject.Web.Common.Bootstrapper.b__0(INinjectHttpApplicationPlugin c) in c:\Projects\Ninject\Ninject.Web.Common\src\Ninject.Web.Common\Bootstrapper.cs:52 Ninject.Infrastructure.Language.ExtensionsForIEnumerableOfT.Map(IEnumerable`1 series, Action`1 action) in c:\Projects\Ninject\ninject\src\Ninject\Infrastructure\Language\ExtensionsForIEnumerableOfT.cs:32 Ninject.Web.Common.Bootstrapper.Initialize(Func`1 createKernelCallback) in c:\Projects\Ninject\Ninject.Web.Common\src\Ninject.Web.Common\Bootstrapper.cs:52 Ninject.Web.Common.NinjectHttpApplication.Application_Start() in c:\Projects\Ninject\Ninject.Web.Common\src\Ninject.Web.Common\NinjectHttpApplication.cs:80 

您是从NinjectHttpApplication派生的, NinjectHttpApplication您正在同时使用App_Start 。 选一个! 阅读Ninject.MVC3的文档以获取更多信息。

要明确说明这一点,如果使用NuGet添加’Ninject.Mvc3’包(我使用的是3.0.0.6版), 则无需global.asax.cs 进行任何修改 。 通过在MVC 4项目的App_Start文件夹中创建NinjectWebCommon类,NuGet包为您提供了神奇的function。

我说这是因为我似乎跟着原始海报的类似教程(我跟着一篇关于代码项目的文章,名为’ asp.net mvc4中的dependency injection和使用Ninject的webapi’ ),并且与原始版本完全相同海报。 代码项目文章没有明确表示您应该使用NuGet(并且不要触摸global.asax.cs 手动添加Ninject引用(并修改global.asax.cs )。

确保您没有引用也使用NinjectMVC3 App_Start 。 删除对此类的引用后,我的项目开始工作。 此外,如前所述,检查名称空间是否匹配并且是正确的。