tags:

views:

1379

answers:

3

I have three dlls ProjBL.dll , ProjDL.dll and ProjMC.dll.

ProjBL.dll is Business object dll

ProjDL.dll is Data Access layer method dll

ProjMC.dll is Master Class dll contains Properties

ProjDL.dll depends on ProjMC.dll and ProjBL.dll depends on ProjDL.dll

I have load ProjBL.dll in Memory using Assembly.Load() method from folder on D: drive with specified folder.

Currently it gives error that "One of dependent Assembly not found"

The Method Used is as below

    DirectoryInfo dllDirectory = new DirectoryInfo(folderPath); 
    FileInfo[] dllFiles = dllDirectory.GetFiles("*.dll"); 
    int dllCount = dllFiles.Length; 
    FileStream fs = null;         
    if (dllCount > 0) 
    { 
        long streamLength = 0; 
        for (int fileCount = 0; fileCount < dllCount; fileCount++) 
        { 
            fs = new FileStream(dllFiles[fileCount].FullName, FileMode.Open); 
            streamLength += fs.Length; 
            fs.Close(); 
        } 
        byte[] memory = new byte[streamLength]; 
        byte[] memory1 = null; 
        byte[] memory2 = null; 
        byte[] memory3 = null; 

        fs = new FileStream(dllFiles[0].FullName, FileMode.Open); 
        BinaryReader br = new BinaryReader(fs); 
        memory1 = br.ReadBytes(Convert.ToInt32(fs.Length));  // Loads ProjMC.dll 

        fs = new FileStream(dllFiles[1].FullName, FileMode.Open); 
        br = new BinaryReader(fs); 
        memory2 = br.ReadBytes(Convert.ToInt32(fs.Length));  // Loads ProjDA.dll 


        fs = new FileStream(dllFiles[2].FullName, FileMode.Open); 
        br = new BinaryReader(fs); 
        memory3 = br.ReadBytes(Convert.ToInt32(fs.Length));   // Loads ProjBL.dll 

        fs.Close(); 
        br.Close(); 

        memory1.CopyTo(memory, 0); 
        memory2.CopyTo(memory, memory1.Length); 
        memory3.CopyTo(memory, (memory1.Length + memory2.Length)); 

        assemblyUsed = Assembly.Load(memory);   

    }         
    return assemblyUsed;
A: 

Why so complex? Assembly.LoadFrom(string) will do the job nicely. Or you're trying to achieve some really weird behavior?

Anton Gogolev
A: 

Since ProjBL.dll needs ProjDL.dll which needs ProjMC.dll the CLR will need to be able to find ProjDL.dll and ProjMC.dll (and any other dependencies) when you load ProjBL.dll.

It should be able to find them if they're either in the same directory as the executable or in the probepath. You can use fuslogvw to see where the CLR is looking for the dependent assemblies.

Arnshea
+1  A: 

When the current application looks for assemblies, it looks in several locations (bin folder, gac, etc..) if it can not find one, then the developer needs to manually tell the application where to look. You can do this by intercepting the AssemblyResolve event, and using the event args to tell the CLR where your assembly is.

AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
....................
Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
{
   var assemblyPath=
          Path.GetFullPath("..\\..\\..\\example\\" + args.Name.Substring(0,  args.Name.IndexOf(",")) + ".dll");

    return Assembly.LoadFrom(assemblyPath);
}
Climber104