fix/11848: BAML assembly resolution to respect AssemblyLoadContext - #11849
fix/11848: BAML assembly resolution to respect AssemblyLoadContext#11849cunhamauro wants to merge 2 commits into
Conversation
Update Baml2006SchemaContext to resolve BAML-referenced assemblies in the AssemblyLoadContext of the assembly that initiated the resolution. Previously, BAML assembly resolution was effectively AssemblyLoadContext- unaware: it relied on global assembly resolution through the application domain, which could return an assembly loaded into a different context or fail to resolve assemblies that only existed in a custom AssemblyLoadContext. Resolve assemblies through the requesting assembly's AssemblyLoadContext first, preserving the load-context identity of the requesting assembly and ensuring BAML dependencies are resolved from the same isolated context. Fall back to the existing global resolution logic when the requesting assembly has no associated AssemblyLoadContext or the context cannot resolve the requested assembly. This enables proper BAML support for applications and plugins using custom AssemblyLoadContexts and prevents cross-context assembly resolution.
|
@dotnet-policy-service agree |
There was a problem hiding this comment.
Pull request overview
Updates WPF’s BAML assembly resolution to prefer resolving referenced assemblies within the AssemblyLoadContext associated with the BAML-owning (“local”) assembly, addressing type identity mismatches when multiple ALCs load assemblies with the same identity.
Changes:
- Add a
NETCOREAPP-guarded resolution path that callsAssemblyLoadContext.GetLoadContext(_localAssembly)and attemptsalc.LoadFromAssemblyName(...)before legacy lookup. - Preserve existing legacy resolution behavior as a fallback when ALC-based resolution is unavailable or fails.
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Swallow only specific assembly-load failures and not non-load-related exceptions. Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
|
This unfortunately requires fixes in numerous other places, it is not as simple. Beyond fixes there are also considerations since fixing this won't come without trade-offs and explaining silent failures. |
I cannot dismiss that concern with certainty. All existing tests and checks pass, and I have also validated the change successfully against a focused reproduction using multiple isolated AssemblyLoadContext instances. However, I agree that resolving this specific issue does not by itself prove that every existing WPF loading scenario remains unaffected.
Known WPF assemblies and previously resolved entries continue through the existing cache fast path. Readers created without a local assembly also retain the previous behavior entirely. If context-based resolution fails with one of the expected assembly-loading exceptions, FileNotFoundException, FileLoadException, or BadImageFormatException, the existing legacy resolution logic is preserved as a fallback.
Deferred resources and templates reuse the same Baml2006SchemaContext, whose local assembly remains fixed, so their subsequent cached resolutions remain associated with the same requesting context. Concurrent resolution should also converge on the same assembly because AssemblyLoadContext controls its own assembly cache. If you are aware of specific flows or other areas that may be affected by this change, I would be very interested in investigating them and adding targeted tests. I am fully open to revising the approach if those tests reveal regressions or additional trade-offs that should be addressed. |
Fixes #11848
Description
A BAML reference normally identifies an assembly by its name and version. If more than one loaded assembly in the global appdomain satisfies that criteria, it becomes ambiguous. Selecting the last loaded matching assembly from a process-wide set of loaded assemblies can bind BAML to a type from an unrelated ALC.
The parser must instead resolve the reference in the loading context appropriate to the BAML-owning assembly. This preserves the normal rule that code and the BAML it owns refer to the same component-local types.
Customer Impact
This issue has impact on both companies that develop and ship products and on their clientes that load different products simultaneously into a single host application.
Regression
This issue was not present in .NET Framework because it did not allow assemblies with the same combination of name and version to be loaded more than once.
Testing
The fix has been validated locally using this project: https://github.com/cunhamauro/XamlParserTypeIdentityMismatch
Risk
The implementation risk is minimal because the fix follows the CLR’s existing assembly-loading semantics: a dependency is resolved in the AssemblyLoadContext of the assembly that requests it.
In most applications, this change will not produce any observable difference because assemblies are loaded into the default AssemblyLoadContext. The change only affects scenarios where the same assembly identity is loaded in multiple ALCs.
Microsoft Reviewers: Open in CodeFlow
Read more about this @: