Closed
Description
When using a Ninject Provider<T>
(factory) to create a dependency, the dependencies resolved from the IContext
passed to the Provider will not honor any 'Per Request' scoping.
When in Owin/WebApi world, ninject creates a named scope per request which is automatically bound to a context but it seems that the context passed to the CreateInstance
method is somehow not aware of any global named scope.
To reproduce, create a provider which relies on some per request object:
public class FooProvider : Provider<IFoo>
{
protected override IFoo CreateInstance(IContext context)
{
var bar = context.Kernel.Get<IBar>(); // This should call the Bar constructor
var bar2 = context.Kernel.Get<IBar>(); // This should return the same instance but is calling the constructor again
return new Foo(bar, bar2);
}
}
Registration:
Bind<IBar>().To<Bar>().InRequestScope();
Bind<IFoo>().ToProvider<FooProvider>();
Now you would need a WebApi controller hosted in Owin to rely on an IFoo.
Metadata
Metadata
Assignees
Labels
No labels