Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ninject.MVC3. Bootstrapper.Initialize throws "Sequence contains no elements"

this question is not new, but my problem seems to have a different root than those I have seen so far.

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

Versions:

  1. Ninject 3.2.2.0
  2. Ninject.MVC3 3.2.1.0
  3. Ninject.Web.Common 3.2.3.0
  4. Ninject.Web.Common.WebHost 3.2.3.0
  5. 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 of the second one:

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 any of the two projects, I get an InvalidOperationException saying "Sequence contains no elements" when calling

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 had this problem when they make global.asax derive from NinjectHttpApplication and also use NinjectWebCommon class, or when they rename assemblies (Ninject + MVC3 = InvalidOperationException: Sequence contains no elements). That's not my case.

Other guy got this error when two projects in the same solution where using WebActivator to initialize Ninject (Ninject for Web Site and Api - Sequence contains no elements). So I tried to unload one of the projects, but still kept getting the error.

Any ideas on what is going on?

like image 595
peflorencio Avatar asked Oct 29 '14 13:10

peflorencio


1 Answers

Indeed there was another project using WebActivator in the solution: a class library referenced by one of the MVC projects. I didn't suspect of it, because it doesn't make any sense to have WebActivator in there.

like image 90
peflorencio Avatar answered Oct 03 '22 21:10

peflorencio