【问题标题】:Erro on Ninject: Sequence contains no elementsNinject 上的错误:序列不包含任何元素
【发布时间】:2015-09-09 13:06:58
【问题描述】:

当我在 Azure 服务器上发布时出现此错误:
序列不包含元素

我不确定这个错误是由 Ninject 引起的。我已经尝试了所有类似错误的提示,但似乎这是一个一般性错误。
我尝试在多台机器上运行相同的代码,但只有在公共 Azure 时才会出现此错误。
如果您遇到了一些事情并且可以提供帮助,非常感谢您。

堆栈跟踪

[InvalidOperationException: Sequence contains no elements] 
   System.Linq.Enumerable.Single(IEnumerable`1 source) +311
   Ninject.Web.Mvc.NinjectMvcHttpApplicationPlugin.Start() +44
   Ninject.Web.Common.Bootstrapper.<Initialize>b__0(INinjectHttpApplicationPlugin c) +8
   Ninject.Infrastructure.Language.ExtensionsForIEnumerableOfT.Map(IEnumerable`1 series, Action`1 action) +130
   Ninject.Web.Common.Bootstrapper.Initialize(Func`1 createKernelCallback) +140
   Splora.SploraV1.UI.Web.NinjectWebCommon.Start() +102

[TargetInvocationException: Exception has been thrown by the target of an invocation.]
   System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor) +0
   System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments) +192
   System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) +136
   WebActivatorEx.BaseActivationMethodAttribute.InvokeMethod() +73
   WebActivatorEx.ActivationManager.RunActivationMethods(Boolean designerMode) +350
   WebActivatorEx.ActivationManager.Run() +78

[InvalidOperationException: The pre-application start initialization method Run on type WebActivatorEx.ActivationManager threw an exception with the following error message: Exception has been thrown by the target of an invocation..]
   System.Web.Compilation.BuildManager.InvokePreStartInitMethodsCore(ICollection`1 methods, Func`1 setHostingEnvironmentCultures) +613
   System.Web.Compilation.BuildManager.InvokePreStartInitMethods(ICollection`1 methods) +141
   System.Web.Compilation.BuildManager.CallPreStartInitMethods(String preStartInitListPath, Boolean& isRefAssemblyLoaded) +159
   System.Web.Compilation.BuildManager.ExecutePreAppStart() +157
   System.Web.Hosting.HostingEnvironment.Initialize(ApplicationManager appManager, IApplicationHost appHost, IConfigMapPathFactory configMapPathFactory, HostingEnvironmentParameters hostingParameters, PolicyLevel policyLevel, Exception appDomainCreationException) +656

[HttpException (0x80004005): The pre-application start initialization method Run on type WebActivatorEx.ActivationManager threw an exception with the following error message: Exception has been thrown by the target of an invocation..]
   System.Web.HttpRuntime.FirstRequestInit(HttpContext context) +4531288
   System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context) +94
   System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr, HttpContext context) +191   



NinjectWebCommon.cs:

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 = RegisterServices();
        try
        {
            kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
            kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();

            return kernel;
        }
        catch
        {
            kernel.Dispose();
            throw;
        }
    }


    private static StandardKernel RegisterServices()
    {
        return new Container().GetModules();
    } 

【问题讨论】:

  • 但是你的代码在哪里?
  • 执行Enumerable.Single 调用的代码是什么? Single 在执行什么操作?
  • 我不知道他会在哪里抛出错误。我无法调试,因为这个云。
  • 是的,但至少你可以看看Ninject.Web.Mvc.NinjectMvcHttpApplicationPlugin.Start() 的源代码,看看它在哪里调用Single
  • 调试帮助问题必须提供Minimal, Complete, and Verifiable example,另见here

标签: c# .net azure ninject publish


【解决方案1】:

我发现了问题,在 Azure 上发布时,我没有在发布屏幕的“设置”选项卡上选择“在目标位置删除其他文件”选项。

因此,发布并没有删除我不再使用的旧 DLL。 我清除了 Azure 上的所有文件并再次发布,它工作正常。

之后,我在发布时选择了“在目标位置删除其他文件”选项,但它没有再次发生。

【讨论】:

  • 谢谢马库斯。这对我有帮助。我(愚蠢地)尝试了从 Bitbucket 到 Azure 的持续集成选项,这对我来说简直是一团糟。当我回到手动发布时,我遇到了和你一样的错误。
【解决方案2】:

在我的情况下,这个问题发生在 UAT 中,但不在我的机器上。

原因是我更改了命名空间,结果在发布文件夹中有两个具有相同类的库。

我不得不清理发布文件夹并再次发布。 现在都好了

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-17
    • 1970-01-01
    • 2015-09-04
    • 2017-11-21
    • 1970-01-01
    相关资源
    最近更新 更多