container icon indicating copy to clipboard operation
container copied to clipboard

Make UnityContainers fit for usage with .NET Core AssemblyLoadContexts

Open sxleixer opened this issue 6 years ago • 1 comments

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 Dispose method of the IUnityContainer destroying 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 InternalRegistration object. 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

sxleixer avatar Jun 11 '20 09:06 sxleixer

Interesting idea, let me think about it

ENikS avatar Jun 11 '20 16:06 ENikS