Loading assemblies and its dependencies

前端 未结 3 1881
醉话见心
醉话见心 2020-12-05 07:29

My application dynamically loads assemblies at runtime from specific subfolders. These assemblies are compiled with dependencies to other assemblies. The runtime trys to loa

相关标签:
3条回答
  • 2020-12-05 08:03

    One nice approach I've used lately is to add an event handler for the AppDomain's AssemblyResolve event.

    AppDomain currentDomain = AppDomain.CurrentDomain;
    currentDomain.AssemblyResolve += new ResolveEventHandler(MyResolveEventHandler);
    

    Then in the event handler method you can load the assembly that was attempted to be resolved using one of the Assembly.Load, Assembly.LoadFrom overrides and return it from the method.

    EDIT:

    Based on your additional information I think using the technique above, specifically resolving the references to an assembly yourself is the only real approach that is going to work without restructuring your app. What it gives you is that the location of each and every assembly that the CLR fails to resolve can be determined and loaded by your code at runtime... I've used this in similar situations for both pluggable architectures and for an assembly reference integrity scanning tool.

    0 讨论(0)
  • 2020-12-05 08:04

    You can use the <codeBase> element found in the application configuration file. More information on "Locating the Assembly through Codebases or Probing".

    Well, the loaded assembly doesn't have an application configuration file.

    Well if you know the specific folders at runtime you can use Assembly.LoadFrom.

    0 讨论(0)
  • 2020-12-05 08:26

    You can use the <probing> element in a manifest file to tell the Runtime to look in different directories for its assembly files.

    http://msdn.microsoft.com/en-us/library/823z9h8w.aspx

    e.g.:

    <configuration>
     <runtime>
      <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
         <probing privatePath="bin;bin2\subbin;bin3"/>
      </assemblyBinding>
     </runtime>
    </configuration>
    
    0 讨论(0)
提交回复
热议问题