Convention based binding in Ninject 3.0

2.1k views Asked by At

I'm trying to setup Ninject on my new project and I want to scan assembilies using convetions so that

IFoo

will automatically resolve to

IBar

All the samples and documentation describes using this approach

kernel.Scan(...)

while the Ninject 3 Kernel seems to use .Load instead. If I'm using Load for scanning then how do I configure scanning with conventions?

Edit

I was able to get it working using: https://github.com/ninject/ninject.extensions.conventions/wiki/What-is-configuration-by-convention

However, now I can't get my modules to load. I'm using the code below and getting the error below.

    IKernel kernel = new StandardKernel();
    kernel.Bind(x => x
        .FromAssembliesMatching("Crt.*.dll")
        .SelectAllTypes()
        .BindAllInterfaces()
        );
    kernel.Load("*.dll");
    return kernel;

Error:

Ninject.ActivationException was unhandled
  Message=Error activating ITrainingEngine
More than one matching bindings are available.
Activation path:
  1) Request for ITrainingEngine

Suggestions:
  1) Ensure that you have defined a binding for ITrainingEngine only once.

  Source=Ninject
  StackTrace:
       at Ninject.KernelBase.Resolve(IRequest request) in c:\Projects\Ninject\ninject\src\Ninject\KernelBase.cs:line 380
       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[T](IResolutionRoot root, IParameter[] parameters) in c:\Projects\Ninject\ninject\src\Ninject\Syntax\ResolutionExtensions.cs:line 37
       at Crt.BlackBox.Train.Program.Run() in C:\code\Crimson\CRT\readmission\src\Crt.BlackBox\Crt.BlackBox.Train\Program.cs:line 54
       at Crt.BlackBox.Train.Program.Main(String[] args) in C:\code\Crimson\CRT\readmission\src\Crt.BlackBox\Crt.BlackBox.Train\Program.cs:line 24
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: 
1

There are 1 answers

0
Remo Gloor On BEST ANSWER

Use SelectAllClasses instead of SelectAllTypes