【问题标题】:Unable to determine the identity of domain using System.IO.Packaging无法使用 System.IO.Packaging 确定域的身份
【发布时间】:2012-05-19 01:53:13
【问题描述】:

我在通过 COM 互操作使用 System.IO.Packaging 时收到“无法确定域的身份”,有几篇文章描述了为什么会发生这种情况,解决方案是在其自己的 AppDomain 中运行有问题的函数。

所以我采用了示例代码,如下所示,但我仍然收到错误,我想知道我做错了什么,而且,对于 VS 2010,它说 AddAssembly 和 AddHost 已过时 - 我想知道这是否意味着它们不再实现,但如果是这样的话,我真的不明白如何使用新方法(AddAssemblyEvidence 和 AddHostEvidence)??

 AppDomainSetup setup = new AppDomainSetup();
 setup.ApplicationBase = AppDomain.CurrentDomain.BaseDirectory;

 Evidence evidence = new Evidence(AppDomain.CurrentDomain.Evidence);
 evidence.AddAssembly(Assembly.GetExecutingAssembly().FullName);
 evidence.AddHost(new Zone(SecurityZone.MyComputer));

 AppDomain domain = AppDomain.CreateDomain("BlobPackage", evidence, setup);

 BlobPackage blob_interal = (BlobPackage)domain.CreateInstanceAndUnwrap(typeof(BlobPackage).Assembly.FullName, typeof(BlobPackage).FullName);

 blob_interal.pack(FilePath, RootPath, m_source_files); <-- STILL FAILS

 AppDomain.Unload(domain);

【问题讨论】:

  • 你可以这样使用新方法:evidence.AddAssemblyEvidence(new ApplicationDirectory(Assembly.GetExecutingAssembly().FullName));evidence.AddHostEvidence(new Zone(SecurityZone.MyComputer));

标签: com interop appdomain system.io.packaging


【解决方案1】:

我自己解决了这个问题,我忘了从 MarshalByRefObject 继承我的类。

它有点愚蠢,它允许你创建一个实例并调用它,除非它仍在默认域中运行,你会认为它会抛出异常或其他东西,无论如何通过将类标记为 [Serializable()] 和从 MarshalByRefObject 派生修复它。

【讨论】:

  • 在考虑跨应用程序域调用时,用SerializableAttribute 标记类并从MarshalByRefObject 派生它在某种程度上是相互排斥的。
猜你喜欢
  • 1970-01-01
  • 2013-03-25
  • 1970-01-01
  • 2014-01-28
  • 1970-01-01
  • 2016-05-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多