I am struggling with using the factory extensions for Ninject.
When using the extension in combination with InCallScope, I expected the same instance to be returned from the factory's create method, but instead I get two different instances.
Have I misunderstood the InCallScope concept or do I need to add something else?
using System;
using Ninject;
using Ninject.Extensions.Factory;
using Ninject.Extensions.NamedScope;
namespace MyTest
{
class Program
{
static void Main()
{
var kernel = new StandardKernel();
kernel.Bind<IMyFactory>().ToFactory();
// Does not give what I want, not a surprise...
// kernel.Bind<IMyStuff>().To<MyStuff1>().InTransientScope();
// Works - of course, but I don't want a singleton. It should only be used in call scope...
// kernel.Bind<IMyStuff>().To<MyStuff1>().InSingletonScope();
kernel.Bind<IMyStuff>().To<MyStuff>().InCallScope();
var myFactory = kernel.Get<IMyFactory>();
// Creating my first instance...
var myStuff1 = myFactory.Create();
// Creating my second instance...
var myStuff2 = myFactory.Create();
//
if (myStuff1.SeqNo != myStuff2.SeqNo)
throw new Exception("Except them to be equal...");
}
}
public interface IMyFactory
{
IMyStuff Create();
}
public interface IMyStuff
{
int SeqNo { get; }
}
public class MyStuff : IMyStuff
{
private static int _staticSeqNo;
public MyStuff()
{
SeqNo = _staticSeqNo;
_staticSeqNo++;
}
public int SeqNo { get; private set; }
}
}