Passing data through appdomains using MarshalByRefObject

I have a slight problem transferring some data between two .NET domains, and I hope someone here can help me.

Basically what I have is the main application ( Main ), which loads the main A and B into it , then when I run the plugin ( C ), the Main calls the domain B domain method , which creates a new domain and loads C and instance B so that C can access only B and not others.

B contains a pointer to IDispatch Main , but only it seems to get it after loading it into a new domain using C. What I'm trying to do is send a copy of the pointer from a new instance of domain B and send it to A , which is still works in the default domain.

For recording only, I control A, B and C , but not Home

Sorry if this is a little hard to understand, I tried to explain it.

code:

In A:

public class Tunnel : MarshalByRefObject
{
    public void SetPointer(int dispID)
    {
        IntPtr pointer = new IntPtr(dispID);
    }
}

In B:

//Call by Main after loading plug in but after A.dll is loaded.
public void CreateDomain()
{
  AppDomain maindomain= AppDomain.CurrentDomain;
  tunnel = (Tunnel)maindomain.CreateInstanceAndUnwrap(typeof(Tunnel).FullName,
                                                      typeof(Tunnel).FullName);

   AppDomain domain = base.CreateDomain(friendlyName, securityInfo, appDomainInfo);
   //Load assembly C (plug in) in domain.
   // C uses B so it loads a new instance of B into the domain also at the same time.

  // If I do this here it creates new instance of A but I need to use the one in
  // the main domain.
  //tunnel = (Tunnel)domain.CreateInstanceAndUnwrap(typeof(Tunnel).FullName,
                                                    typeof(Tunnel).FullName);
  tunnel.SetPointer(//Send data from B loaded in new domain.)

}

So in the end it looks something like this:

Default Domain:

  • Main.dll
  • A.DLL
  • B.dll

Connect domain:

  • B.dll
  • C.dll
+3
source share
3 answers

AppDomain.CurrentDomain.CreateInstanceAndUnwrap(...)

, , . , ..

AppDomain domain = AppDomain.Create(...)
Tunnel tunnel = (Tunnel)domain.CreateInstanceAndUnwrap(...)

tunnel.SetPointer(...), .

+8

, , :

class B : MarshallByRef

, A:

AppDomain domain = ...
B inst = (B) domain.CreateInstanceAndUnwrap(typeof(B).Assembly.FullName, typeof(B).FullName);
inst.DoPlugin("C");

DoPlugin() "C" .

0

, ( unit test):

var dom = AppDomain.CreateDomain("NewDomain",
    AppDomain.CurrentDomain.Evidence, AppDomain.CurrentDomain.BaseDirectory,
    AppDomain.CurrentDomain.RelativeSearchPath, false);
try
{
    var tunnel = (MyMarshallByRef)dom.CreateInstanceAndUnwrap(
        typeof(MyMarshallByRef).Assembly.FullName,
        typeof(MyMarshallByRef).FullName);

    tunnel.DoStuff("data");
}
finally
{
    AppDomain.Unload(dom);
}

, DoStuff [Serializable]

0
source

Source: https://habr.com/ru/post/1723418/


All Articles