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

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

Merged

Conversation

boylec
Copy link

@boylec boylec commented Jun 15, 2024

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
Copy link
Author

boylec commented Jun 16, 2024

@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!

@boylec boylec force-pushed the fix/isolated-assemblies branch from 9f42a48 to 86fd5c4 Compare June 17, 2024 20:46
src/nuget.config Outdated Show resolved Hide resolved
@boylec
Copy link
Author

boylec commented Jan 2, 2025

@andrerav done and done thanks!

@andrerav andrerav merged commit 1457dce into MapsterMapper:development Jan 2, 2025
1 check passed
@andrerav
Copy link
Contributor

andrerav commented Jan 2, 2025

Thank you for this awesome contribution @boylec. If you have some extra time to spare, please update the Wiki to reflect the new API changes.

@boylec
Copy link
Author

boylec commented Jan 2, 2025

@andrerav as far as I can tell there are no changes to consumer-facing APIs of the library.

It has been a long time since I made this change though, are you seeing something specific?

Also: you're very welcome!

@andrerav
Copy link
Contributor

andrerav commented Jan 2, 2025

Ah, disregard that. I had a brief lapse of reason :)

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

Successfully merging this pull request may close these issues.

2 participants