6

当我尝试在 Ninject 中使用 ToFactory 时,我得到 Ninject.ActivationException

var test = new StandardKernel();
test.Bind<IFoo>().To<Foo>();
test.Bind<IFooFactory>().ToFactory();
var factory = test.Get<IFooFactory>();
var foo = factory.GetFoo();           //<--Ninject.ActivationException

工厂:

public interface IFooFactory
{
    IFoo GetFoo();
}

例外:

Ninject.ActivationException was unhandled
  Message=Error activating IFoo
No matching bindings are available, and the type is not self-bindable.
Activation path:
  1) Request for IFoo

Suggestions:
  1) Ensure that you have defined a binding for IFoo.
  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.

  Source=Ninject
  StackTrace:
       at Ninject.KernelBase.Resolve(IRequest request) in c:\Projects\Ninject\ninject\src\Ninject\KernelBase.cs:line 359
       at Ninject.ResolutionExtensions.GetResolutionIterator(IResolutionRoot root, Type service, Func`2 constraint, IEnumerable`1 parameters, Boolean isOptional, Boolean isUnique) in c:\Projects\Ninject\ninject\src\Ninject\Syntax\ResolutionExtensions.cs:line 263
       at Ninject.ResolutionExtensions.Get(IResolutionRoot root, Type service, String name, IParameter[] parameters) in c:\Projects\Ninject\ninject\src\Ninject\Syntax\ResolutionExtensions.cs:line 164
       at Ninject.Extensions.Factory.Factory.InstanceResolver.Get(Type type, String name, Func`2 constraint, ConstructorArgument[] constructorArguments, Boolean fallback) in c:\Projects\Ninject\ninject.extensions.factory\src\Ninject.Extensions.Factory\Factory\InstanceResolver.cs:line 75
       at Ninject.Extensions.Factory.StandardInstanceProvider.GetInstance(IInstanceResolver instanceResolver, MethodInfo methodInfo, Object[] arguments) in c:\Projects\Ninject\ninject.extensions.factory\src\Ninject.Extensions.Factory\Factory\StandardInstanceProvider.cs:line 78
       at Ninject.Extensions.Factory.FactoryInterceptor.Intercept(IInvocation invocation) in c:\Projects\Ninject\ninject.extensions.factory\src\Ninject.Extensions.Factory\Factory\FactoryInterceptor.cs:line 57
       at Castle.DynamicProxy.AbstractInvocation.Proceed()
       at Castle.Proxies.IFooFactoryProxy.GetFoo()
4

1 回答 1

6

您的方法的命名会触发约定,因此生成的工厂正在请求IFoo命名的“Foo”,但配置尚未使用该名称注册该类型的服务。

于 2012-05-07T12:35:08.157 回答