Article ID: 897297 - Last Review: November 14, 2007 - Revision: 2.7 How to consume assemblies that are located in a folder that is different from the application base folder in Visual Basic .NET or in Visual Basic 2005On This PageINTRODUCTIONThis article discusses how to consume assemblies that are
located in a folder than is different from the application base folder in a Microsoft
Visual Basic .NET or Microsoft Visual Basic 2005 solution. MORE INFORMATIONWhen you run a Visual Basic .NET or Visual Basic 2005 application, the common
language runtime (CLR) expects to locate any assemblies that the application
uses in either the global assembly cache or the application folder.
When the CLR does not successfully bind to an assembly, you
receive an error message that is similar to the following: System.IO.FileNotFoundException. File or assembly name
AssemblyName, or one of its dependencies, was not found.
Method 1: Install the assemblies in the global assembly cacheThis method requires that you sign an assembly with a strong name. To sign an assembly with a strong name and to then install the assembly in the global assembly cache, follow these steps:
http://msdn2.microsoft.com/en-us/library/6axd4fx6(vs.71).aspx
(http://msdn2.microsoft.com/en-us/library/6axd4fx6(vs.71).aspx)
For more information about how to sign an assembly with a strong
name, visit the following MSDN Web site:http://msdn2.microsoft.com/en-us/library/xc31ft41(vs.71).aspx
(http://msdn2.microsoft.com/en-us/library/xc31ft41(vs.71).aspx)
Note This method may be difficult and time-consuming if many assemblies exist and if the assemblies have dependencies on Microsoft Component Object Model (COM) objects. Any assembly dependencies must also be discoverable by the CLR. Therefore, if you plan to install your assembly to the global assembly cache, also install the dependencies of the assembly into the global assembly cache. Method 2: Use an Application.Config file together with the CodeBase tagBy using an application configuration file, you can specify the location at which the CLR should look for dependent assemblies. Specifically, use the Codebase tag. By using this tag, you can put your assemblies in a separate folder. However, this method still requires that you name the library assembly by using a strong name. To use the application configuration file, follow these steps:
Specifying an assembly's location http://msdn2.microsoft.com/en-us/library/4191fzwb(vs.71).aspx (http://msdn2.microsoft.com/en-us/library/4191fzwb(vs.71).aspx) <codeBase> element http://msdn2.microsoft.com/en-us/library/efs781xb(vs.71).aspx (http://msdn2.microsoft.com/en-us/library/efs781xb(vs.71).aspx) How the runtime locates assemblies http://msdn2.microsoft.com/en-us/library/yx7xezcf(vs.71).aspx (http://msdn2.microsoft.com/en-us/library/yx7xezcf(vs.71).aspx) Method 3: Use the System.Reflection.Assembly.LoadFrom methodThis method uses the Assembly.LoadFrom method to explicitly load an assembly from a fully qualified path name and from a fully qualified file name. To do this, follow these steps:
For more information about the Assembly.LoadFrom method, visit the following MSDN Web site: http://msdn2.microsoft.com/en-us/library/system.reflection.assembly.loadfrom(vs.71).aspx
(http://msdn2.microsoft.com/en-us/library/system.reflection.assembly.loadfrom(vs.71).aspx)
Method 4: Use the AssemblyResolve eventThe AssemblyResolve event occurs whenever the CLR unsuccessfully tries to bind to an assembly. Use the AddHandler method in the application to add an event handler that returns the correct assembly whenever the AssemblyResolve event occurs.Note This method does not require that you name the assemblies by using strong names. The AssemblyResolve event handler must return an [Assembly] object that represents the assembly to which the CLR must bind. Typically you can use the Assembly.LoadFrom method to load the assembly. Then, return the loaded assembly. Note The event handler must be located in a procedure that does not contain any references to the relocated assemblies. Additionally, the event handler must be called before any code that depends on the relocated assemblies is called. The following code is an example of an application that loads an assembly by using the AssemblyResolve event: http://msdn2.microsoft.com/en-us/library/system.appdomain.assemblyresolve(vs.71).aspx
(http://msdn2.microsoft.com/en-us/library/system.appdomain.assemblyresolve(vs.71).aspx)
REFERENCES
For more information, click the following article number to view the article in the Microsoft Knowledge Base:
302340
(http://support.microsoft.com/kb/302340/
)
How
to create an assembly with a strong name in .NET Framework SDK
| Article Translations
|
Back to the top
