container
container copied to clipboard
Make UnityContainers fit for usage with .NET Core AssemblyLoadContexts
What's the problem?
When using Unity 5.11.7 for resolving types inside an AssemblyLoadContext, these type instance are getting cached by the IUnityContainer object.
Thus it is not possible to unload the AssemblyLoadContext since there is a dangling reference around.
Why do I do this?
Because I want to implement a Plug-In-System using Unity which resolves arbitrary Types in .NET Core.
How to solve this issue?
- One may call the
Disposemethod of theIUnityContainerdestroying all information on registered types. [This option is terrible for me.] - Add a flag to the resolve method to NOT cache the type passed to the method.
- Abother way may be to use a child container, but I found out, this also caches the Type inside the parent container using a
InternalRegistrationobject. Moving the registration of the type up to the child container will solve the issue. [This option is way better and very acceptable, since then I will have to hold only the child container and the context. First disposing the container, then the context will solve my issue.]
Problems
- How are second-, third-, ... nth-order resolves handled and cached?
Extras
I created a minimal example to show the issue. https://github.com/sxleixer/UnityCachingMinimalExample
Interesting idea, let me think about it