How to load assemblies outside the appbase(more information)

后端 未结 4 1697
悲&欢浪女
悲&欢浪女 2021-01-19 06:13

I want to know how to load assemblies which are outside the Appbase and Appdomain.

My problem is that I have a list of assemblies that are on a shared directory. My

相关标签:
4条回答
  • 2021-01-19 06:17

    7 1/2 years late. You can't https://docs.microsoft.com/en-us/dotnet/framework/configure-apps/specify-assembly-location

    The directories specified in privatePath must be subdirectories of the application base directory.

    0 讨论(0)
  • 2021-01-19 06:18

    There is an event that is fired if it can't find an assembly or one of its references:

    AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve);
    
    Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
            {
    
            }
    

    Add it before you're loading your assemblies. Put a breakpoint in it and in args there should be some info about the assembly you're missing

    0 讨论(0)
  • 2021-01-19 06:34

    What About MSDN - Assembly Load File

    string filePath = "C:\asmPath";
    Assembly myAssembly = Assembly.LoadFile(filePath);
    

    You can also define a probe path in your app.config (which I consider a better solution) and having the CLR loading the assemblies on demand. MSDN Probing Path

    <configuration>
       <runtime>
          <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
             <probing privatePath="bin;bin2\subbin;bin3"/>
          </assemblyBinding>
       </runtime>
    </configuration>
    
    0 讨论(0)
  • 2021-01-19 06:44

    thanks to @hcb's instruction I finally solve this problem:

    AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve);
    
    Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
    {
        if (args.Name.Contains("Dll2Find"))
        {
            try
            {
                return Assembly.LoadFrom(@"D:\findHere\Dll2Find.dll");
            }
            catch (Exception)
            {
                return null;
            }
        }
        return null;
    }
    
    0 讨论(0)
提交回复
热议问题