我在两个 .NET 应用程序域之间传递一些数据时遇到了一些麻烦,我希望这里有人可以帮助我。
基本上我拥有的是一个主要应用程序(Main) 加载程序集A and B进入它的主域,然后当我运行插件时(C) Main调用创建域方法B它创建一个新域并加载C和一个实例B进入其中,这样C只能访问B而不是其他人。
B包含一个指向 IDispatch 的指针Main但似乎只有在将其加载到新域后才能获得它C。我想做的是从新域实例发送指针的副本B并将其发送至A它仍在默认域中运行。
仅供我控制的记录A、B、C但不是Main
抱歉,如果这有点难以理解,我尽力解释一下。
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.)
}
所以最后看起来像这样:
默认域:
插入域:
在上面的代码中,您正在调用
AppDomain.CurrentDomain.CreateInstanceAndUnwrap(...)
这只是在当前域中创建对象的一种迂回方式,就像您刚刚调用构造函数一样。您需要在远程域上调用该方法,即。
AppDomain domain = AppDomain.Create(...)
Tunnel tunnel = (Tunnel)domain.CreateInstanceAndUnwrap(...)
如果您随后调用将在远程对象上运行的tunnel.SetPointer(...)。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)