TinyIoC, the default IoC container for Nancy, does not autoregister multiple instances for the same interface.

So the following code will give you two 0’s.

using System;
using System.Collections.Generic;
using System.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            var container = new TinyIoC.TinyIoCContainer();
            container.AutoRegister();
            Console.WriteLine(container.Resolve<C3>().NumberOfElements);
            Console.WriteLine(container.Resolve<IEnumerable<I1>>().Count());
            Console.ReadLine();
        }
    }

    public interface I1
    {
        
    }
    public class C1:I1
    {
        
    }
    public class C2:I1
    {
        
    }

    public class C3
    {
        private int _count;

        public C3(IEnumerable<I1> list)
        {
            _count = list.Count();
        }

        public int NumberOfElements { get { return _count; }
        }
    }

}

There is one way of registering these instances and that is by using container.RegisterMultiple

csharp container.RegisterMultiple<I1>(new[] {typeof (C1), typeof (C2)}); But when I add implementations I have to update that and I could forget to do that. So I need a way to automate that.

With a little help of StackOverflow, because it is easier to copy/paste then to think myself, I came to this.

csharp var container = new TinyIoC.TinyIoCContainer(); var type = typeof(I1); var types = AppDomain.CurrentDomain.GetAssemblies().ToList() .SelectMany(s => s.GetTypes()) .Where(x => type.IsAssignableFrom(x) && x.IsClass); container.RegisterMultiple<I1>(types.ToArray()); And now I can add implementations and the container will pick it up.

GrumpyDev azures me this will be included in the next version of TinyIoC and Nancy.

So the above is for TinyIoC 1.2 and Nancy 0.17.1