gpt4 book ai didi

c# - Ninject.MVC3。 Bootstrapper.Initialize 抛出 "Sequence contains no elements"

转载 作者:太空狗 更新时间:2023-10-29 21:17:18 25 4
gpt4 key购买 nike

这个问题并不新鲜,但我的问题似乎与我目前所见的问题有着不同的根源。

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

版本:

  1. 注入(inject) 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:

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);
kernel.BindFilter<GenericErro>(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,提示“序列不包含任何元素”

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# - Ninject.MVC3。 Bootstrapper.Initialize 抛出 "Sequence contains no elements",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26632107/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com