Поделиться через


How to load the assembly from location different from the application path or any of its subdirectories!

Just got a case where customer needed to load C# Assembly from location which is not in the hierarchy of the application path nor is that in the GAC. The application which loads the assembly is a C++ native application using interop.

After searching a lot and trying a bit, could find a KB article which states about it.
https://support.microsoft.com/kb/897297

The solution is, to genrate the configuration file for the C++ executable in the application path. My sample application had the architecture as follows,

C++ native application ----> C# assembly (namespace: CSharpLibrary) --> Another C# assembly (namespace: CSharpLibraryParent)
[Note: Arrow means "uses"]

So, as it involves the interop, I regasm the first C# assembly with /codebase and /tlb option and compiled my C++ native application. But to replicate the scenario, wanted put the last assembly named CSharpLibraryParent in "c:\New Folder" and make it work. So, in this scenario, my configuration file showed up like following,

<configuration>
   <runtime>
      <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
         <dependentAssembly>
            <assemblyIdentity name="CSharpLibrary" culture="neutral" publicKeyToken="d091f52b0033dd95"/>
            <codeBase version="1.0.0.0" href="FILE://D:/Jigar/CSharpLibrary/CSharpLibrary.dll"/>
         </dependentAssembly>

         <dependentAssembly>
            <assemblyIdentity name="CSharpLibraryParent" culture="neutral" publicKeyToken="e87b16e3407e53b1"/>
            <codeBase version="1.0.0.0" href="FILE://C:/New Folder/CSharpLibraryParent.dll"/>
         </dependentAssembly>

      </assemblyBinding>
   </runtime>
</configuration>