I have a troublesome Windows service (see this question), and I am considering creating a custom scope factory to generate a new scope each time the service ticks.
I plan to inject the kernel into this factory, so that it can return a new scope for the service on every tick and destroy / release the scope after each tick.
To facilitate this, I plan to added this to my RegisterServices() method:
kernel.Bind<IKernel>().ToConstant(kernel);
And my custom scope factory would look like this:
public class CustomScopeFactory : ICustomScopeFactory
{
private readonly IKernel kernel;
public CustomScopeFactory(IKernel kernel)
{
this.kernel = kernel;
}
public ICustomScope CreateScope()
{
return kernel.Get<ICustomScope>();
}
public void DisposeScope(ICustomScope scope)
{
kernel.Release(scope);
}
}
I wouldn’t usually consider registering the kernel in the kernel or injecting it, but in this scenario I think it is a valid use case.
So, is it ok to register the Ninject kernel in the kernel itself? Any thoughts or alternatives would be appreciated.