"Server Error in '/' Application. Sequence contains no elements" after refactoring namespace
Asked Answered
R

3

10

I'm using MVC 4 and Ninject 3 with NinjectWebCommon in the App_Start folder.

And my Global.asax.cs is MvcApplication : HttpApplication

I'm getting the error below because the Ninject is starting twice - Why?

Server Error in '/' Application.

Sequence contains no elements

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.InvalidOperationException: Sequence contains no elements

Source Error: 


Line 50:             kernelInstance = createKernelCallback();
Line 51: 
Line 52:             kernelInstance.Components.GetAll<INinjectHttpApplicationPlugin>().Map(c => c.Start());
Line 53:             kernelInstance.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();
Line 54:             kernelInstance.Inject(this);

Source File: c:\Projects\Ninject\Ninject.Web.Common\src\Ninject.Web.Common\Bootstrapper.cs    Line: 52 

Stack Trace: 


[InvalidOperationException: Sequence contains no elements]
   System.Linq.Enumerable.Single(IEnumerable`1 source) +315
   Ninject.Web.Mvc.NinjectMvcHttpApplicationPlugin.Start() in c:\Projects\Ninject\ninject.web.mvc\mvc3\src\Ninject.Web.Mvc\NinjectMvcHttpApplicationPlugin.cs:53
   Ninject.Web.Common.Bootstrapper.<Initialize>b__0(INinjectHttpApplicationPlugin c) in c:\Projects\Ninject\Ninject.Web.Common\src\Ninject.Web.Common\Bootstrapper.cs:52
   Ninject.Infrastructure.Language.ExtensionsForIEnumerableOfT.Map(IEnumerable`1 series, Action`1 action) in c:\Projects\Ninject\ninject\src\Ninject\Infrastructure\Language\ExtensionsForIEnumerableOfT.cs:32
   Ninject.Web.Common.Bootstrapper.Initialize(Func`1 createKernelCallback) in c:\Projects\Ninject\Ninject.Web.Common\src\Ninject.Web.Common\Bootstrapper.cs:52
   Company.App.App_Start.NinjectWebCommon.Start() in c:\Development\Company\trunk\src\App\App_Start\NinjectWebCommon.cs:29

[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) +108
   System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters) +19
   WebActivator.BaseActivationMethodAttribute.InvokeMethod() +236
   WebActivator.ActivationManager.RunActivationMethods() +534
   WebActivator.ActivationManager.RunPreStartMethods() +41
   WebActivator.ActivationManager.Run() +64

[InvalidOperationException: The pre-application start initialization method Run on type WebActivator.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) +550
   System.Web.Compilation.BuildManager.InvokePreStartInitMethods(ICollection`1 methods) +132
   System.Web.Compilation.BuildManager.CallPreStartInitMethods(String preStartInitListPath) +90
   System.Web.Compilation.BuildManager.ExecutePreAppStart() +135
   System.Web.Hosting.HostingEnvironment.Initialize(ApplicationManager appManager, IApplicationHost appHost, IConfigMapPathFactory configMapPathFactory, HostingEnvironmentParameters hostingParameters, PolicyLevel policyLevel, Exception appDomainCreationException) +516

[HttpException (0x80004005): The pre-application start initialization method Run on type WebActivator.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) +9874568
   System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context) +101
   System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr, HttpContext context) +254

Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.18044
Redfield answered 24/9, 2013 at 22:12 Comment(0)
R
37

This was caused when I refactored the namespaces and binary name.

This meant that a old copy of the dll with the old name was still in the bin folder.

Which meant both dll's were being activated at start up.

A Clean did not remove it, so I deleted manually and the problem went away.

Redfield answered 25/9, 2013 at 20:2 Comment(5)
Thank you so much for this. However, not only did I have to remove the Bin folders, I had to run a disk clean-up to get the web project to build; I presume it was using old files in temporary storage.Rossman
Same for me, I had the same problem and no other solution worked.Orbit
+1 Deployed to multiple servers with same issue. I also renamed a namespace / assembly. I did a project clean, deleted the entire project from the published to server folder, and then republished. Worked like a charm!Lux
Old answer but useful even now :)Fret
In my case I renamed namespaces/binaries and was deploying to my existing Azure "staging" slot on App Service, which had old versions of the binaries still on it. Deleting the staging slot and recreating it meant that the next deployment created everything from scratch, and then the problem was fixed.Yield
S
1

I came across this issue and spent a couple hours trying various recommendations on the internet. The thing which fixed it in the end was to check for circular references across namespaces in my project. Have a look at your .csproj files and check all the references are correct

Sukkoth answered 5/4, 2016 at 3:44 Comment(0)
T
0

For me i'd accidentally installed ninject into a non web project, removing it from my data project solved the issue for me.

Trout answered 9/8, 2016 at 14:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.