1

I am using Ninject.Extensions.Conventions to add bindings dynamically. The .dll names to load are stored in configuration. If the configuration is incorrect and the .dll can not be loaded it would be good to know about that. Currently any failure to load a .dll is not bubbled up. For example, if I try to load a potato there is no error I can catch:

foreach (var customModule in customModuleConfigs)
{
    KeyValuePair<string, KVP> module = customModule;

    _kernel.Bind(scanner => scanner
        .From(module.Value.Value)
        .SelectAllClasses().InheritedFrom<ICamModule>()
        .BindAllInterfaces());

    // I need to know this failed
    _kernel.Bind(scanner => scanner
        .From("potato")
        .SelectAllClasses().InheritedFrom<ICamModule>()
        .BindAllInterfaces());
}

Is there a way to know I have a bad configuration? In the IntelliTrace window I see an exception thrown but caught before it bubbles up.

Eric Scherrer
  • 3,328
  • 1
  • 19
  • 34
  • 1
    I have worked around this for now by getting the count of bindings before and after then comparing, but still looking for a better way. – Eric Scherrer Jan 12 '15 at 21:20

2 Answers2

1

You could create a wrapper around the AllInterfacesBindingGenerator class and use this to count the generated bindings:

public class CountingInterfaceBindingGenerator : IBindingGenerator
{
    private readonly IBindingGenerator innerBindingGenerator;

    public CountingInterfaceBindingGenerator()
    {
        this.innerBindingGenerator =
            new AllInterfacesBindingGenerator(new BindableTypeSelector(), new SingleConfigurationBindingCreator());
    }

    public int Count { get; private set; }

    public IEnumerable<IBindingWhenInNamedWithOrOnSyntax<object>> CreateBindings(Type type, IBindingRoot bindingRoot)
    {
        this.Count++;

        return this.innerBindingGenerator.CreateBindings(type, bindingRoot);
    }
}

Usage:

var kernel = new StandardKernel();
var bindingGenerator = new CountingInterfaceBindingGenerator();

kernel.Bind(b =>
{
    b.From("potato")
        .SelectAllClasses()
        .InheritedFrom<ICamModule>()
        .BindWith(bindingGenerator);
});

if (bindingGenerator.Count == 0)
    // whatever

This is probably longer than your current code, but it would allow further customization of the bindings that have been created.

Frank
  • 4,461
  • 13
  • 28
  • Thanks Frank, this is a much more graceful approach than what I have and I will use it. I gave the answer to #BatteryBackupUnit thought because it allows me to catch and log the assembly load failure - which is what I was looking for (in addition to logging if no implementations found in the assembly). – Eric Scherrer Jan 13 '15 at 15:03
1

You'll need to do the loading of the Assembly yourself, then you can control whether there is an exception thrown. Use the

From(params Assembly[] assemblies) overload.

Load the assembly by either using Assembly.LoadFrom() or Assembly.Load.

BatteryBackupUnit
  • 12,934
  • 1
  • 42
  • 68
  • Thank you, this was exactly what I needed. For some reason I thought there would be more fuss about loading an assembly external to the Ninject framework but this is pretty straightforward and allows me to catch and log the exception for easier maintenance. – Eric Scherrer Jan 13 '15 at 15:01