MapsterMapper / Mapster

A fast, fun and stimulating object to object Mapper
MIT License
4.23k stars 325 forks source link

Use assembly loading context to isolate loaded assemblies from AppDomain #714 #716

Open boylec opened 3 weeks ago

boylec commented 3 weeks ago

Mapster.Tool update to solve issue #714

TL; DR

Replace Assembly.LoadFrom calls with something more robust that prevents assembly/type collisions between the assemblies in the running Mapster.Tool process (such as .NET framework assemblies) and the scanned assemblies - while also preserving the ability to scan for attributes that are in the Mapster assembly (using share Assembly resolution).

See Pertinent MSFT Docs

More detail

Added an AssemblyLoadContext implementation for dynamically loading assemblies when scanning in an isolated way so that they don't have collisions with all of the assemblies loaded by Mapster by default (for example framework assemblies such as System.Text.Json).

The implementation also allows the setting of a subset of assembly names that will be shared with AssemblyLoadContext.Default (the default context that all assemblies are resolved from if no custom ALC were created). Some scanned types need to be owned by the default assembly context such as those from the "Mapster" assembly.

The end result is that we can resolve types from the target assembly being scanned, and we can do things like .GetCustomAttribute() and the attribute will be found because it comes from a shared assembly across the default (runtime) AssemblyLoadContext and the custom one used to avoid collisions.

boylec commented 3 weeks ago

@andrerav what are the chances this can get merged any time soon and released as pre release?

Our org relies on this package and this is a blocker for us.

We can run off of our fork if it's going to be awhile.

Thanks!