简体   繁体   English

' 初始化 ninject 时序列不包含任何元素的异常

[英]' Sequence contains no elements' exception when initializing ninject

I have a problem with Ninject which I have not seen before and don't know how to fix.我有一个 Ninject 问题,这是我以前从未见过的,不知道如何解决。 I am using the MVC NuGet package for MVC 5.我正在为 MVC 5 使用 MVC NuGet 包。

In my NinjectWebCommon.cs, there is the following method:在我的 NinjectWebCommon.cs 中,有以下方法:

    public static void Start() 
    {
        DynamicModuleUtility.RegisterModule(typeof(OnePerRequestHttpModule));
        DynamicModuleUtility.RegisterModule(typeof(NinjectHttpModule));
        bootstrapper.Initialize(CreateKernel);
    }

However, it falls over when trying to execute the last line, complaining that the "sequence contains no elements".但是,它在尝试执行最后一行时失败,抱怨“序列不包含任何元素”。 I was previously using Unity for DI but have changed to Ninject.我以前使用 Unity 进行 DI,但已更改为 Ninject。

Any ideas?有任何想法吗?

Thanks in advance, M提前致谢,M

In my case, my project had two NinjectWebCommon.cs folders with the same/similar content.就我而言,我的项目有两个具有相同/相似内容的 NinjectWebCommon.cs 文件夹。 Dropping the folder has resolved the issue.删除文件夹已解决问题。

For my two penneth and related to @Pavan's post.对于我的两个 Penneth 并与@Pavan 的帖子相关。 I started receiving the error after renaming an assembly.重命名程序集后,我开始收到错误消息。 I had the old DLL lingering in the bin folder.我在 bin 文件夹中保留了旧的 DLL。 Cleaning the solution didn't fix it.清洁解决方案并没有解决它。 I had to go in and delete the contents of bin by hand.我不得不手动删除 bin 的内容。

I faced this type of error while preparing a new solution by replacing File Name and other text in Entire solution.通过替换整个解决方案中的文件名和其他文本来准备新解决方案时,我遇到了这种类型的错误。

Everything was perfect like buld the application.一切都很完美,就像应用程序一样。

But when i go to run the application it says me "Sequence contains no elements" error.但是当我去运行应用程序时,它说“序列不包含元素”错误。

And finally i finally resolve that issue by using following.....最后我终于通过使用以下.....解决了这个问题

  1. open every project folder by right click on the project >> Then select "Open Folder in File Explorer通过右键单击项目打开每个项目文件夹>>然后选择“在文件资源管理器中打开文件夹

  2. Remove 'bin' and 'obj' folder删除“bin”和“obj”文件夹

  3. Clean Entire project清洁整个项目

  4. Build application构建应用程序

  5. and finally run the application.最后运行应用程序。

Hopefully, it will work for you....希望它对你有用......

Thanks...谢谢...

I think this is a dupe of this question: Ninject + MVC3 = InvalidOperationException: Sequence contains no elements我认为这是对这个问题的欺骗: Ninject + MVC3 = InvalidOperationException: Sequence contains no elements

TL;DR - make sure you clear out all old dlls when you publish your site. TL;DR - 确保在发布站点时清除所有旧的 dll。

An additional explanation detail to extend the helpful answer by @Greg_B:一个额外的解释细节,以扩展@Greg_B 的有用答案:

A reference to another project in the solution that also uses Ninject.对解决方案中另一个也使用 Ninject 的项目的引用。 Removing this reference solves the problem.删除此引用可以解决问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM