c# - Why Ninject does not release disposed objects with InCallScope? -


i'm trying use ninject (version 3.0.1) in winforms application, have several (currently) self-binded service class, construct using ninject. service class needs other service classes (sub-services). of these service classes need repository interact database, have abstract irepository interface. need have same repository whole service-hierarchy in service class, i'm using incallscope() scope when binding irepository. i'm using xpo orm tool, have xporepository implementation, i'm binding to. see other question scenario.

my binding looks this:

bind<irepository>().to<xporepository>().incallscope(); 

i don't have explicit toself() bindings each service class, assume when them ninject, should have transient scope, interpret have manually dispose them.

assume have services1 , services2 service class, both having constructor parameter of type irepository. assume services1 use methods of services2, add constructor parameter services1 type services2. without ninject, do:

var repo = new myrepository(); // implementing irepository var service1 = new services1(repo, new services2(repo)); 

i'm using 1 of services in background thread (using tpl), in loop this:

while (true) {     if (cancellatiopending())         break;     using (var service = _kernel.get<service1>())     {         // stuff using service class     }     thread.sleep(20*1000); } 

i had same structure before using ninject, have (i think) implemented disposal of every objects, including repositories in correct places. however, i've noticed since i'm utilizing ninject this, have big memory leak in application, , crashes in every 2-3 hours outofmemoryexception. put breakpoint inside loop, , noticed ninject cache has thousands of entries full of disposed xporepository objects. disposed me guess, i'm not sure called dispose method.

why ninject holding these disposed objects? expect when dispose main service in end of using block (which scope of irepository objects due incallscope()) every object in scope should disposed , released ninject.

edit: before comment or answer why pattern not good, know better. know extract service interfaces make use of di , improve testability, , know should use func<irepository> constructor parameter, , inject it, , every service have own reponsibility dispose repository. have no time such refactorings currently.

ninject release repository if following things true:

  • no 1 holding reference service1
  • service1 gc'd (since have thread sleep of 20 sec there high chance has been promoted gen 2 , released rarely)
  • cache pruning executed after service1 gc'd, cache pruning interval defaults 30 sec. may want try shorter interval.
  • alternatively previous point can try force immediate releasing implementing ninject.infrastructure.disposal.inotifywhendisposed in service1

Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -