【发布时间】:2012-09-21 00:28:17
【问题描述】:
我有一个从不同的 dll 文件加载外部程序集的应用程序。我正在将程序集加载到之前创建的另一个域。所有程序集都返回 XElement 对象,我需要将此对象用于主 AppDomain 中的其他方法,但 XElement 对象没有 Serializable 属性,这就是我无法按原样发送此对象的原因。从外部库中获取 XElement 对象非常重要。我尝试为此使用序列化,但每次我都失败了。
我尝试创建一些包装类。这个类只是从 XElement 创建流,我尝试在主域中从这个流中读取,但是这个流当时是关闭的。
如果有人能帮我解决这个问题,我会很高兴。 提前致谢。
private XElement CallModule(string modulePath, string moduleName,
Dictionary<string, string> parameters)
{
AppDomainSetup moduleDomainSetup = new AppDomainSetup();
moduleDomainSetup.ApplicationBase = AppDomain.CurrentDomain.RelativeSearchPath;
AppDomain moduleDomain =
AppDomain.CreateDomain("moduleDomain", null, moduleDomainSetup);
try
{
Module remoteobj = (Module)moduleDomain.CreateInstanceFromAndUnwrap(
HttpContext.Current.Request.MapPath(modulePath),
moduleName + "." + moduleName);
Module.WrappedStream remoteResult =
remoteobj.Execute(queryString["command"], parameters);
XElement res = XElement.Load(remoteResult.Stream);
return res;
}
catch (Exception ex)
{
throw ex;
}
finally
{
AppDomain.Unload(moduleDomain);
}
}
【问题讨论】:
-
我觉得很奇怪,XElement 确认了 IXmlSerializable 接口 - ( public class XElement : XContainer, IXmlSerializable ) 你测试过只返回字符串还是 int,对你有用吗。
-
@orn,XElement 不是二进制可序列化的,也不是通过引用编组的,这是跨应用程序域使用它所必需的。
-
Yaroslav,您的示例缺少在另一个 AppDomain 中运行并创建流的代码部分。因此,无法诊断该特定调用失败的原因。 (无论如何,我认为这不是一个好主意 - 似乎是我的回答)。