I created a project with EF6 + Ninject + Owin. I realized that Ninject InRequestScope doesn't work, infact, in a single Web Api request the constructor of my DBContext derived class fires more than once.
The Startup file of my web api project is like:
public void Configuration(IAppBuilder app)
{
ConfigureOAuth(app);
HttpConfiguration config = new HttpConfiguration();
WebApiConfig.Register(config);
app.UseWebApi(config);
app.UseNinjectMiddleware(CreateKernel).UseNinjectWebApi(config);
}
private static IKernel CreateKernel()
{
// I have my Ninject module in a separate assembly
var kernel = new StandardKernel(new Core.Ioc.AutoBotMapper());
GlobalConfiguration.Configuration.DependencyResolver = new NinjectDependencyResolver(kernel);
return kernel;
}
My Module:
public class AutoBotMapper : NinjectModule
{
private readonly ILog _logger = LogManager.GetLogger("CORE");
public override void Load()
{
// Contesto is my DBContext
Bind<Contesto>().ToSelf().InRequestScope();
// Other Binds
Bind<ITournamentServiceFactory>().ToFactory();
Bind<ITournamentCloser>().To<TournamentCloser>();
...
}
}
I don't use the Bootstrapper "NinjectWebCommon" since i found this approach on the net.
Does exist a way to bypass this bug?