【问题标题】:Load dll into another domain exception将 dll 加载到另一个域异常中
【发布时间】:2013-09-29 06:27:11
【问题描述】:

嗨,我正在将 dll 加载到另一个域中,当加载到该域时它工作正常,但是当我想通过代理对象从该域获取一些信息时,它给了我以下异常是审查代码是否有任何错误步骤???

 public class AssemblyProxy
    {
        System.Type[] _types;

    public System.Type[] GetTypes() 
    {
        return _types;
    }

    public string FullName { get; set; }

    public void LoadAssembly(string path)
    {
        try
        {
            Evidence evidence = new Evidence(AppDomain.CurrentDomain.Evidence);

            AppDomain TestDomain = AppDomain.CreateDomain("AssemblyDomain", evidence, AppDomain.CurrentDomain.BaseDirectory, System.IO.Path.GetFullPath(path), true);

            Proxy _asmProxy = (Proxy)TestDomain.CreateInstanceFromAndUnwrap(AppDomain.CurrentDomain.BaseDirectory+"Common.dll", typeof(Proxy).FullName);

            _asmProxy.LoadAssembly(path);

            FullName = _asmProxy.FullName;
            _types = _asmProxy.GetTypes(); //Here i got Exception [Can not load file or assembly]

            AppDomain.Unload(TestDomain);
        }
        catch (Exception ex) 
        {

        }

    }

}

class Proxy : MarshalByRefObject
{
    System.Type[] _types;

    public string FullName { get; set; }

    public System.Type[] GetTypes() 
    {
        return _types;                    
    }

    public void LoadAssembly(string path) 
    {
        System.Reflection.Assembly _assembly = System.Reflection.Assembly.Load(System.IO.File.ReadAllBytes(path));
        _types = _assembly.GetTypes();
        FullName = _assembly.FullName;
    }
}

我得到的例外是:

无法加载文件或程序集

【问题讨论】:

  • 什么异常?使用堆栈跟踪发布消息
  • @SriramSakthivel :在 System.Reflection.RuntimeAssembly._nLoad(AssemblyName 文件名,字符串代码库,证据 assemblySecurity,RuntimeAssembly locationHint,StackCrawlMark 和 stackMark,IntPtr pPrivHostBinder,布尔 throwOnFileNotFound,布尔 forIntrospection,布尔suppressSecurityChecks)在系统。 Reflection.RuntimeAssembly.InternalLoadAssemblyName(AssemblyName assemblyRef, Evidence assemblySecurity, RuntimeAssembly reqAssembly, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)

标签: c# .net applicationdomain


【解决方案1】:

我通过阅读以下博客文章解决了这个问题:我的问题是我从新域返回 System.Type 对象,这是不允许的,您可以从代理对象返回字符串,但不能从 System.Type 对象返回

p>

Link

【讨论】:

    【解决方案2】:

    我解决这个问题的方法是在AppDomain的上下文中调用LoadFrom(不是Load)

    sDomain = AppDomain.CreateDomain(DOMAIN_NAME);
    sDomain.DoCallBack(AppDomainCallback);
    
    // runs in the context of the AppDomain
    private void AppDomainCallback()
    {
      Assembly assembly = Assembly.LoadFrom(mAssemblyName);
    }
    

    【讨论】:

    • 我没明白你的意思 AppDomainCallBack() 将成为我的主 AppDomain 的一部分,还是应该放在代理类中??
    • 我会将 Proxy 中的所有内容放入 AssemblyProxy 中,然后使用 DoCallback 在新 AppDomain 的上下文中加载 dll。然后,您可以使用 LoadFrom 并传递完整的 dll 路径,也可以使用 AppDomain.CurrentDomain.AssemblyResolve 进行程序集解析。缺点是AppDomains之间的通信必须序列化
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-12
    • 1970-01-01
    相关资源
    最近更新 更多