C# ASP.NET MVC4中的Ninject
因此,在经过大量的修改之后,我终于将Ninject连接到我的MVC4应用程序中并进行编译。我遇到的问题是,据我所知,idependencycoscope接口已不存在,System.Web.Http.Dependencies命名空间已被删除 因此,我现在的问题是,在运行我得到的应用程序时,我将所有东西都连接到了网络中:C# ASP.NET MVC4中的Ninject,c#,ninject,asp.net-mvc-4,C#,Ninject,Asp.net Mvc 4,因此,在经过大量的修改之后,我终于将Ninject连接到我的MVC4应用程序中并进行编译。我遇到的问题是,据我所知,idependencycoscope接口已不存在,System.Web.Http.Dependencies命名空间已被删除 因此,我现在的问题是,在运行我得到的应用程序时,我将所有东西都连接到了网络中: Sequence contains no elements [InvalidOperationException: Sequence contains no el
Sequence contains no elements
[InvalidOperationException: Sequence contains no elements]
System.Linq.Enumerable.Single(IEnumerable`1 source) +379
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:31
Ninject.Web.Common.Bootstrapper.Initialize(Func`1 createKernelCallback) in c:\Projects\Ninject\Ninject.Web.Common\src\Ninject.Web.Common\Bootstrapper.cs:53
Ninject.Web.Common.NinjectHttpApplication.Application_Start() in c:\Projects\Ninject\Ninject.Web.Common\src\Ninject.Web.Common\NinjectHttpApplication.cs:81
看看这本书。昨晚我刚刚将这段代码从MVC3移植到MVC4,并且工作正常。确切地说是第322页
我看不到的是你在哪里映射你的接口到你的具体项目
Bind<ISomething>().To<Something>();
就这样
于2012年11月14日更新
另一方面,如果您使用的是MVC WebAPI,那么您将希望使用来自nuget的。此外,请查看他们样本中的“联系人经理”。这有助于清理Ninject的实现。好吧,在我的头撞墙太久之后,我弄明白了到底发生了什么。在.NET 4.5上运行的MVC4的默认项目类型引用了System.Web.Http的原始RC版本,而不是更新版本 名称空间丢失,对象不存在,生活不好 解决步骤:
更新通过将MVC从MVC4 Beta更新为MVC4 RC解决了这一问题。当您将从NuGet软件包安装最新的Ninject.MVC3时,我们在
NinjectWebCommon.cs
文件顶部找到以下代码:
[assembly: WebActivator.PreApplicationStartMethod(typeof(MvcApplication1.App_Start.NinjectWebCommon), "Start")]
[assembly: WebActivator.ApplicationShutdownMethodAttribute(typeof(MvcApplication1.App_Start.NinjectWebCommon), "Stop")]
在这种情况下,我们不需要在global.asax中显式注册ninject
我发现在MVC4中使用Ninject有一个很好的内容我倾向于将我的Ninject引导保存在一个单独的项目中。为了使用
IBindingInSyntax
的.InRequestScope()
扩展方法,我通过Nuget添加了Ninject.Web.Common库。唉,这个库包括app_start引导程序,通过WebActivator产生了重复的NinjectWebCommon类和附件(在所述项目中1个,在MVC项目本身中1个)
我从我的引导项目中删除了重复的App_Start文件夹,这就解决了它。我遇到了同样的问题,不太确定在以下更改后修复了什么 将Ninject.MVC4添加到项目中
已删除NinjectWebCommon.cs(生成的文件,因为集成已存在于global.ascx.cs文件中)我正在使用DD4T,并遇到相同的错误 在确认nuget package manager安装了所有软件包之后, 我发现缺少一些DLL/引用(newtonsoft等): 然后,在重新安装Newtonsoft.Json(要重新安装软件包,请在Nuget软件包管理器中使用以下命令:更新软件包–重新安装Newtonsoft.Json),并从Tridion Deployer bin中放入netrtsn.dll后,我得到了一个错误——“序列不包含元素”,其堆栈跟踪与本问题中给出的完全相同 感谢Naga提供此决议
删除了NinjectWebCommon.cs(生成的文件,因为该集成已经存在于global.ascx.cs文件中)和wohoooo!!!!所有错误都已解决,Tridion+MVC4=DD4T现在运行正常。我在实际MVC网站项目引用的项目中使用nuget安装Ninject.MVC4时也遇到了这个问题
问题在于,自动安装在引用项目的App_Start目录中的NinjectWebCommon.cs文件与安装在我的网站项目中的(实际的、有用的)文件冲突。从引用的项目中删除NinjectWebCommon.cs文件可以解决此错误。只需从项目中删除
NinjectWebCommon.cs
文件(它位于App_Start
文件夹中)。一切都应该正常
来源:任何想要谷歌搜索答案的人都可能得不到答案,但欢迎您尝试。在谷歌的前10页中,我已经阅读了通过自己的搜索找到的每一篇文章。我已经在我的MVC4应用程序中安装了Ninject.MVC3 nuget包,DI刚刚开箱即用。可以尝试一下吗?这里也是一样,MVC4和Ninject.MVC3包非常有用。。。虽然在.NET4上,而不是在4.5上…我认为这是这里的难点。。。System.Web.Http命名空间中发生了一些更改,并且删除了依赖项NamaSpace。IDependencyResolver现在位于System.Web.Http.Services内部,不在依赖项下。您好,让标题简要说明问题是什么总是有帮助的。我已经看到了关于这本书的博客文章。它在.NET4.0下工作,而不是在.NET4.5下工作。您是否遇到IDependencyScope不再存在或命名空间System.Web.Http.Dependencies不再存在?如果你这样做了,我们也许可以交谈,但给我一个亚马逊图书的链接绝对是对我的帮助
ModelValidatorProviders.Providers.Remove(ModelValidatorProviders.Providers.OfType<DataAnnotationsModelValidatorProvider>().Single());
public void Start()
{
ModelValidatorProviders.Providers.Remove(ModelValidatorProviders.Providers.OfType<DataAnnotationsModelValidatorProvider>().Single());
DependencyResolver.SetResolver(this.CreateDependencyResolver());
RemoveDefaultAttributeFilterProvider();
}
protected override IKernel CreateKernel()
{
var kernel = new StandardKernel();
kernel.Load(Assembly.GetExecutingAssembly());
kernel.Bind<IRenderHelper>().To<RenderHelper>();
kernel.Unbind<IDocumentViewerAdapter>();
GlobalConfiguration.Configuration.ServiceResolver.SetResolver(new NinjectDependencyResolver(kernel));
ModelValidatorProviders.Providers.Add(new DataAnnotationsModelValidatorProvider());
FilterProviders.Providers.Add(new FilterAttributeFilterProvider());
return kernel;
}
Exception Details: Ninject.ActivationException: Error activating IntPtr
No matching bindings are available, and the type is not self-bindable.
Activation path:
3) Injection of dependency IntPtr into parameter method of constructor of type Func{IKernel}
2) Injection of dependency Func{IKernel} into parameter lazyKernel of constructor of type HttpApplicationInitializationHttpModule
1) Request for IHttpModule
Suggestions:
1) Ensure that you have defined a binding for IntPtr.
2) If the binding was defined in a module, ensure that the module has been loaded into the kernel.
3) Ensure you have not accidentally created more than one kernel.
4) If you are using constructor arguments, ensure that the parameter name matches the constructors parameter name.
5) If you are using automatic module loading, ensure the search path and filters are correct.
Bind<ISomething>().To<Something>();
public NinjectDependencyResolver() {
_kernal = new StandardKernel();
RegisterServices(_kernel);
}
public static void RegisterServices(IKernel kernel) {
kernel.Bind<ISomething>().To<Something>();
}
public class NinjectDependencyResolver : IDependencyResolver {
private IKernal _kernel;
public NinjectDependencyResolver(){
_kernal = StandardKernal();
AddBindings();
}
public NinjectDependencyResolver(IKernel kernel)
{
_kernel = kernel;
}
public object GetService(Type serviceType)
{
return _kernel.TryGet(serviceType);
}
public IEnumerable<object> GetServices(Type serviceType)
{
return _kernal.GetAll(serviceType);
}
public IBindingToSyntax<T> Bind<T>() {
return _kernal.Bind<T>();
}
public static void RegisterServices(IKernel kernel){
//Add your bindings here.
//This is static as you can use it for WebApi by passing it the IKernel
}
}
DependencyResolver.SetResolver(new NinjectDependencyResolver());
[assembly: WebActivator.PreApplicationStartMethod(typeof(MvcApplication1.App_Start.NinjectWebCommon), "Start")]
[assembly: WebActivator.ApplicationShutdownMethodAttribute(typeof(MvcApplication1.App_Start.NinjectWebCommon), "Stop")]