When I use Ninject with any extension in XBAP (WPF Browser) application, I get Stack Overflow exception (small pun intended).
Here's stack trace. It's rather large, but complete (that's why I've posted it on pastebin):
I'm trying to use Conventions and Factory extensions for Ninject. If I reference assembly of either one, I get Stack overflow exception. So to be clear, when I remove references to both libraries, application runs correctly.
It's really weird, because Ninject seems to be creating new AppDomain, but CLR apparently tries to load entry/executing assembly (the only one in my solution) and execute it. That obviously leads to Stack overflow exception.
I'm also using Caliburn.Micro, plus additional libraries: Caliburn.Micro.Contrib, Caliburn.Micro.Extras (along with their dependencies) - all latest versions.
Does anyone have an idea, what's causing this and how to fix it?
After fiddling around with AppDomains, I've found out that Ninject has nothing to do with this.
(an extensive explanation follows, solution/workaround is at the end of the answer)
Ninject creates seperate
AppDomain
inAssemblyNameRetriever
to retrieve assembly names. In my case, those assemblies are Ninject extensions:The problem lies with the object
AppDomainSetup
stored inAppDomain.CurrentDomain.SetupInformation
property. It hasActivationArguments
property set to instance ofActivationArguments
class, which contains information that causes entry assembly to execute (which is fine, since XBAP application has to start somehow).However, since Ninject uses same setup information, this causes infinite recursion and thus our favorite StackOverflow exception.
The workaround is to set property
ActivationArguments
tonull
. However,AppDomain
class creates a copy ofAppDomainSetup
object inSetupInformation
property getter. Thus it is necessary to get a copy ofAppDomainSetup
object, setActivationArguments
tonull
and via reflection set value_FusionStore
field ofAppDomain
class to this object.TL;DR: Before creating Ninject kernel, we have to invoke this code:
No idea if this will cause any issues later. Thus far, I believe this code requires full trust, because it accesses private field (and sets its value).