c# – Ninject.MVC3. Bootstrapper.Initialize throws“Sequence contains no elements”

前端之家收集整理的这篇文章主要介绍了c# – Ninject.MVC3. Bootstrapper.Initialize throws“Sequence contains no elements”前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
这个问题并不新鲜,但我的问题似乎与我迄今为止看到的问题有所不同.

我有一个包含几个项目的解决方案:其中两个是C#MVC4.我在两者上安装了Ninject.MVC3 Nuget包,我在App_Start文件方法(https://github.com/ninject/Ninject.Web.Mvc/wiki/Setting-up-an-MVC3-application)中使用了NinjectWebCommon类.

版本:

> 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的第一个项目:

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:

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);
        }        
    }
}

这是第一个项目的global.asax文件

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();
        }
    }
}

而第二个:

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();
        }
    }
}

当我尝试运行这两个项目中的任何一个时,我在调用时得到一个InvalidOperationException,说“Sequence contains no elements”

bootstrapper.Initialize(CreateKernel);

堆栈跟踪:

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

有些人在使用Global.asax派生自NinjectHttpApplication并使用NinjectWebCommon类或重命名程序集时(Ninject + MVC3 = InvalidOperationException: Sequence contains no elements)有这个问题.那不是我的情况.

其他人在使用WebActivator初始化Ninject(Ninject for Web Site and Api – Sequence contains no elements)的同一解决方案中的两个项目时遇到此错误.所以我试图卸载其中一个项目,但仍然不断收到错误.

关于发生了什么的任何想法?

解决方法

实际上,在解决方案中还有另一个使用WebActivator的项目:一个MVC项目引用的类库.我没有怀疑它,因为在那里拥有WebActivator没有任何意义.

猜你在找的C#相关文章