问题标签 [ninject-3]
For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.
ninject - Ninject 构造函数绑定
我正在使用 NInject。我有一个像这样的对象
我希望能够通过只传递魔法来注入一些 foo 。每个类的魔法是不同的,所以这应该返回不同的实例。
我第一次尝试绑定它我已经完成了
但是后来我必须用看起来有点不合适的东西来查找它。
我不想明确地将自己绑在魔线上。
我得到的印象Bind<IFoo>().ToConstructor(ctorArg => new Foo(xxx, ctorArg.Kernel.Get<IFoo>());
几乎可以让我到达那里,但我不知道我应该为“xxx”输入什么,以便它查找参数。
ninject - 在 Ninject 2+ 中禁用非显式绑定类的隐式绑定/注入
如果您从 NInject 请求未绑定的对象,则默认行为是(如果有合适的构造函数可用)似乎是创建适当对象的实例。
我想禁用此行为(我遇到了一个难以调试的问题,因为某些东西是自动绑定的,而不是在模块中获取我的自定义绑定)。 这个问题暗示这是可能的,但我无法从 NInject wiki 中找到答案。
inversion-of-control - Ninject 约定与 Ninject 工厂扩展将多种类型绑定到一个接口
我正在尝试扩展题为Ninject Factory Extension Bind Multiple Concrete Types To One Interface by using Ninject Conventions for 基于约定的ICar
实现绑定的 SO 问题中提出的场景。
我正在研究由 Akim和他的 Gist撰写的公认答案,概述了完整的示例。
不同之处在于我已经用ICar
基于约定的绑定替换了显式绑定(或者至少尝试了它;)
当我尝试car
在测试中实例化变量时,我得到ActivationException
:
我怎样才能让这个测试通过?
这是其余的代码,尽可能精简,这样您就不必参考原始问题
asp.net - Ninject ASP.NET extension throwing TypeLoadException
I'm trying to use Ninject 3 in an ASP.NET webforms application. We've been using it without the Ninject.Web extension for a while by creating a new injection kernel everywhere we needed one. It didn't occur to me until now that that's not the right way to do things. I wrote a little test application that is successfully using Ninject.Web, but when I tried to do the same thing in our real application, I'm receiving a TypeLoadException as soon as the page loads. It doesn't even make it into my code where I can set a breakpoint. Here's the exception and stack trace:
Could not load type 'System.Runtime.CompilerServices.ExtensionAttribute' from assembly 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.
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.TypeLoadException: Could not load type 'System.Runtime.CompilerServices.ExtensionAttribute' from assembly 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.
Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
Stack Trace:
[TypeLoadException: Could not load type 'System.Runtime.CompilerServices.ExtensionAttribute' from assembly 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.]
System.ModuleHandle.ResolveType(RuntimeModule module, Int32 typeToken, IntPtr* typeInstArgs, Int32 typeInstCount, IntPtr* methodInstArgs, Int32 methodInstCount, ObjectHandleOnStack type) +0
System.ModuleHandle.ResolveTypeHandleInternal(RuntimeModule module, Int32 typeToken, RuntimeTypeHandle[] typeInstantiationContext, RuntimeTypeHandle[] methodInstantiationContext) +525
System.ModuleHandle.ResolveTypeHandle(Int32 typeToken, RuntimeTypeHandle[] typeInstantiationContext, RuntimeTypeHandle[] methodInstantiationContext) +19
System.Reflection.RuntimeModule.ResolveType(Int32 metadataToken, Type[] genericTypeArguments, Type[] genericMethodArguments) +315
System.Reflection.CustomAttribute.FilterCustomAttributeRecord(CustomAttributeRecord caRecord, MetadataImport scope, Assembly& lastAptcaOkAssembly, RuntimeModule decoratedModule, MetadataToken decoratedToken, RuntimeType attributeFilterType, Boolean mustBeInheritable, Object[] attributes, IList derivedAttributes, RuntimeType& attributeType, IRuntimeMethodInfo& ctor, Boolean& ctorHasParameters, Boolean& isVarArg) +208
System.Reflection.CustomAttribute.GetCustomAttributes(RuntimeModule decoratedModule, Int32 decoratedMetadataToken, Int32 pcaCount, RuntimeType attributeFilterType, Boolean mustBeInheritable, IList derivedAttributes, Boolean isDecoratedTargetSecurityTransparent) +1171 System.Reflection.CustomAttribute.GetCustomAttributes(RuntimeAssembly assembly, RuntimeType caType) +146
WebActivator.AssemblyExtensions.GetActivationAttributes(Assembly assembly) +114
WebActivator.ActivationManager.RunActivationMethods() +286
WebActivator.ActivationManager.RunPreStartMethods() +38
WebActivator.ActivationManager.Run() +56[InvalidOperationException: The pre-application start initialization method Run on type WebActivator.ActivationManager threw an exception with the following error message: Could not load type 'System.Runtime.CompilerServices.ExtensionAttribute' from assembly 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'..]
System.Web.Compilation.BuildManager.InvokePreStartInitMethods(ICollection`1 methods) +11567502
System.Web.Compilation.BuildManager.CallPreStartInitMethods() +465
System.Web.Hosting.HostingEnvironment.Initialize(ApplicationManager appManager, IApplicationHost appHost, IConfigMapPathFactory configMapPathFactory, HostingEnvironmentParameters hostingParameters, PolicyLevel policyLevel, Exception appDomainCreationException) +1167[HttpException (0x80004005): The pre-application start initialization method Run on type WebActivator.ActivationManager threw an exception with the following error message: Could not load type 'System.Runtime.CompilerServices.ExtensionAttribute' from assembly 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'..]
System.Web.HttpRuntime.FirstRequestInit(HttpContext context) +11556592 System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context) +141 System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr, HttpContext context) +4813333
None of my searching has turned up anything terribly helpful, but the main avenue I've explored is a potential conflict between the version of .NET that Ninject was built against and the version that I'm actually running. I've never installed .NET 4.5 on my system, so I'm certain that I'm building against 4.0. I then built the most recent version of Ninject (3.0.2) and the extensions from source to be sure that it's using 4.0 as well. I also tried adding all the libraries that I'm using in the real app to my test app to see if there's a conflict but that didn't turn up anything. (However, I didn't go so far as to put in code that exercises them. I don't know if that would change anything.)
Has anyone seen this kind of problem before or have any ideas that I can try? Is there more info I can provide that would be helpful?
Alternatively, is there some other way I can use Ninject in an ASP.NET application instead of the ninject.web extension? Would wrapping the Ninject kernel up in a singleton work just as well?
asp.net-mvc - Ninject 3 多重绑定
我的问题实际上是在这里发布的一个老问题的重复: Ninject 2.2 multiple bindings
似乎有人要在 2011 年处理这个问题。有谁知道是否有办法在 Ninject 中关闭此类警告?还是其他一些解决方法?
编辑
作为对@BatteryBackupUnit 的回应,这是我的确切问题:
我有多个库......在我的核心库中,我做了这样的事情:
- 查找宿主应用程序(包括宿主)引用的所有程序集
IDependency
查找从所有这些程序集继承的所有类型。- 自动将所有这些注册为瞬态
然后从另一个库(主机应用程序可能会或可能不会引用),我有这个:
这里IDbContextFactory
也是一个IDependency
,所以它已经被核心库加载了,现在我在这里注册它,但范围不同(单例)。
根据经验(并且之前已经测试过),我知道这在 Autofac 中没有问题,但是 Ninject 给了我关于已经注册它的错误消息。
理想情况下,最好只覆盖任何以前的注册......“级联风格”(因为缺乏更好的短语)......
ninject - 在 Ninject 内核中处理超出范围的对象
我试图弄清楚内核如何处理其中的对象。例如,下面的代码创建了两个作用域,并断言当作用域相同时解析同一个对象,如果不同则解析不同的对象。
因为我有两个范围,所以RedBall
容器中有两个实例。
- 如何\何时删除这些?
- 如何扩展测试以证明容器中的球已被处理?