1

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

Jehof
  • 34,674
  • 10
  • 123
  • 155
Numan Hanif
  • 246
  • 2
  • 17
  • What exception? Post the message with stack trace – Sriram Sakthivel Sep 24 '13 at 07:01
  • @SriramSakthivel : at System.Reflection.RuntimeAssembly._nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks) at System.Reflection.RuntimeAssembly.InternalLoadAssemblyName(AssemblyName assemblyRef, Evidence assemblySecurity, RuntimeAssembly reqAssembly, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks) – Numan Hanif Sep 24 '13 at 07:31

2 Answers2

1

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);
}
Serve Laurijssen
  • 9,266
  • 5
  • 45
  • 98
  • I did'nt get your point AppDomainCallBack() will be part of my main AppDomain or it should be placed in proxy class ?? – Numan Hanif Sep 24 '13 at 08:01
  • I would have put everything from Proxy in AssemblyProxy and then load the dll in the context of the new AppDomain with DoCallback. Then you can either use LoadFrom and pass a full dll path or use Assembly resolving with AppDomain.CurrentDomain.AssemblyResolve. The drawback is that communication between AppDomains has to be serialized – Serve Laurijssen Sep 24 '13 at 08:47
0

I Have solved the issue by reading following Blog Post: the problem in my case is that i am returning System.Type Object from new domain which is no allowed you can return strings from proxy object but not System.Type object

Link

Blu
  • 4,036
  • 6
  • 38
  • 65
Numan Hanif
  • 246
  • 2
  • 17