Problems loading assembly dependencies dynamically at run-time

前端 未结 3 1306
逝去的感伤
逝去的感伤 2021-01-06 08:11

let me try to explain my problem. I\'m currently trying to develop a small \"plugin-framework\" written in .Net (mainly for experimenting a bit). So the idea is to have a ma

相关标签:
3条回答
  • 2021-01-06 08:23

    You can set the probing path of the runtime so it can find the assemblies. Set the probing element in your apps config file.

    <configuration>
      <runtime>
        <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
          <probing privatePath="plugins;dependencies"/>
        </assemblyBinding>
      </runtime>
    </configuration>
    
    0 讨论(0)
  • 2021-01-06 08:24

    BTW: You can speed up loading assemblies considerably if you cache your assemblies that you already resolved in a Dictionary. If A depends on B, C and B depends on C and you load A, AssemblyResolve will be called twice for C, and loading the assembly only once is faster :)

    (I am not sure if it is always the case that AssemblyResolve is called more than once, but I noticed it when debugging a project once. And it does not hurt to cache the assemblies...)

    0 讨论(0)
  • 2021-01-06 08:45

    The AssemblyResolve event happens when the framework tries to load an assembly, and fails.

    What this means is that if it's giving you Presentation.Zune.dll in the args, that the framework can't find that assembly, and this is your chance to intercept it and do other things, like load it from a directory that the framework might not know about - eg your plugins\dependencies folder...

    Off the top of my head, I'd try something like this:

    Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
    {
        if( File.Exists(".\\Plugins\\"+args.Name) )  // it's a plugin
            return Assembly.Load(".\\Plugins\\"+args.Name);
        else if( File.Exists(".\\Plugins\\Dependencies\\"+args.Name) )  // it's a dependency OF a plugin
            return Assembly.Load(".\\Plugins\\Dependencies\\"+args.Name);
        else
            throw new Exception();
    }
    
    0 讨论(0)
提交回复
热议问题