How to load dependent Assembly using <dependentAssembly>

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kiranrajmane9859
    New Member
    • Mar 2009
    • 2

    How to load dependent Assembly using <dependentAssembly>

    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"
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    You need to load all 3 of the assemblies required by your application, not just your ProjBL.DLL.

    Comment

    • kiranrajmane9859
      New Member
      • Mar 2009
      • 2

      #3
      I have used following method which doesnot work.
      I loads individual assembly but if we combine result it doesn't work.
      I think there requires linking between these assemblies and i doesn't know exact way.

      =============== =============== =============== =======
      Code:
      private Assembly ReadAssemblyFromMemory()
          {       
              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;
        }
      =============== =============== =============== =======

      To solve this problem I have done it in another way as below method


      Code:
      private Assembly ReadTotalAssemblyFromList()
          {
              FileInfo fileName = new FileInfo(AssemblyPath);
              DirectoryInfo dllDirectory = new DirectoryInfo(AssemblyPath.Replace(fileName.Name,""));
              FileInfo[] dllFiles = dllDirectory.GetFiles("*.dll");
              int dllCount = dllFiles.Length;
              FileStream fs = null;
              byte[] memory = null;
              Assembly CombinedAssembly = null;
              if (dllCount > 0)
              {
                  for (int fileCount = dllCount - 1; fileCount >= 0; fileCount--)
                  {
                      if (dllFiles[fileCount].Name == fileName.Name)   // Loads ProjBL.dll at last after loading ProjMC.dll and ProjDL.dll
                      {
                          fs = new FileStream(dllFiles[fileCount].FullName, FileMode.Open);
                          BinaryReader br = new BinaryReader(fs);
                          memory = br.ReadBytes(Convert.ToInt32(fs.Length));
                          assemblyUsed = Assembly.Load(memory);
                          br.Close();
                          fs.Close();
                      }
                      else    // Loads ProjMC.dll and ProjDL.dll
                      {
                          fs = new FileStream(dllFiles[fileCount].FullName, FileMode.Open);
                          BinaryReader br = new BinaryReader(fs);
                          memory = br.ReadBytes(Convert.ToInt32(fs.Length));
                          assemblyUsed = Assembly.Load(memory);
                          br.Close();
                          fs.Close();
                      }                         
                  }
              }
              return assemblyUsed;
          }

      =============== =============== =============== ====

      Please provide the way to combine these Assemblies . Above two way doesn't work
      Last edited by Frinavale; Mar 3 '09, 05:08 AM. Reason: Added [code] tags: Please post code in [code] [/code] tags

      Comment

      Working...