在Ninject中使用ToFactory时的ActivationException

时间:2012-05-07 09:05:53

标签: c# ninject factory ninject-extensions

当我尝试在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()

1 个答案:

答案 0 :(得分:5)