Note that there are some explanatory texts on larger screens.

plurals
  1. PO.NET Remoting and HttpContext.Current
    text
    copied!<p>We have a plugin system where the plugin code runs on a separate AppDomain from the main process, using .NET remoting for the objects to communicate. </p> <p>One class is similar to HttpContext.Current (which also suffers from the problem) (edit, the actual implementation):</p> <pre><code>public class MyClass { public static MyClass Instance { get { if(HttpContext.Current != null) return HttpContext.Current.Items["MyClassInstance"]; } set { if(HttpContext.Current != null) HttpContext.Current.Items["MyClassInstance"] = value; } } } </code></pre> <p>Then, we have a communicating object which inherits from MarshalByRefObject:</p> <pre><code>public class CommunicatingClass : MarshalByRefObject, ICommunicatingClass { public void DoSomething() { MyClass.Instance.DoSomething(); } } </code></pre> <p>The CommunicatingClass is created on the main AppDomain, and works fine. Then, there's the plugin class, which is created on its AppDomain, and given an instance of the CommunicatingClass:</p> <pre><code>public class PluginClass { public void DoSomething(ICommunicatingClass communicatingClass) { communicatingClass.DoSomething(); } } </code></pre> <p>The problem is, that even though CommunicatingClass resides on the main appdomain (verified with the Immediate Window), all of the static data such as MyClass.Instance and HttpContext.Current have disappeared and are null. I have a feeling that MyClass.Instance is somehow being retrieved from the plugin AppDomain, but am unsure how to resolve this.</p> <p>I saw another question that suggested <code>RemotingServices.Marshal</code>, but that did not seem to help, or I used it incorrectly. Is there a way that the CommunicatingClass can access all static methods and properties like any other class in the main AppDomain?</p> <p>Edit:</p> <p>The PluginClass is given an instance like this:</p> <pre><code>public static PluginClass Create() { var appDomain = GetNewAppDomain(); var instance = (PluginClass)appDomain.CreateInstanceAndUnwrap(assembly, type); instance.Communicator = new CommunicatingClass(); return instance; } </code></pre> <p>Edit 2:</p> <p>Might have found the source of the problem. MyClass.Instance is stored in HttpContext.Current.Items (see above edit).</p> <p>Is there any way at all that HttpContext.Current can access the correct HttpContext? I'm still wondering why, even though it is being run in HttpContext.Current's AppDomain, CommunicatingClass.DoSomething, when calling MyClass.Instance, retrieves stuff from PluginClass' AppDomain (if that makes any sense).</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload