【问题标题】:Loading DLL file with AppDomain.Load into newly created app domain.使用 AppDomain.Load 将 DLL 文件加载到新创建的应用程序域中。
【发布时间】:2018-03-12 22:38:29
【问题描述】:

我只是想弄清楚 AppDomain 并将 DLL 从不同位置加载到新创建的 AppDomain 中,但遇到了困难。我知道我将使用不同的方法,但我仍在试图弄清楚这一点。这是该示例的背景故事。

我有一个名为 MyReadableDLL.dll 的 dll,位于 c:\DLLTest。内容如下:

namespace MyReadableDLL
{
        public class ReadThis : System.MarshalByRefObject
        {
        public string RetString()
            {
                    return "You read this from 'Read This!'";
            }
        }
}

我在一个单独的项目中创建了一个 DomainBuilder,内容如下:

    class DomainBuilder
    {
        AppDomainSetup domaininfo = new AppDomainSetup();
        AppDomain appDomain;

        public DomainBuilder()
        {
            domaininfo.ApplicationBase = @"c:\DLLtest";
                    appDomain = AppDomain.CreateDomain("MyTestDomain", null, domaininfo);
        var currentdomain = AppDomain.CurrentDomain.FriendlyName;
                var temporarydomain = appDomain.FriendlyName;
                var appbase = appDomain.SetupInformation.ApplicationBase;
                Assembly assembly = appDomain.Load("MyReadableDLL");
    }
}

我不明白为什么当我获得 appDomain.Load 行时它会失败。根据我对 AddDomainSetup 的了解,AppliationBase 设置了用于检查 EXE/DLL 文件的默认目录,并且该文件夹中只有 MyReadableDLL 文件。 我已经看到了许多使用 CreateInstanceFromandUnwrap 的解决方案,我确信我会朝那个方向发展。与此同时,我试图找到一个工作示例 1) AddDomain 创建然后 2) AppDomain.Load 到新创建的域中。就是这样。我错过了什么?

【问题讨论】:

    标签: appdomain appdomainsetup


    【解决方案1】:

    上述场景中明显失败的原因有很多。

    1. AppDomian.Load 需要程序集的全名

      "EventsPublisher, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
      
    2. 确保以管理员身份运行程序以克服安全异常

    请查看以下理解 AppDomain 的示例,其中显示了在另一个应用程序域中执行代码的最简单方法。

    using System;
    using System.Reflection;
    
    public class Worker : MarshalByRefObject
    {
        public void PrintDomain() 
        { 
            Console.WriteLine("Object is executing in AppDomain \"{0}\"",
                AppDomain.CurrentDomain.FriendlyName); 
        }
    }
    
    class Example
    {
        public static void Main()
        {
            // Create an ordinary instance in the current AppDomain
            Worker localWorker = new Worker();
            localWorker.PrintDomain();
    
            // Create a new application domain, create an instance
            // of Worker in the application domain, and execute code
            // there.
            AppDomain ad = AppDomain.CreateDomain("New domain");
            Worker remoteWorker = (Worker) ad.CreateInstanceAndUnwrap(
                typeof(Worker).Assembly.FullName,
                "Worker");
            remoteWorker.PrintDomain();
        }
    }
    
    /* This code produces output similar to the following:
    
    Object is executing in AppDomain "source.exe"
    Object is executing in AppDomain "New domain"
     */
    

    查看以下示例以加载程序集

    using System;
    using System.Reflection;
    
    class Test {
    
       static void Main() {
          InstantiateINT32(true);      // OK!
       }
    
       static void InstantiateINT32(bool ignoreCase) {
          try {
             AppDomain currentDomain = AppDomain.CurrentDomain;
             object instance = currentDomain.CreateInstanceAndUnwrap(
                "mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089",
                "SYSTEM.INT32",
                ignoreCase,
                BindingFlags.Default,
                null,
                null,
                null,
                null,
                null
             );
             Console.WriteLine(instance.GetType());
          } catch (TypeLoadException e) {
             Console.WriteLine(e.Message);
          }
       }
    }
    

    我写了一篇博客,介绍了相同的介绍。也可以随时查看我的博客以获取更多详细信息。 Sending Events using AppDomain

    【讨论】:

      猜你喜欢
      • 2011-09-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多