当前位置: 动力学知识库 > 问答 > 编程问答 >

c# - How to load assemblies outside the appbase(more information)

问题描述:

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 application needs to load these assemblies, which are located outside the path specified in the Appbase (path to the executable). I do not want to move them into the Appbase folder.

For more information, I have an application which is running in a distributed domain to test a collection of assemblies. When the application starts, it loads these assemblies from an array. When I test this application on my local desktop, it works well (loads and make reflection from assemblies, etc.), but from the cluster computers, it can't load those same assemblies, and throws the following Exception:

FileNotFoundException. Could not load file or assembly or one of its dependencies. The system cannot find the file specified.

网友答案:

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>
网友答案:

Your code either cannot find the root assembly you are trying to load, or this assembly references other DLLs that are not either locally stored with it, or in another discoverable location.

Fusion Log Viewer helps with binding issues.

http://msdn.microsoft.com/en-us/library/e74a18c4(v=vs.71).aspx

网友答案:

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

分享给朋友:
您可能感兴趣的文章:
随机阅读: