Ninject for Web Site and Api - Sequence contains no elements

[亡魂溺海] 提交于 2019-12-12 09:52:46

问题


I have a single VS2010 solution with a Web.Api project and an MVC 3 project.

Both the Web.APi and the MVC project have their own App_Start with NinjectWebCommon in and their own bindings declared in there.

When trying to use the Api I always get the following message:

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 30: DynamicModuleUtility.RegisterModule(typeof(OnePerRequestHttpModule)); Line 31: DynamicModuleUtility.RegisterModule(typeof(NinjectHttpModule)); Line 32: bootstrapper.Initialize(CreateKernel); Line 33: } Line 34:

Source File: D:\Code\GreenDeal\PoS\GreenDeal.UI\App_Start\NinjectWebCommon.cs Line: 32

Stack Trace:

[InvalidOperationException: Sequence contains no elements] System.Linq.Enumerable.Single(IEnumerable1 source) +371 Ninject.Web.Mvc.NinjectMvcHttpApplicationPlugin.Start() in c:\Projects\Ninject\ninject.web.mvc\mvc3\src\Ninject.Web.Mvc\NinjectMvcHttpApplicationPlugin.cs:53 Ninject.Infrastructure.Language.ExtensionsForIEnumerableOfT.Map(IEnumerable1 series, Action1 action) in c:\Projects\Ninject\ninject\src\Ninject\Infrastructure\Language\ExtensionsForIEnumerableOfT.cs:32 Ninject.Web.Common.Bootstrapper.Initialize(Func1 createKernelCallback) in c:\Projects\Ninject\Ninject.Web.Common\src\Ninject.Web.Common\Bootstrapper.cs:53 XXXX.UI.App_Start.NinjectWebCommon.Start() in D:\Code\XXXX\PoS\XXXX.UI\App_Start\NinjectWebCommon.cs:32

I do not get this error if I remove the WebActivator.PreApplicationStartMethod from the Api or the MVC site.

I presume there is an issue with having the two Starts but am not sure what to do? Can I make them co-exist? Should I split into 2 solutions and hope it goes away? When deployed the site and the api will reside on separate servers and the site will consume the api.


回答1:


Went back to basics.

Followed the steps defined in here: http://www.eyecatch.no/blog/2012/06/using-ninject-with-webapi-rc/

Still had issues but that was because the MVC, client site had a reference to ninject.webapi. Removed that and all works.



来源:https://stackoverflow.com/questions/14138503/ninject-for-web-site-and-api-sequence-contains-no-elements

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!