Description
Description
When an assembly loaded into a custom AssemblyLoadContext creates a proxy using DispatchProxy for an interface from an assembly loaded into that AssemblyLoadContext, the first custom ALC works fine. If you create a second custom AssemblyLoadContext and load the same assembly and try to create a proxy for the same interface, but for the interface type loaded into the second ALC, the returned proxy incorrectly implements the interface from the first ALC.
This is an issue when the DispatchProxy proxyType is from the Default ALC (or at least not the same ALC as the interface being proxied).
Reproduction Steps
I created a solution here which demonstrates the problem:
https://github.com/mconnew/IssueRepros/tree/main/DispatchProxyWithAlc
Expected behavior
The second custom ALC instance should create the proxy implementing the interface loaded into the second ALC instance.
Actual behavior
The second custom ALC instance creates the proxy implementing the interface that's loaded into the first ALC instance.
Regression?
No response
Known Workarounds
Explicitly load the assembly which contains the proxyType into the ALC that contains the interface being proxied. The repro app purposefully puts the proxyType implementation into a third assembly to mimic the typical WCF scenario where the WCF client libraries are usually used from the default ALC and not explicitly loaded.
Configuration
.NET 8
Windows 11, x64
Not specific to configuration, this had been reported by multiple users.
Other information
This is affecting WCF scenarios. This issue has been reported occurring with Azure Functions (via Microsoft support) and PowerPlatform Dataverse. Here's the Dataverse issue with it reported. Someone mentions about seeing the issue in Azure Functions at the end of this issue.
Here's an issue where it was previously reported, and I don't believe the problem was fully understood and incorrectly closed as by design. Returning a proxy for an interface type different than the one you asked for (as the same interface in different ALC's at runtime are treated as different types) isn't the design of this feature.