加载DLL到另一个域例外加载、DLL

由网友(原来原来,原来如此。)分享简介:你好我加载的dll到另一个领域,它在加载到该域工作正常,但是当我想从通过代理对象域的一些信息也给了我异常下面是code审查是否有任何错误的一步??? 公共类AssemblyProxy{的System.Type [] _​​types;公众的System.Type [] GetTypes(){返回_types;}公共字符...

你好我加载的dll到另一个领域,它在加载到该域工作正常,但是当我想从通过代理对象域的一些信息也给了我异常下面是code审查是否有任何错误的一步???

 公共类AssemblyProxy
    {
        的System.Type [] _​​types;

    公众的System.Type [] GetTypes()
    {
        返回_types;
    }

    公共字符串全名{获得;组; }

    公共无效LoadAssembly(字符串路径)
    {
        尝试
        {
            证据证据=新证据(AppDomain.CurrentDomain.Evidence);

            AppDomain中TESTDOMAIN = AppDomain.CreateDomain(AssemblyDomain,证据,AppDomain.CurrentDomain.BaseDirectory,System.IO.Path.GetFullPath(路径),TRUE);

            代理_asmProxy =(代理)TestDomain.CreateInstanceFromAndUnwrap(AppDomain.CurrentDomain.BaseDirectory +Common.dll的typeof(代理).FullName);

            _asmProxy.LoadAssembly(路径);

            全名= _asmProxy.FullName;
            _types = _asmProxy.GetTypes(); //这里我得到异常[无法加载文件或程序集]

            AppDomain.Unload(TESTDOMAIN);
        }
        赶上(例外前)
        {

        }

    }

}

级代理:MarshalByRefObject的
{
    的System.Type [] _​​types;

    公共字符串全名{获得;组; }

    公众的System.Type [] GetTypes()
    {
        返回_types;
    }

    公共无效LoadAssembly(字符串路径)
    {
        System.Reflection.Assembly _Assembly = System.Reflection.Assembly.Load(System.IO.File.ReadAllBytes(路径));
        _types = _assembly.GetTypes();
        全名= _assembly.FullName;
    }
}
 

我得到的例外是:

  

无法加载文件或程序集

解决方案

我解决这个问题的方法是通过调用LoadFrom(不加载)和应用程序域的上下文:

  sDomain = AppDomain.CreateDomain(域名);
sDomain.DoCallBack(AppDomainCallback);

//运行在的AppDomain的背景下
私人无效AppDomainCallback()
{
  装配装配= Assembly.LoadFrom(mAssemblyName);
}
 

Serv U 安装配置以及外网访问使用

Hi I am loading dll into another domain, it works fine when loaded into that domain but when i want some information from that domain through proxy object it gives me exception below is the code for review is there any wrong step ???

 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;
    }
}

The exception I get is:

Can not load file or assembly

解决方案

The way I solved this problem was by calling LoadFrom (not Load) and in the context of the AppDomain:

sDomain = AppDomain.CreateDomain(DOMAIN_NAME);
sDomain.DoCallBack(AppDomainCallback);

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

阅读全文

相关推荐

最新文章