【问题标题】:Load assembly to temporary Appdomain from another directory将程序集从另一个目录加载到临时 Appdomain
【发布时间】:2014-05-29 09:46:17
【问题描述】:

作为 BaaS 项目的一部分,我需要从“来宾”程序集中动态执行代码。我已经尝试了其他类似问题中的所有示例和答案,包括 AppDomainToolKit 方法,但没有运气。

与插件方法类似,我通过将相关程序集复制到主机应用程序的 bin 路径中得到了肯定的结果。但是,这对于当前场景是不可能的:

  • 需要根据请求应用权限和限制
  • 应在临时应用域中评估每个请求
  • 从临时域的路径加载所需的汇编和引用类型

到目前为止,我最新的一段代码如下

        // ClassLibrary1.dll and ClassLibrary2.dll are in the same directory, both are marked as Serializable

        var binPath =  @"C:\AssemblyDemo\ClassLibrary1\bin\Debug\";
        var lib1Path = binPath + "ClassLibrary1.dll";
        var lib2Path = binPath + "ClassLibrary2.dll";

        var setup = new AppDomainSetup();
        setup.ApplicationBase = binPath;
        AppDomain domain = AppDomain.CreateDomain("domainname", null, setup);
        ObjectHandle handle = domain.CreateInstanceFrom(lib1Path, "ClassLibrary1.Class1");
        var unwrap = handle.Unwrap();
        var m1 = unwrap.GetType().GetMethod("Method1");
        var result = m1.Invoke(unwrap, null);

handle.Unwrap() 抛出异常Type is not resolved for member 'ClassLibrary1.Class1,ClassLibrary1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.

【问题讨论】:

    标签: c# .net .net-assembly appdomain


    【解决方案1】:

    如果您只想在临时 AppDomain 中加载程序集,则不能在主域中调用 UnwrapUnwrap 也在那里加载类型。您必须将对对象的所有访问权从临时程序集移至临时 AppDomain。您可以使用在子域中实例化并从父域调用的 AppDomain.DoCallBack 或自定义 MarshalByRef 类来执行此操作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-23
      • 2021-07-11
      相关资源
      最近更新 更多