0

我有一个 DLL,我在需要自己处理外部程序集负载的环境中使用它。我想将程序集加载到 AppDomain 中。当我使用 CurrentAppDomain 尝试此操作时效果很好,但是在我自己创建的应用程序域中执行此操作时失败。背景是我想在最后卸载appdomain,以便最终“释放”程序集。

public ZipEx() 
{
    try
    {
        AppDomainSetup domaininfo = new AppDomainSetup();
        domaininfo.ApplicationBase = System.Environment.CurrentDirectory;
        Evidence adevidence = AppDomain.CurrentDomain.Evidence;

        //THIS CODE WORKS
        //System.AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
        //System.AppDomain.CurrentDomain.Load("ICSharpCode.SharpZipLib");

        //THIS CODE DOES NOT WORK
        AppDomain zipDomain2 = AppDomain.CreateDomain("ADZib2", adevidence, domaininfo);
        PolicyLevel polLevel = PolicyLevel.CreateAppDomainLevel();
        PermissionSet permSet = new PermissionSet(PermissionState.Unrestricted);
        permSet.AddPermission(new SecurityPermission(SecurityPermissionFlag.AllFlags));
        polLevel.RootCodeGroup.PolicyStatement = new PolicyStatement(permSet);
        zipDomain2.SetAppDomainPolicy(polLevel);
        zipDomain2.AssemblyResolve += CurrentDomain_AssemblyResolve;
    }
    catch (Exception ex)
    {
        System.Windows.Forms.MessageBox.Show("ex in ctor" + Environment.NewLine + ex.ToString());
    }
    if (_loadedAssembly == null)
    {
    }
}

Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
{
    System.Windows.Forms.MessageBox.Show("CurrentDomain_AssemblyResolve");
    Assembly assembly = null;
    bool foundAssembly = false;

    int idx = args.Name.IndexOf(',');
    if (idx > 0)
    {
        string partialName = args.Name.Substring(0, idx);
        string dllName = partialName + ".dll";

        //Add the directorys where the assembly hould be resolved
        List<string> directorySearch = new List<string>
        {
          string.Format("{0}{1}{2}",Environment.CurrentDirectory,Path.DirectorySeparatorChar, dllName),
          string.Format("{0}{1}{2}",AppPath,Path.DirectorySeparatorChar, dllName)
        };

        foreach (string fileName in directorySearch)
        {
            if (File.Exists(fileName))
            {
                foundAssembly = true;
                assembly = Assembly.LoadFrom(fileName);
                break;
            }
        }

        if (assembly == null)
        {
            if (!foundAssembly)
            {
                foreach (string fileName in directorySearch)
                {
                }
            }
            else
            {
            }
        }
    }
    if (assembly != null) 
    {
        System.Windows.Forms.MessageBox.Show("assembly is not null");
    }
    return assembly;
}

我的问题是如何使用我创建的 appdomain 来加载程序集?

4

1 回答 1

1

在您当前的示例中,我没有看到任何未注释的代码会导致新创建的 AppDomain 尝试加载程序集。我假设在此答案中未注释的代码中缺少对 Load 的调用。

根据AppDomain.Load帮助中的注释,它应该只在当前 AppDomain 中使用,否则它会尝试将程序集加载到 BOTH AppDomains 中,这可能是您遇到异常的原因。要将程序集仅加载到另一个 AppDomain,您应该调用其中一个 CreateInstance 函数。在这种特殊情况下,我建议使用CreateInstanceFromAndUnwrap函数,因为它允许您按名称和类型指定程序集。

如果您没有想要实例化的类型并且能够跨 AppDomain 边界进行交互(这似乎不太可能但可能),您可能必须创建并丢弃一些简单的类型,例如枚举或结构才能使其工作。

于 2015-01-28T15:00:33.973 回答