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
+25Lines changed: 25 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -50,3 +50,28 @@ between client and server.
50
50
Sometimes a client may need to block its caller until a response to a JSON-RPC request comes back.
51
51
The dynamic proxy maintains the same async-only contract that is exposed by the @StreamJsonRpc.JsonRpc class itself.
52
52
[Learn more about sending requests](sendrequest.md), particularly under the heading about async responses.
53
+
54
+
## AssemblyLoadContext considerations
55
+
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
+
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.
62
+
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).
64
+
65
+
For example, you might use the following code when StreamJsonRpc is loaded into a different ALC from your own code:
66
+
67
+
```cs
68
+
// Whatever ALC can resolve *all* type references in *all* proxy interfaces.
This initializes the `proxy` local variable with a proxy that will be able to load all types that your own <xref:System.Runtime.Loader.AssemblyLoadContext> can load.
/// Translates a <see cref="MethodInfo"/> from one ALC into another ALC, so that it can be invoked
37
+
/// within the context of the new ALC.
38
+
/// </summary>
39
+
/// <param name="alc">The <see cref="AssemblyLoadContext"/> to load the method into.</param>
40
+
/// <param name="helperMethodInfo">The <see cref="MethodInfo"/> of the method in the caller's ALC to load into the given <paramref name="alc"/>.</param>
0 commit comments