I'm trying to use Ninject to manage lifetime on objects. For my IRepository object, I am requiring IDisposable to be implemented, and within the ConcreteRepository, I have implemented IDisposable to kill my NHibernateSession.
My issue is that I also put a static variable within the ConcreteRepository to count the number of instantiations and destructions/disposals of the ConcreteRepository... when I run the application, I'm running out of connections to the database, and my log is showing that my application is never releasing my connections.
My Global.asax:
public class Global : NinjectHttpApplication
{
protected override void OnApplicationStarted()
{
base.OnApplicationStarted();
AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
ControllerBuilder.Current.DefaultNamespaces.Add("WebPortal.Controllers");
var log4netConfigFileInfo = new System.IO.FileInfo(Server.MapPath("~/log4net.xml"));
log4net.Config.XmlConfigurator.ConfigureAndWatch(log4netConfigFileInfo);
log4net.ILog log = log4net.LogManager.GetLogger(typeof(Global));
log.Info("Started...");
}
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new HandleErrorAttribute());
}
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
}
protected override Ninject.IKernel CreateKernel()
{
var kernel = new Ninject.StandardKernel(
new Utils.UtilsModule(),
new Web.DataObjects.NHibernate.DataObjectsNHibernateModule(),
new Payroll.PayrollModule(),
new Web.DataObjects.DbModule()
);
kernel.Load(Assembly.GetExecutingAssembly());
return kernel;
}
}
My Controller module that I'm using to test with:
public class DatabaseAreaModelModule
: NinjectModule
{
public override void Load()
{
Bind<DiscountEdit>().ToSelf().InRequestScope();
Bind<ItemCategoryEdit>().ToSelf().InRequestScope();
Bind<ItemEdit>().ToSelf().InRequestScope();
Bind<ModifierEdit>().ToSelf().InRequestScope();
Bind<ModifierSetEdit>().ToSelf().InRequestScope();
Bind<RevenueCenterEdit>().ToSelf().InRequestScope();
Bind<RevenueClassEdit>().ToSelf().InRequestScope();
Bind<TaxEdit>().ToSelf().InRequestScope();
}
}
My "NHibernate" Ninject module:
public class DataObjectsNHibernateModule
: NinjectModule
{
public override void Load()
{
Bind<ISessionFactory>().ToProvider<NHibernateSessionProvider>().InSingletonScope();
Bind<IRepository>().To<NHibernateRepository>().InRequestScope();
}
}
What I'm trying to figure out is why when I ask for something to be InRequestScope(), it isn't being disposed... any ideas?