ActivationException when using ToFactory in Ninject
Asked Answered
C

1

6

When I try to use the ToFactory in Ninject, I get 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

the factory:

public interface IFooFactory
{
    IFoo GetFoo();
}

The exception:

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()
Catalase answered 7/5, 2012 at 9:5 Comment(0)
H
6

The naming of your method is triggering a convention such that the resulting generated factory is requesting an IFoo named "Foo" but the configuration has not registered a service of that type by that name.

Heid answered 7/5, 2012 at 12:35 Comment(4)
I do not understand. I don't use any names?Catalase
If you read the linked docu you will see that you are requesting a named instance.Heid
The problem was that I named it GetFoo(). To get it to work I had to cal it CreateFoo()Catalase
The link included in this answer includes the following text: "The default instance provider of the extension uses a convention of attempting to resolve an instance based on a named binding whenever the name of the method declaration on the abstract factory interface method starts with “Get”…" - this is why you had to change from GetFoo to CreateFooKatabasis

© 2022 - 2024 — McMap. All rights reserved.