Ninject.MVC3. Bootstrapper.Initialize throws "The sequence contains no elements"

this question is not new, but my problem seems to be different from the ones I have seen so far.

I have a solution containing several projects: two of them are C # MVC4. I installed the Ninject.MVC3 Nuget package for both, and I use the NinjectWebCommon class in the approach to the App_Start folder ( https://github.com/ninject/Ninject.Web.Mvc/wiki/Setting-up-an-MVC3-application ).

Versions:

  • Ninject 3.2.2.0
  • Ninject.MVC3 3.2.1.0
  • Ninject.Web.Common 3.2.3.0
  • Ninject.Web.Common.WebHost 3.2.3.0
  • WebActivator 2.0.5

NinjectWebCommon.cs of the first project:

using System.Web.Mvc; using Ninject.Web.Mvc.FilterBindingSyntax; using S1.MVC.Filters.CentralAutenticacao.Business; using S1.MVC.Filters.Error; [assembly: WebActivatorEx.PreApplicationStartMethod(typeof(S1.CRM.Eventos.App_Start.NinjectWebCommon), "Start")] [assembly: WebActivatorEx.ApplicationShutdownMethodAttribute(typeof(S1.CRM.Eventos.App_Start.NinjectWebCommon), "Stop")] namespace S1.CRM.Eventos.App_Start { using System; using System.Web; using Microsoft.Web.Infrastructure.DynamicModuleHelper; using Ninject; using Ninject.Web.Common; 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(); try { kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel); kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>(); RegisterServices(kernel); return kernel; } catch { kernel.Dispose(); throw; } } /// <summary> /// Load your modules or register your services here! /// </summary> /// <param name="kernel">The kernel.</param> private static void RegisterServices(IKernel kernel) { kernel.BindFilter<FiltroCentralAutenticacao>(FilterScope.Global, 0); kernel.BindFilter<GenericErro>(FilterScope.Global, 0); } } } 

NinjectWebCommon.cs of the second project:

 using System.Web.Mvc; using Ninject.Web.Mvc.FilterBindingSyntax; using S1.MVC.Filters.CentralAutenticacao.Business; using S1.MVC.Filters.Error; [assembly: WebActivatorEx.PreApplicationStartMethod(typeof(S1.CRM.Crud.App_Start.NinjectWebCommon), "Start")] [assembly: WebActivatorEx.ApplicationShutdownMethodAttribute(typeof(S1.CRM.Crud.App_Start.NinjectWebCommon), "Stop")] namespace S1.CRM.Crud.App_Start { using System; using System.Web; using Microsoft.Web.Infrastructure.DynamicModuleHelper; using Ninject; using Ninject.Web.Common; 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(); try { kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel); kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>(); RegisterServices(kernel); return kernel; } catch { kernel.Dispose(); throw; } } /// <summary> /// Load your modules or register your services here! /// </summary> /// <param name="kernel">The kernel.</param> private static void RegisterServices(IKernel kernel) { kernel.BindFilter<FiltroCentralAutenticacao>(FilterScope.Global, 0); kernel.BindFilter<GenericErro>(FilterScope.Global, 0); } } } 

Here is the global.asax file of the first project:

 using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Web; using System.Web.Http; using System.Web.Mvc; using System.Web.Optimization; using System.Web.Routing; namespace S1.CRM.Eventos { // Note: For instructions on enabling IIS6 or IIS7 classic mode, // visit http://go.microsoft.com/?LinkId=9394801 public class MvcApplication : HttpApplication { protected void Application_Start() { AreaRegistration.RegisterAllAreas(); WebApiConfig.Register(GlobalConfiguration.Configuration); FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); RouteConfig.RegisterRoutes(RouteTable.Routes); BundleConfig.RegisterBundles(BundleTable.Bundles); AuthConfig.RegisterAuth(); } } } 

and second:

 using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Http; using System.Web.Mvc; using System.Web.Optimization; using System.Web.Routing; namespace S1.CRM.Crud { // Note: For instructions on enabling IIS6 or IIS7 classic mode, // visit http://go.microsoft.com/?LinkId=9394801 public class MvcApplication : HttpApplication { protected void Application_Start() { AreaRegistration.RegisterAllAreas(); WebApiConfig.Register(GlobalConfiguration.Configuration); FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); RouteConfig.RegisterRoutes(RouteTable.Routes); BundleConfig.RegisterBundles(BundleTable.Bundles); AuthConfig.RegisterAuth(); } } } 

When I try to run either of the two projects, I get an InvalidOperationException saying "The sequence contains no elements" when called

 bootstrapper.Initialize(CreateKernel); 

Stacktrace:

 in System.Linq.Enumerable.Single[TSource](IEnumerable`1 source) in Ninject.Web.Mvc.NinjectMvcHttpApplicationPlugin.Start() in Ninject.Web.Common.Bootstrapper.<Initialize>b__0(INinjectHttpApplicationPlugin c) in Ninject.Infrastructure.Language.ExtensionsForIEnumerableOfT.Map[T](IEnumerable`1 series, Action`1 action) in Ninject.Web.Common.Bootstrapper.Initialize(Func`1 createKernelCallback) in S1.CRM.Eventos.App_Start.NinjectWebCommon.Start() in d:\git-paulo\S1.CRM\S1.CRM.Eventos\App_Start\NinjectWebCommon.cs:line 30 

Some people have this problem when they make global.asax derived from NinjectHttpApplication and also use the NinjectWebCommon class or when they rename assemblies ( Ninject + MVC3 = InvalidOperationException: The sequence does not contain elements ). This is none of my business.

Another guy got this error when two projects in one solution, where using WebActivator to initialize Ninject ( Ninject for website and Api-Sequence does not contain elements ). So I tried to unload one of the projects, but still kept getting an error.

Any ideas on what's going on?

+5
source share
3 answers

Indeed, a different project using WebActivator was used in the solution: the class library referenced by one of the MVC projects. I did not suspect this, because it makes no sense to have a WebActivator there.

+8
source

This error also occurs if links exist. Has 2 code files + NinjectWebCommon.cs (usually located in the App_Start folder) in the solution.

If there are several Initialize () methods, this also results in an error similar to " The sequence contains no elements "

+1
source

I experienced "Sequence contains no elements" due to my links. Please check your recommendations. If there is a link to another project that contains the Ninject Infrastructure directory, it also causes an error. If you delete, the problem can be resolved.

0
source

Source: https://habr.com/ru/post/1205780/


All Articles