【问题标题】:Dynamic Assembly Loading in .Net 4.0.Net 4.0 中的动态程序集加载
【发布时间】:2013-10-29 11:16:15
【问题描述】:

我的问题始于将 .Net 2.0 应用程序迁移到 .Net 4.0。我必须这样做的原因是 Windows 8 默认不启用早期的 .Net 版本,我的应用程序无法要求用户启用它。

该应用程序是一个 NPAPI 插件,它通过 UnmanagedExports 使用 .Net 组件。我将它设计为低完整性应用程序,因此它必须驻留在用户的“LocalLow”目录中。

在我的应用程序中,我使用动态程序集加载机制在运行时加载多个程序集。我使用以下方法加载程序集,

MyInterface Instance;

Assembly assembly = Assembly.LoadFrom(AssemblyFile);
Type type = assembly.GetType(Identifier); // Identifier is implementing the MyInterface 
Instance = Activator.CreateInstance(type) as MyInterface;

// Do something with the Instance

将项目修改为 .Net 4.0 后,我注意到当二进制文件放在 LocalLow 目录中时插件会崩溃(它在其他地方也可以使用)。我的下一步是用最少的代码创建一个简约的插件来解决问题。我注意到动态程序集加载失败,出现以下异常,

System.IO.FileLoadException: Could not load file or assembly '<assemblyPath>' or one of its dependencies. Operation is not supported. (Exception from HRESULT: 0x80131515 (COR_E_NOTSUPPORTED)) --->

System.NotSupportedException: An attempt was made to load an assembly from a network location which would have caused the assembly to be sandboxed in previous versions of the .NET Framework. This release of the .NET Framework does not enable CAS policy by default, so this load may be dangerous. If this load is not intended to sandbox the assembly, please enable the loadFromRemoteSources switch. See http://go.microsoft.com/fwlink/?LinkId=131738 for more information.

我尝试了以下方法来创建一个单独的域并加载程序集,但没有成功,

添加配置“loadFromRemoteSources”也不起作用。 .Net 组件似乎没有加载 .dll.config 文件。 (可能是因为 UnmanagedExporting

我的问题是,

  • 是否可以从 LocalLow 动态加载程序集?
  • CLR 4.0 中的新 CAS 策略是否也适用于 LocalLow? 据我所知,它应该只影响通过网络加载的程序集
  • 还有其他方法可以解决这个问题吗?

【问题讨论】:

  • 你为什么在LocalLow?这是一个专门保护不受信任的应用程序(低完整性进程)可写的目录。由于它用于 Internet Explorer 和其他浏览器,它可能被视为“网络位置”,但它可能只是 dll 上的 MOTW。
  • 我不得不切换到 LocalLow 的原因是因为 Internet Explorer 在运行插件时会阻止对所有其他路径的访问,尤其是当我们正在写入文件时,check this link。唯一的选择是使用 LocalLow。
  • 正是我的观点。由于它对插件是可写的,.Net 将其从加载程序集列入黑名单。 LocalLow 用于需要 插件编写的文件,而不是插件本身。在另一个目录中找到插件(想到程序文件)并将您的临时数据文件写入 LocalLow。

标签: c# .net .net-assembly unmanaged dynamic-assemblies


【解决方案1】:

虽然它没有专门解决您的 LocalLow 问题,但如果您能够从目录中“读取文件”,则可以使用此处详述的“解决方法”: How can I get LabView to stop locking my .NET DLL?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-19
    • 2010-10-27
    • 1970-01-01
    相关资源
    最近更新 更多