简体   繁体   English

Ninject .ToFactory:异常:“抛出异常:Ninject.dll 中的‘Ninject.ActivationException’”

[英]Ninject .ToFactory: Exception: "Exception thrown: 'Ninject.ActivationException' in Ninject.dll"

I instruct Ninject to create a factory on an interface:我指示 Ninject 在一个接口上创建一个工厂:

this.Bind<IModelFactory>().ToFactory().InSingletonScope();

In this factory interface, I define the factory:在这个工厂接口中,我定义了工厂:

interface IModelFactory
{
    MyClass CreateMyClassFactory(string param1, DateTime param2);
}

I then attempt to use the factory to create a new class:然后我尝试使用工厂来创建一个新类:

Class myClass = modelFactory.CreateMyClassFactory(param1, new DateTime(2017,01,01);

... but it throws this exception: ...但它抛出这个异常:

Exception thrown: 'Ninject.ActivationException' in Ninject.dll抛出异常:Ninject.dll 中的“Ninject.ActivationException”

Additional information: Error activating string附加信息:激活字符串时出错

No matching bindings are available, and the type is not self-bindable.没有匹配的绑定可用,并且类型不可自绑定。

Activation path:激活路径:

2) Injection of dependency string into parameter title of constructor of type MyClass 2) 将依赖字符串注入 MyClass 类型的构造函数的参数标题中

1) Request for HedgeOrderMyClass 1) 请求 HedgeOrderMyClass

Suggestions:建议:

1) Ensure that you have defined a binding for string. 1) 确保您已为字符串定义了绑定。

2) If the binding was defined in a module, ensure that the module has been loaded into the kernel. 2) 如果绑定是在模块中定义的,请确保该模块已加载到内核中。

3) Ensure you have not accidentally created more than one kernel. 3) 确保您没有意外创建多个内核。

4) If you are using constructor arguments, ensure that the parameter name matches the constructors parameter name. 4) 如果您使用构造函数参数,请确保参数名称与构造函数参数名称匹配。

5) If you are using automatic module loading, ensure the search path and filters are correct. 5) 如果您使用自动模块加载,请确保搜索路径和过滤器正确。

When you instruct Ninject to create a factory on an interface, you are asking it to pull a new copy of the class out of the container, and fill in any missing parameters at runtime.当您指示 Ninject 在接口上创建工厂时,您是在要求它从容器中拉出该类的新副本,并在运行时填充任何缺失的参数。

This is the class we want:这是我们想要的类:

public class MyClass
{

}

As long as there is at least one constructor that accepts a parameter of type string and DateTime , then it will work:只要至少有一个构造函数接受stringDateTime类型的参数,它就会起作用:

public class MyClass
{
    // If this constructor is missing, then it throws an exception!
    MyClass(string param1, DateTime param2, ISomeOtherInterface someOtherInterface)
    {
    }
}

Ninject knows how to resolve ISomeOtherInterface , so that's no problem (assume we used Bind<>.To<> elsewhere). Ninject 知道如何解析ISomeOtherInterface ,所以这没问题(假设我们在别处使用了Bind<>.To<> )。 But how does it resolve string param1 and DateTime param2 ?但是它如何解析string param1DateTime param2呢?

The factory method comes to the rescue: it fills in those parameters from the arguments to the factory call, and everything works nicely.工厂方法来拯救:它填充从参数到工厂调用的那些参数,并且一切正常。

If you run with the debugger, you will see the constructor above getting called, with the same parameters as specified in the factory creation call.如果您使用调试器运行,您将看到上面的构造函数被调用,其参数与工厂创建调用中指定的参数相同。

This is a really powerful idea, and avoids having to manually write all of the glue code to create your own factories.这是一个非常强大的想法,并且避免了必须手动编写所有粘合代码来创建自己的工厂。

This is how I normally solve the problem with the Ninject.ActivationException:这就是我通常用 Ninject.ActivationException 解决问题的方法:

using Ninject.Extensions.Factory;

// ...

var kernel = new StandardKernel()
kernel.Bind<IMyClass>().ToFactory();

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

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