Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ActivationException when using ToFactory in Ninject

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()
like image 685
magol Avatar asked May 07 '12 09:05

magol


1 Answers

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.

like image 155
Remo Gloor Avatar answered Oct 02 '22 00:10

Remo Gloor