You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docfx/docs/dynamicproxy.md
+12-9Lines changed: 12 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -53,21 +53,24 @@ The dynamic proxy maintains the same async-only contract that is exposed by the
53
53
54
54
## AssemblyLoadContext considerations
55
55
56
-
When in a .NET process with multiple <xref:System.Runtime.Loader.AssemblyLoadContext> instances, you should consider whether StreamJsonRpc is loaded in an <xref:System.Runtime.Loader.AssemblyLoadContext> that can load all the types required by the proxy interface.
56
+
When in a .NET process with multiple <xref:System.Runtime.Loader.AssemblyLoadContext>(ALC) instances, you should consider whether StreamJsonRpc is loaded in an ALC that can load all the types required by the proxy interface.
57
57
58
-
By default, StreamJsonRpc will generate dynamic proxies in the <xref:System.Runtime.Loader.AssemblyLoadContext> that StreamJsonRpc is loaded within.
59
-
This means that if your own code is running in a different <xref:System.Runtime.Loader.AssemblyLoadContext> from StreamJsonRpc and ask for a proxy, the proxy may fail to activate from a type load failure even if your calling code *can* or has loaded that type.
60
-
It might also manifest as an <xref:System.MissingMethodException> or <xref:System.InvalidCastException> due to types loading into multiple <xref:System.Runtime.Loader.AssemblyLoadContext> instances.
58
+
By default, StreamJsonRpc will generate dynamic proxies in the ALC that the (first) interface requested for the proxy is loaded within.
59
+
This is usually the right choice because the interface should be in an ALC that can resolve all the interface's type references.
60
+
When you request a proxy that implements *multiple* interfaces, and if those interfaces are loaded in different ALCs, you *may* need to control which ALC the proxy is generated in.
61
+
The need to control this may manifest as an <xref:System.MissingMethodException> or <xref:System.InvalidCastException> due to types loading into multiple ALC instances.
61
62
62
-
In such cases, you may control the <xref:System.Runtime.Loader.AssemblyLoadContext> used to generate the proxy by surrounding your proxy request with a call to <xref:System.Runtime.Loader.AssemblyLoadContext.EnterContextualReflection*> (and disposal of its result).
63
+
In such cases, you may control the ALC used to generate the proxy by surrounding your proxy request with a call to <xref:System.Runtime.Loader.AssemblyLoadContext.EnterContextualReflection*> (and disposal of its result).
63
64
64
-
For example, you might use the following code when StreamJsonRpc is loaded into a different <xref:System.Runtime.Loader.AssemblyLoadContext> from your own code:
65
+
For example, you might use the following code when StreamJsonRpc is loaded into a different ALC from your own code:
65
66
66
67
```cs
67
-
IMyServiceproxy;
68
-
using (AssemblyLoadContext.EnterContextualReflection(MethodBase.GetCurrentMethod()!.DeclaringType!.Assembly))
68
+
// Whatever ALC can resolve *all* type references in *all* proxy interfaces.
// We have to key the dynamic assembly by ALC as well, since callers may set a custom contextual reflection context
766
767
// that influences how the assembly will resolve its type references.
767
-
AssemblyLoadContextalc=AssemblyLoadContext.CurrentContextualReflectionContext??AssemblyLoadContext.GetLoadContext(typeof(ProxyGeneration).Assembly)??thrownewException("No ALC for our own assembly!");
768
+
// If they haven't set a contextual one, we assume the ALC that defines the (first) proxy interface.
0 commit comments