I\'m having some difficulty using Ninject\'s InSingletonScope binding with Web Api RC. No matter how I create my binding, it looks like perhaps Web Api is handling scope/li
Instead of not disposing the kernel (which will not call the internal dispose) you can simply implement your own singleton:
public static class NinjectSingletonExtension
{
public static CustomSingletonKernelModel SingletonBind(this IKernel i_KernelInstance)
{
return new CustomSingletonKernelModel(i_KernelInstance);
}
}
public class CustomSingletonKernelModel
{
private const string k_ConstantInjectionName = "Implementation";
private readonly IKernel _kernel;
private T _concreteInstance;
public CustomSingletonKernelModel(IKernel i_KernelInstance)
{
this._kernel = i_KernelInstance;
}
public IBindingInNamedWithOrOnSyntax To(TImplement i_Constant = null) where TImplement : class, T
{
_kernel.Bind().To().Named(k_ConstantInjectionName);
var toReturn =
_kernel.Bind().ToMethod(x =>
{
if (i_Constant != null)
{
return i_Constant;
}
if (_concreteInstance == null)
{
_concreteInstance = _kernel.Get(k_ConstantInjectionName);
}
return _concreteInstance;
}).When(x => true);
return toReturn;
}
}
And then simply use:
i_Kernel.SingletonBind().To();
Rather then
i_Kernel.Bind().To().InSingletonScope();
note: although it is only matters for the first request, this implementation is not thread safe.