title: Ninject binds to the wrong anonymous method when there is more than one method bound type
body: <p>I am building a framework that I don't want to couple to a particular IOC container so have created a layer on top of Ninject / structuremap etc.</p>
  
  <p>I have a binding class that accepts a Func to allow binding to a method.</p>
  
  <p>For example</p>
  
  <pre><code>public class Binding
  {
   public Type Source { get; set; }
   public Func&lt;object&gt; Method {get; set; }
   public Scope { get; set; }
  }
  </code></pre>
  
  <p>If I have a binding like...</p>
  
  <pre><code>var binding = new Binding() { 
  Source = typeof(IRepository),
  Method = () =&gt; new Repository(new LinqToSqlRepository(connectionString)),
  Scope = Scope.HttpRequest
  };
  </code></pre>
  
  <p>The framework wrapping Ninject creates Ninject bindings for my generic binding like this</p>
  
  <pre><code>Module :NinjectModule
  {
   IList&lt;Binding&gt; _Bindings;
  
   public Module(IList&lt;Binding&gt; bindings)
   {
      _Bindings = bindings;
   }
  
   public override void Load() { 
      foreach (var binding in _Bindings) {
        switch(binding.Scope) {
          case IocScope.HttpRequest:
            Bind(binding.Source).ToMethod(c =&gt; binding.Method()).InRequestScope();
            break;
          // ... omitted for brevity
        }
      }
    }
  }
  </code></pre>
  
  <p>This works fine when there is only one binding being bound to a method. When there are multiple bindings being bound within the same module to methods however the incorrect type is returned. From debugging, it looks as if the last binding is always used.</p>
  
  <p>Thus the problem with an example;</p>
  
  <pre><code>var binding1 = new Binding() { 
   Source = typeof(IRepository),
   Method = () =&gt; new Repository(new LinqToSqlRepository(connectionString)),
   Scope = Scope.HttpRequest
  };
  
  var binding2 = new Binding() { 
   Source = typeof(ICalendar),
   Method = () =&gt; new MvcCalendar( ..... )
   Scope = Scope.HttpRequest
  };
  </code></pre>
  
  <p>At runtime when Ninject is requested to new up an MVC Controller which takes in an IRepository and an ICalendar, I receive a type conversion error saying that a MvcCalendar cannot be converted to an IRepository. I have discovered that for some reason the last binding is always being returned for the first requested type.</p>
  
  <p>This is a highly simplified version of what is really going on to try and highlight the actual issue, the wrong method being bound to a requested type when there are multiple method bindings. I hope this still explains the issue though.</p>
  
  <p>This appears to be related to some sort of closure scoping issue. I also wonder whether Ninject is getting is getting confused by the Func instead of Func usage.</p>
  
  <p><strong>Unit Test Example</strong></p>
  
  <p>Here is a test module I load into my custom IOC container. This does not depend on any particular IOC framework. When I instantiate a NinjectIocContainer to handle the DI, the internal binding of this in Ninject occurs as example further up (see NinjectModule)</p>
  
  <pre><code>public class MultipleMethodBoundTypesModule : IocModule
  {
      public override void Load()
      {
          Bind&lt;IPerson&gt;().To(() =&gt; new Person()).In(IocScope.Transient);
          Bind&lt;IRobot&gt;().To(() =&gt; new Robot(new Person())).In(IocScope.Transient);
      }
  }
  </code></pre>
  
  <p>Here is a simple test that tries to retrieve each of the types.</p>
  
  <pre><code>    [Test]
      public void   Expect_That_Multiple_Method_Bound_Types_Can_Exist_Within_The_Same_Module()
      {
          // arrange
          var container = Get_Container_With_Module(new MultipleMethodBoundTypesModule());
  
          // act
          var person = container.Get&lt;IPerson&gt;();
          var robot = container.Get&lt;IRobot&gt;();
  
          // assert
          Assert.IsNotNull(person);
          Assert.IsNotNull(robot);
      }
  </code></pre>
  
  <p>As explained eariler, this throws a type conversion where the last closure (for the robot) is being bound to a person.</p>
  
  <blockquote>
    <p>TestCase 'Ioc.Test.NinjectContainerTest.Expect_That_Multiple_Method_Bound_Types_Can_Exist_Within_The_Same_Module'
    failed: System.InvalidCastException : Unable to cast object of type 'Ioc.Test.Robot' to type 'Ioc.Test.IPerson'.
        at System.Linq.Enumerable.d__b1<code>1.MoveNext()
        at System.Linq.Enumerable.Single[TSource](IEnumerable</code>1 source)
        at Ninject.ResolutionExtensions.Get[T](IResolutionRoot root, IParameter[] parameters)
        NinjectIocContainer.cs(40,0): at Ioc.Ninject.NinjectIocContainer.GetTInstance
        IocTestBase.cs(149,0): at Ioc.Test.IocTestBase.Expect_That_Multiple_Method_Bound_Types_Can_Exist_Within_The_Same_Module()</p>
  </blockquote>