Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No way to unload an AssemblyLoadContext if JsonConvert.Serialize() has been called on an object from it #2253

Open
k-kosachev opened this issue Jan 9, 2020 · 2 comments

Comments

@k-kosachev
Copy link

If you execute Newtonsoft.Json.JsonConvert.Serialize() on a custom object defined in an assembly in a collectible AssemblyLoadContext, the AssemblyLoadContext can no longer be unloaded.
It obviously happens because of using a number of different caches like ConvertUtils.CastConverters with strong references and without any possibility to remove them (but reflection which seems like a bad workaround).

@ltrzesniewski
Copy link

It's not because of the Json.NET caches - they can be unloaded if Json.NET is itself loaded into the AssemblyLoadContext (their statics live in the load context).

The inability to unload is apparently due to the usage of the System.ComponentModel wrappers - that assembly is loaded into the default context and also caches quite a lot of things (and these caches living in the default context are causing the problem). You can apparently forcefully load it into your own AssemblyLoadContext though, but the AssemblyDependencyResolver won't help you with this.

See here: dotnet/runtime#13283

@rrs
Copy link

rrs commented Aug 8, 2020

Is a fix likely to happen in Newtonsoft?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants