c# - Ninject.Web.MVC + MVC3 throws StackOverflowException -
i've got simple web application using asp.net mvc3 , ninject.web.mvc (the mvc3 version).
the whole thing working fine, except when application ends. whenever ends, kernel disposed, seen in application_end() in ninjecthttpapplication:
reflector tells me this:
public void application_end() { lock (this) { if (kernel != null) { kernel.dispose(); kernel = null; } this.onapplicationstopped(); } }
what happens webserver goes down stackoverflowexception (i tried both iis7 , built-in webserver in vs2010). can assume it's going wrong, haven't written code myself on application end.
i figured out kernel knows how resolve ikernel (which returns kernel itself), might cause stack overflow? imagine happens:
- kernel.dispose()
- dispose instances in kernel
- hey! @ this, kernel in kernel. return step 1.
in other words, kernel gets disposed, disposes references holds (which includes self-reference), causes dispose itself.
does make sense?
edit:
it seems problem in ninjecthttpapplication. take @ activation code:
public void application_start() { lock (this) { kernel = this.createkernel(); ... kernel.bind<iresolutionroot>().toconstant(kernel).insingletonscope(); ... } }
it seems ok, what's happening whenever iresolutionroot called, kernel cached within itself. when disposing kernel, cache emptied disposes cached objects, causes circular reference.
a simple solution ninjecthttpapplication change binding. change constant binding method one:
kernel.bind<iresolutionroot>().toconstant(kernel).insingletonscope();
becomes
kernel.bind<iresolutionroot>().tomethod(x => this.kernel);
this solves problem, not sure if whole circular dispose caching issue bug in ninject.
i encountered same issue.
i ended copying code ninjecthttpapplication
, removing kernel.dispose()
in application_end
function.
public void application_end() { lock (this) { if (kernel != null) { //kernel.dispose(); kernel = null; } this.onapplicationstopped(); } }
that should fix error. not sure if there planned fix though.
Comments
Post a Comment